對於我們在生產中的nginx服務器端,如需升級咱們的nginx系統版本,前題就是需要在升級過程中不能影響到nginx的運行,今天我們就介紹一下nginx的平滑升級,講述一下據體的升級操作過程。
還有一個就是,最近遇到項目上線需要進行安全測試,按圈掃描之後報告當中提示nginx存在安全漏洞,需要進行修復,一種解決辦法是去打補丁,一種就是直接升級到該漏洞影響的版本之外,我們這裏說的就是第二種方式。
首先我們查看下服務器上面原nginx的版本
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module --add-module=/root/ngx_http_substitutions_filter_module
[root@localhost ~]#
在這裏我們可以看到這臺nginx服務器端的版本是1.15.9,下面是配置選項,這個一會我們下載最新的版本後,需要用到這個配置項來對新版本進行配置。
下載最新的nginx,(官方下載地址:http://nginx.org/en/download.html)
[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.17.0.tar.gz
--2019-06-19 12:37:46-- http://nginx.org/download/nginx-1.17.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 1032978 (1009K), 1016293 (992K) remaining [application/octet-stream]
Saving to: ‘nginx-1.17.0.tar.gz’
100%[++=======================================================================================================================================>] 1,032,978 33.9KB/s in 33s
2019-06-19 12:38:19 (30.3 KB/s) - ‘nginx-1.17.0.tar.gz’ saved [1032978/1032978]
[root@localhost ~]#
解壓nginx-1.17.0.tar.gz,並cd到解壓目錄
[root@localhost ~]# tar -zxvf nginx-1.17.0.tar.gz
………………………………………………………………
nginx-1.17.0/auto/cc/name
nginx-1.17.0/auto/cc/owc
nginx-1.17.0/auto/cc/sunc
[root@localhost ~]# cd nginx-1.17.0
[root@localhost nginx-1.17.0]#
配置安裝選項,要和我們上面查詢版本時所查看到的配置保持一致,要不你可就少安裝功能了,導致一些功能不可用了。
[root@localhost nginx-1.17.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module --add-module=/root/ngx_http_substitutions_filter_module
checking for OS
+ Linux 3.10.0-957.1.3.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-36) (GCC)
………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………
編譯
[root@localhost nginx-1.17.0]# make
……………………………………………………………………………………
……………………………………………………………………………………
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/root/nginx-1.17.0'
運行平滑升級命令
[root@localhost nginx-1.17.0]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@localhost nginx-1.17.0]#
注:到這裏運行平滑級後,升級並沒有完成,需要再次編譯安裝纔可以,原本第一次認爲到這裏升級就結束了,但查詢版本號還是原來的版本。
編譯安裝(其實make 和make install 可以一步運行寫成。make && make install)
[root@localhost nginx-1.17.0]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.17.0'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
|| mv '/usr/local/nginx/sbin/nginx' \
'/usr/local/nginx/sbin/nginx.old'
至此我們的平滑升級結束,我們可以再次查詢nginx的版本號。
[root@localhost nginx-1.17.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module --add-module=/root/ngx_http_substitutions_filter_module
[root@localhost nginx-1.17.0]#
查詢得到我們已把nginx升級到最新的1.17.0版本了,經測試,你也可以把平滑升級命令make upgrade這一步省略掉,直接編譯安裝,但安裝後需要重啓nginx纔可以,是重啓,不是重新加載。