目錄
Nginx的平滑升級
1、查看原先系統Nginx版本和編譯參數並記錄
[root@localhost ~]# nginx -v
nginx version: nginx/1.13.12
[root@localhost ~]# nginx -V
nginx version: nginx/1.13.12
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-http_xslt_module --with-pcre
2、下載最新版本Nginx並解壓
[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.1.tar.gz
--2018-11-15 02:24:36-- http://nginx.org/download/nginx-1.14.1.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ...
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1014040 (990K) [application/octet-stream]
Saving to: ‘nginx-1.14.1.tar.gz’
100%[==================================================================================================================================>] 1,014,040 117KB/s in 7.6s
2018-11-15 02:24:44 (130 KB/s) - ‘nginx-1.14.1.tar.gz’ saved [1014040/1014040]
[root@localhost ~]# tar -zxf nginx-1.14.1.tar.gz
3、編譯Nginx 1.14.1
[root@localhost ~]# cd nginx-1.14.1
[root@localhost nginx-1.14.1]# ./configure --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-http_xslt_module --with-pcre
[root@localhost nginx-1.14.1]# make #到make這一步即可,不需要執行make install
4、備份原來的nginx啟動腳本
[root@localhost nginx-1.14.1]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# mv nginx nginx_old
5、拷貝nginx-1.14.1目錄下的obj目錄下的nginx到nginx的sbin目錄下
[root@localhost sbin]# cp /root/nginx-1.14.1/objs/nginx /usr/local/nginx/sbin/
6、回到nginx-1.14.1目錄下執行make upgrade
[root@localhost sbin]# cd /root/nginx-1.14.1
[root@localhost nginx-1.14.1]# make upgrade #這一步會將結束舊進程,並開啟新的進程進行管理nginx的任務,從而達到平滑升級的效果
/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`
這里需要注意的是,使用make upgrade進行平滑升級時,會默認發送USR2信號到/usr/local/nginx/logs/nginx.pid,但是如果你的pid文件位置不一致,就會出現文件不存在的ERROR
而我們需要做的是,放棄使用make upgrade,而是直接使用以下命令,假設nginx.pid的路徑為:/var/run/nginx.pid
[root@localhost nginx-1.14.1]# /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
[root@localhost nginx-1.14.1]# kill -USR2 `cat /var/run/nginx.pid`
此時會在/var/run/下生成一個nginx.pid.oldbin的pid文件
[root@localhost nginx-1.14.1]# kill -QUIT `cat /var/run/nginx.pid.oldbin` #退出舊進程
7、查看nginx的最新版本
[root@localhost nginx-1.14.1]# nginx -v
nginx version: nginx/1.14.1
[root@localhost nginx-1.14.1]# nginx -V
nginx version: nginx/1.14.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-http_xslt_module --with-pcre