nginx版本低,可是又不能關閉怎麼辦?一招教你平滑升級

1.查看現有的 nginx 編譯參數

我這裏以1.16.0爲例升級到1.16.1(編譯安裝的才能夠)
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V       
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --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=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream

2.上傳新版本的源碼包nginx-1.16.1.tar.gz,解壓到/usr/local/

(注意:按照原來的編譯參數安裝 nginx 的方法進行安裝,只須要到 make,千萬不要 make install 。若是make install 會將原來的配置文件覆蓋,你以前的東西就沒有了)nginx

[root@localhost ~]# ls     #我這裏已經上傳了新版本的源碼包
nginx-1.16.1.tar.gz
[root@localhost ~]# tar -xf nginx-1.16.1.tar.gz -C /usr/local/    #解壓到指定目錄
[root@localhost ~]# cd /usr/local/nginx-1.16.1/

在這裏插入圖片描述

#這裏面的參數是將第一步中的參數複製過來
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --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=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
[root@localhost nginx-1.16.1]# make
#####千萬不要make install

3.平滑升級

#備份原 nginx 二進制文件
[root@localhost nginx-1.16.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.1.16.0.bak
#備份二進制文件和 nginx 的配置文件(期間nginx不會中止服務)
[root@localhost nginx-1.16.1]# cp /usr/local/nginx-1.16.1/objs/nginx /usr/local/nginx/sbin/
#複製新的nginx二進制文件,進入新的nginx源碼包
#測試新版本的nginx是否正常
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost nginx-1.16.1]# ll /var/run/nginx.pid*
-rw-r--r--. 1 root root 6 3月  11 22:12 /var/run/nginx.pid
#查看nginx pid
[root@localhost nginx-1.16.1]# kill -USR2 `cat /var/run/nginx.pid`
#給nginx發送平滑遷移信號(若不清楚pid路徑,請查看nginx配置文件)
[root@localhost nginx-1.16.1]# ll /var/run/nginx.pid*
-rw-r--r--. 1 root root 6 3月  11 22:19 /var/run/nginx.pid
-rw-r--r--. 1 root root 6 3月  11 22:12 /var/run/nginx.pid.oldbin
#查看nginx pid,會出現一個nginx.pid.oldbin
[root@localhost nginx-1.16.1]# kill -WINCH `cat /var/run/nginx.pid.oldbin`  #從容關閉舊的Nginx進程
[root@localhost nginx-1.16.1]# kill -HUP `cat /var/run/nginx.pid.oldbin`    #此時不重載配置啓動舊的工做進程
[root@localhost nginx-1.16.1]# kill -QUIT `cat /var/run/nginx.pid.oldbin`   #結束工做進程,完成這次升級
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V      #再次查看版本
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --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=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream

會發現版本升級了,並且老進程也沒有關閉,平滑升級成功。測試

大家的評論和點贊是我寫文章的最大動力,蟹蟹。ui

相關文章
相關標籤/搜索