文档 · 2019年3月1日 0

Nginx安装升级

通常利用系统自带的源安装的nginx 版本比较低,在进行漏洞扫描时候会有很多漏洞所以建议升级到最新版本。

本次就介绍从第一次安装到升级的简单操作。

如果是使用中的 一定要备份配置文件。如果是使用中的 一定要备份配置文件。如果是使用中的 一定要备份配置文件。

首先初次安装,我是centos7系统,初次安装直接yum就可以。

[root@az2-ccs-uat-main-01 /]# yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn

此处省略N个字........................

Installed:
  nginx.x86_64 1:1.12.2-2.el7                                                                                                                                                                

Dependency Installed:
  dejavu-fonts-common.noarch 0:2.33-6.el7     dejavu-sans-fonts.noarch 0:2.33-6.el7              fontconfig.x86_64 0:2.13.0-4.3.el7         fontpackages-filesystem.noarch 0:1.44-8.el7      
  gd.x86_64 0:2.0.35-26.el7                   gperftools-libs.x86_64 0:2.6.1-1.el7               libX11.x86_64 0:1.6.5-2.el7                libX11-common.noarch 0:1.6.5-2.el7               
  libXau.x86_64 0:1.0.8-2.1.el7               libXpm.x86_64 0:3.5.12-1.el7                       libjpeg-turbo.x86_64 0:1.2.90-6.el7        libpng.x86_64 2:1.5.13-7.el7_2                   
  libxcb.x86_64 0:1.13-1.el7                  libxslt.x86_64 0:1.1.28-5.el7                      nginx-all-modules.noarch 1:1.12.2-2.el7    nginx-filesystem.noarch 1:1.12.2-2.el7           
  nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7  nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7  nginx-mod-http-perl.x86_64 1:1.12.2-2.el7  nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 
  nginx-mod-mail.x86_64 1:1.12.2-2.el7        nginx-mod-stream.x86_64 1:1.12.2-2.el7            

Dependency Updated:
  freetype.x86_64 0:2.8-12.el7_6.1                                                                                                                                                           

Complete!
[root@az2-ccs-uat-main-01 /]# nginx -v
nginx version: nginx/1.12.2

安装完了查看版本发现是1.12.2,配置文件默认位置在/etc/nginx/下而目前Nginx官网最新版本是1.15,现在进行升级

先去Nginx官网找到你需要升级的包,下载到服务器。

[root@az2-ccs-uat-main-01 /]# wget http://nginx.org/download/nginx-1.15.9.tar.gz
--2019-03-01 11:13:00--  http://nginx.org/download/nginx-1.15.9.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1031760 (1008K) [application/octet-stream]
Saving to: ‘nginx-1.15.9.tar.gz’

100%[===================================================================================================================================================>] 1,031,760   24.8KB/s   in 29s    

2019-03-01 11:13:30 (35.2 KB/s) - ‘nginx-1.15.9.tar.gz’ saved [1031760/1031760]
[root@az2-ccs-uat-main-01 /]# tar -zxvf nginx-1.15.9.tar.gz 
nginx-1.15.9/
nginx-1.15.9/auto/
nginx-1.15.9/conf/
....
[root@az2-ccs-uat-main-01 /]# cd /nginx-1.15.9
[root@az2-ccs-uat-main-01 nginx-1.15.9]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@az2-ccs-uat-main-01 nginx-1.15.9]#

下载完了解压 进入解压后的目录。

然后查一下当前nginx安装的模块信息,如果你了解自己需要什么模块那可以自己指定。

[root@az2-ccs-uat-main-01 nginx-1.15.9]# nginx -V   此处是大写的V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
[root@az2-ccs-uat-main-01 nginx-1.15.9]# 

根据查出的模块选择你想用的模块进行安装,如果不清楚需要用哪个那就把查询出来的全部复制。

执行安装

###
[root@az2-ccs-uat-main-01 nginx-1.15.9]# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
checking for OS
 + Linux 3.10.0-862.14.4.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
checking for gcc -pipe switch ... found
checking for --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E" ... not found
./configure: error: the invalid value in --with-ld-opt="-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E"


####此处报错 说明这个模块的参数错误,我们可以在编译信息中去掉这个模块。然后再次编译。

[root@az2-ccs-uat-main-01 nginx-1.15.9]# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic'
checking for OS
 + Linux 3.10.0-862.14.4.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... not found
checking for gcc builtin atomic operations ... not found
checking for C99 variadic macros ... not found
checking for gcc variadic macros ... not found
...............................................

./configure: no supported file AIO was found
Currently file AIO is supported on FreeBSD 4.3+ and Linux 2.6.22+ only
###路径不对或者没有找到AIO这个模块,同上,再编译参数中去掉。

[root@az2-ccs-uat-main-01 nginx-1.15.9]# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic'
checking for OS
 + Linux 3.10.0-862.14.4.el7.x86_64 x86_64
checking for C compiler ... found
...........................
checking for int size ...
./configure: error: can not detect int size
###这是说CPU优化模块或者配置参数有问题。没有强迫症去掉有关优化的模块就可以了就可以了。
再次执行
##报错
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
###这个是说某些依赖库没有安装。
###下面进行安装
[root@az2-ccs-uat-main-01 nginx-1.15.9]# yum -y install pcre-devel openssl openssl-devel

###报错
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
解决方法:
[root@az2-ccs-uat-main-01 nginx-1.15.9]# yum -y install libxslt-devel libxml2

###报错
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
###http_image_filter_module是nginx提供的集成图片处理模块,需要gd-devel的支持
解决方法:
[root@az2-ccs-uat-main-01 nginx-1.15.9]# yum -y install gd-devel
###报错
./configure: error: perl module ExtUtils::Embed is required
###缺少ExtUtils
解决方法:
[root@az2-ccs-uat-main-01 nginx-1.15.9]# yum -y install perl-devel perl-ExtUtils-Embed
###报错
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
缺少GeoIP
解决方法:
[root@az2-ccs-uat-main-01 nginx-1.15.9]# yum -y install GeoIP GeoIP-devel GeoIP-data


nginx编译成功回显其目录结构:类似下面这种:
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/share/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"
  nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"
  nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"
  nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"

./configure: warning: the "--with-ipv6" option is deprecated   ###此处为提醒
[root@az2-ccs-uat-main-01 nginx-1.15.9]# 
最后执行安装
[root@az2-ccs-uat-main-01 nginx-1.15.9]# make && make install
安装完成 

最后查看版本信息

[root@az2-ccs-uat-main-01 nginx-1.15.9]# nginx -v
nginx version: nginx/1.15.9
[root@az2-ccs-uat-main-01 nginx-1.15.9]#

全部搞定!

 

打赏