2. Nginx 安装

目录

1. Nginx 包管理工具安装之 yum
2. Nginx 包管理工具安装之 apt
3. Nginx 源码包编译安装


1. Nginx 包管理工具安装之 yum

对于 RHEL/Centos 系统,可以使用 yum 仓库包管理工具安装

  • 配置 yum 源,修改配置文件:/etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

修改 baseurl 中的 OS (可用 rhelcentos 替换)和 OSRELEASE (可用 67 替换) 两个字段。

例如,我的实验环境为 CeontOS 7 的系统:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

更新仓库包的索引,下载安装 Nginx

yum update

yum -y install nginx

2. Nginx 包管理工具安装之 apt

对于 Debian/Ubuntu 系统,需先现添加 Nginx 包和 apt 仓库中的 key认证
sudo apt-key add nginx_signing.key
增加 apt 仓库源,新增配置文件/etc/apt/sources.list.d/nginx.list,需更具不同系统修改
  • Debian 系统
deb http://nginx.org/packages/debian/ codename nginx
deb-src http://nginx.org/packages/debian/ codename nginx
  • Ubuntu 系统
deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx

其中,字段 codename 根据如下对应的系统版本的 codename 修改:

Debian:

Version Codename Supported Platforms
8.x jessie x86_64, i386
9.x stretch x86_64, i386

Ubuntu:

Version Codename Supported Platforms
140.4 trusty x86_64, i386, aarch64/arm64
16.04 xenial x86_64, i386, ppc64el, aarch64/arm64
18.04 bionic x86_64, aarch64/arm64
18.10 cosmic x86_64

例如: 系统版本为: Debian 9, 则修改配置文件内容如下:

deb http://nginx.org/packages/debian/ stretchnginx
deb-src http://nginx.org/packages/debian/ stretchnginx

例如:系统版本为:Ubuntu 16.04,则修改配置文件内容如下:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx
对于 Debian/Ubuntu 系统,运行如下命令,更新 apt 仓库的包索引,安装 Nginx
apt-get update
apt-get install nginx

3. Nginx 源码包编译安装

Nginx 代码提供了两种独立的下载分支——开发班与稳定版。开发分支是一个正处于积极开发状态的版本。在这个版本中,会有一些新功能被集成到其中,在稳定版中是找不到这些功能的。当发布一个 “开发” 版时,它会经历同样的 QA 和作为稳定版的一组类似功能测试。因此,无论哪一个分支都可以用于生产环境中,而稳定版则保持不变。因此,为了与第三方模块向下兼容,在稳定版中第三方模块都可以有效使用。

下载源码包

官网下载地址:http://nginx.org/en/download.html

为了从源码编译 Nginx,需提前安装好编译器,其次,如过要分别启用 SSL 支持和使用 rewrite 模块,那么还需要提供对应的 OpenSSLPCRE(Perl Compatible Regular Expressions)库及开发头文件。rewrite 模块是默认安装的。如果没有 PCRE 库与开发头文件,则需要在配置阶段禁用 rewrite 模块。

解压缩源码包
tar -xf nginx-1.14.2.tar.gz
cd nginx-1.14.2

可看到目录下有如下文件

drwxr-xr-x. 6 1001 1001   4096 Dec 10 20:17 auto
-rw-r--r--. 1 1001 1001 288742 Dec  4 09:52 CHANGES
-rw-r--r--. 1 1001 1001 440121 Dec  4 09:52 CHANGES.ru
drwxr-xr-x. 2 1001 1001   4096 Dec 10 20:17 conf
-rwxr-xr-x. 1 1001 1001   2502 Dec  4 09:52 configure
drwxr-xr-x. 4 1001 1001     68 Dec 10 20:17 contrib
drwxr-xr-x. 2 1001 1001     38 Dec 10 20:17 html
-rw-r--r--. 1 1001 1001   1397 Dec  4 09:52 LICENSE
drwxr-xr-x. 2 1001 1001     20 Dec 10 20:17 man
-rw-r--r--. 1 1001 1001     49 Dec  4 09:52 README
drwxr-xr-x. 9 1001 1001     84 Dec 10 20:17 src

关于上述目录结构的具体介绍如下:

  • auto 目录:存放大量的脚本文件,和 configure 脚本程序相关
  • conf 目录:存放 Nginx 服务器的配置文件
  • contrib 目录:存放其他机构或组织贡献的文档资料
  • html 目录:存放默认网站文件
  • man 目录:存放 Nginx 的帮助文档
  • src 目录: 存放 Nginx 的源代码
  • CHANGES、CHANGES.ru、LICENSE 和 README 都是 Nginx 服务器的相关文档资料
编译安装 Nginx
  1. 安装依赖包

由于 Nginx 中的功能是模块化的,而模块又依赖于一些软件包(如 pcre 库,zlib库 和 openssl 库)才能使用,因此在安装 Nginx 之前,需提前完成 Nginx 模块依赖的软件包安装。

软件包 说明
pcre-devel 为 Nginx 模块(如 rewrite)提供正则表达式库
zlib-devel 为 Nginx 模块(如 gzip)提供数据压缩用的函数库
openssl-devel 为 Nginx 模块(如 ssl)提供密码算法、证书及 SSL 协议等功能

可以使用 yum 方式安装 Nginx 相关依赖包,

yum install pcre-devel openssl-devel zlib-devel
  1. Nginx 编译安装
cd nginx-1.14.2

配置 Nginx 的编译选项,可用命令 ./configure --help 查看编译时的选项参数

[root@centos1 nginx-1.14.2]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

通用配置选项介绍

选项 解释
--prefix= Nginx 安装的根路径,所有其他的安装路径都需要依赖于该选项
--sbin-path= 指定 nginx 二进制文件的路径。如果没有指定,那么这个路径就会依赖于——prefix 选项
--conf-path= 指定配置文件存放路径
--error-log-path= 指定错误文件的路径
--pid-path= 指定文件将会写入 nginx master 进程的 pid,通常在 /var/run 下
--lock-path= 共享存储器互斥锁文件的路径
--user= worker 进程运行的用户
--group= worker 进程运行的组
--with-file-aio 为 FreeBSD 4.3+和Linux 2.6.22 + 系统启用异步 I/O
--with-debug 这个选项用于启用调试日志。生产环境中不建议开启

为 Web 或者 Mail 服务器配置 Nginx

mail 配置选项

选项 说明
--with-mail 该选项用于启用 mail 模块,该模块默认没有被激活
--with-mail_ssl_module 为了代理任何一种类型的使用 SSL/TLS 的 mail,需要激活该模块
--without-mail_pop3_module 在启用 mail 模块后,单独地禁用 POP3 模块
--without-mail_imap_module 在启用 mail 模块后,单独地禁用 IMAP 模块
--without-mail_smtp_module 在启用 mail 模块后,单独禁用 SMTP 模块
--without-http 该选项将会完全禁用 http 模块,如果只想支持 mail,那么可以使用它

http 配置选项

选项 说明
--without-http-cache 在使用 upstream 模块时,Nginx 能够配置本地缓存内容。该选项能禁用缓存
--with-http_perl_module Nginx 配置能够扩展使用 Perl 代码。(如果 I/O受阻时,使用这个模块会降低性能)
--with-perl_modules_path= 对于额外嵌入的 Perl 模块,使用该选项指定该 Perl 解析器的路径
--with-perl= 指定 Perl 路径
--http-log-path= http 访问日志的默认路径
--http=client-body-temp-path= 从客户端收到请求后,该选项设置的默认用于作为请求体临时存放的目录。如果WebDAV模块启用,那么推荐设置该路径为同一文件系统上的目录作为最终的目的地
--http-proxy-temp-path= 在使用代理后,通过该选项设置存放临时文件路径
--http-fastcgi-temp-path= 设置 FastCGI 临时文件的目录
--http-uwsgi-temp-path= 设置 uWSGI 临时文件的目录
--http-scgi-temp-path= 设置 SCGI 临时文件的目录
http 模块配置选项
选项 说明
--with-http_ssl_module 如果需要对流量进行加密,可以使用这个选项,在URL 中开始部分将会是 https (需要 OpenSSL 库)
--with-http_realip_module 如果你的 Nginx 在七层负载均衡器或者其他设备之后,它们将 http 头中的客户端 IP 地址传递,那么你将会需要启用这个模块。在多个客户处于一个 IP 地址的情况下使用
--with-http_addition_module 这个模块作为一个输出过滤器,能够在请求经过一个 location 前后者后时在该 location 本身添加内容
--with-http_xslt_module 该模块用于处理 XML 响应转换,在将图像投递到客户之前进行处理(需要 libgd 库)
--with-http_geoip_module 使用该模块,能够设置各种变量以便在配置文件中的区段使用,基于地理位置查找客户端 IP 地址(需要 MaxMind GeoIP库和响应的预编译数据库文件)
--with-http_sub_module 该模块实现了替代过滤,在响应中用一个字符串替代另一个字符串,提醒:使用该模块隐式禁用标头缓存
--with-http_dav_module 启用这个模块将激活使用 WebDAV 的配置指令。注意,这个模块也只能在有需要使用的基础上启用,如果配置不正确,它可能带来安全问题
--with-http_flv_module 如果需要提供 Flash 流媒体视频文件,那么该模块将会提供伪流媒体
--with-http_mp4_module 这个模块支持 H.264/AAC 文件伪流媒体
--with-http_gzip_static_module 当被调用的资源没有 .gz 结尾格式的文件时,如果想支持发送预压缩版本的静态文件,那么使用这个模块
--with-http_gunzip_module 对于不支持 gzip 编码的客户,该模块用于为客户解压缩预压缩内容
--with-http_random_index_module 如果像提供一个目录中随机选择文件的索引文件,那么这个模块需要被激活
--with-http_secure_link_module 该模块提供了一种机制,它会将一个散列值链接到一个 URL 中,因此,只用那些使用正确的密码能够计算链接
--with-http_sub_status_module 启用这个模块后会收集 Nginx 自身的状态信息。输出的状态信息可以使用 RRDtool 或类似的内容来绘制成图

5. 参考文章

参考文章一
参考文章一

posted @ 2018-12-03 11:22  McSiberiaWolf  阅读(138)  评论(0编辑  收藏  举报