下文以Nginx 1.14.2版本为例描述通过源码编译安装Nginx的操作步骤,其他版本的Nginx也可参考本章节。
适合通过源码编译安装的Nginx版本与操作系统版本如表1所示。
适用的软件版本 |
是否适配CentOS 7.6 |
是否适配CentOS 8.1 |
是否适配openEuler 20.03 LTS SP1 |
是否适配openEuler 22.03 LTS SP1 |
---|---|---|---|---|
Nginx 1.14.2 |
是 |
是 |
是 |
是 |
Nginx 1.19.3 |
是 |
否 |
否 |
是 |
Nginx 1.21.5 |
否 |
否 |
否 |
是 |
以下命令中,“1.14.2”表示Nginx的版本号。安装其他版本的Nginx时,请将“1.14.2”替换为对应的Nginx版本号。
若服务器可以访问网络,则通过wget命令直接下载源码。此处以使用wget命令下载Nginx 1.14.2的源码为例进行说明。下载其他Nginx版本的源码时,替换掉Nginx源码的下载链接即可。
1 2 | cd /home wget https://nginx.org/download/nginx-1.14.2.tar.gz --no-check-certificate |
1 | tar -xvf nginx-1.14.2.tar.gz |
1 | cd nginx-1.14.2/ |
1 | ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-cc-opt="-fPIC" |
1 | ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-openssl=/home/openssl-OpenSSL_1_1_1a |
1 | ./configure --prefix=/usr/local/nginx --with-http_ssl_module |
Nginx的配置参数选项说明如表2所示。
参数选项 |
说明 |
---|---|
--prefix=PATH |
指定Nginx的安装目录。默认安装目录为“/usr/local/nginx”。 |
--with-http_ssl_module |
使用https协议模块。默认情况下,该模块没有被构建。前提是OpenSSL与openssl-devel已安装。 |
--with-http_realip_module |
通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP或X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址。 |
--with-http_addition_module |
可以实现在响应之前或之后追加文本内容。 |
--with-http_flv_module |
加载流媒体(flash)。 |
--with-http_mp4_module |
加载流媒体(mp4)。 |
--with-pcre |
设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre自动找到库文件。使用--with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4-8.30)并解压。perl正则表达式使用在location指令和ngx_http_rewrite_module模块中。 |
--with-openssl=PATH |
指定OpenSSL(版本1.1.1a)的源码解压目录。 |
--with-zlib=PATH |
指定zlib(版本1.1.3-1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib。 |
--with-http_stub_status_module |
用来看管Nginx的当前状态。 不需要配置with-http_stub_status_module模块,该统计模块会影响Nginx性能。 |
--add-module=PATH |
添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译。(Tengine可以在新加入module时无需重新编译。) |
1 | make -j60 && make install |
-j60参数表示充分利用CPU多核优势,加快编译速度。参数-j后数字为CPU核数,可通过lscpu或cat /proc/cpuinfo | grep processor | wc -l查看,此数值应小于等于CPU核数。
1 | ls /usr/local/nginx
|
1 | /usr/local/nginx/sbin/nginx -v
|