Nginx版本升級和回退


此處演示nginx1.18.0升級到nginx1.19.2
nginx1.18.0的安裝,請參考nginx初步入門

1. 查看舊版本(1.18.0)nginx進程

[root@3ad794de6afb application]# ps aux | grep nginx
root      4887  0.0  0.0  45976  1128 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
www       4888  0.0  0.0  46416  2120 ?        S    02:58   0:00 nginx: worker process
root      4899  0.0  0.0   9096   680 pts/0    S+   03:26   0:00 grep --color=auto nginx

2. 編譯安裝新版本的nginx(1.19.2)

2.1 安裝的前置准備,獲取舊版nginx的編譯參數

[root@3ad794de6afb application]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.18.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: --user=www --group=www --prefix=/application/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre        # 復制這段編譯參數

2.2 編譯安裝新版nginx(1.19.2)

[root@3ad794de6afb tools]# wget http://nginx.org/download/nginx-1.19.2.tar.gz
[root@3ad794de6afb tools]# tar zxf nginx-1.19.2.tar.gz -C /application/
[root@3ad794de6afb tools]# cd /application/
[root@3ad794de6afb application]# ls
nginx  nginx-1.18.0  nginx-1.19.2

這里用 2.1 步驟復制的舊版nginx的編譯參數,執行configure

[root@3ad794de6afb application]# cd nginx-1.19.2/
[root@3ad794de6afb nginx-1.19.2]# pwd
/application/nginx-1.19.2
[root@3ad794de6afb nginx-1.19.2]# ./configure --user=www --group=www --prefix=/application/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre
#…………省略部分輸出
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/application/nginx-1.18.0/"
  nginx binary file: "/application/nginx-1.18.0//sbin/nginx"
  nginx modules path: "/application/nginx-1.18.0//modules"
  nginx configuration prefix: "/application/nginx-1.18.0//conf"
  nginx configuration file: "/application/nginx-1.18.0//conf/nginx.conf"
  nginx pid file: "/application/nginx-1.18.0//logs/nginx.pid"
  nginx error log file: "/application/nginx-1.18.0//logs/error.log"
  nginx http access log file: "/application/nginx-1.18.0//logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

注意:有些人可能有疑惑,新下載的nginx在執行./configure的時候--prefix指定的目錄是需要指向舊的nginx所指向的prefix目錄還是隨便指向一個就行,答案是需要指向舊版本的nginx的安裝目錄
執行make

[root@3ad794de6afb nginx-1.19.2]# make   #  完成后不要執行make install
#…………省略部分輸出
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/application/nginx-1.18.0/|" \
	-e "s|%%PID_PATH%%|/application/nginx-1.18.0//logs/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/application/nginx-1.18.0//conf/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/application/nginx-1.18.0//logs/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/application/nginx-1.19.2'

3. 平滑升級

3.1 備份舊版nginx(1.18.0)二進制可執行程序文件

[root@3ad794de6afb nginx-1.19.2]# cd /application/nginx/sbin/ 
[root@3ad794de6afb sbin]# ls
nginx
[root@3ad794de6afb sbin]# cp nginx{,.bak}
[root@3ad794de6afb sbin]# ls
nginx  nginx.bak

3.2 查看nginx升級前的版本

[root@3ad794de6afb sbin]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.18.0        # 升級前,nginx的版本還是1.18.0
#……省略部分輸出

3.3 用新版本nginx(1.19.2)的二進制可執行程序文件,覆蓋舊版本nginx(1.18.0)的二進制可執行程序文件

[root@3ad794de6afb sbin]# cd /application/nginx-1.19.2/objs/
[root@3ad794de6afb objs]# ls
Makefile  autoconf.err  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@3ad794de6afb objs]# cp -f nginx /application/nginx-1.18.0/sbin/nginx
cp: overwrite '/application/nginx-1.18.0/sbin/nginx'? y

3.4 設定舊的服務不再接收用戶請求(下線),新服務啟動子進程接收用戶請求(上線)

3.4.1 先查看當前未升級的nginx進程(這是舊版本的nginx進程)

[root@3ad794de6afb objs]# ps -aux | grep nginx
root      4887  0.0  0.0  45976  1128 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
www       4888  0.0  0.0  46416  2120 ?        S    02:58   0:00 nginx: worker process
root      7525  0.0  0.0   9096   676 pts/0    S+   03:50   0:00 grep --color=auto nginx

3.4.2 找到nginx父進程的pid號,現在對其發送USR2信號

[root@3ad794de6afb objs]# kill -USR2 4887

再次查看進程

[root@3ad794de6afb objs]# ps aux | grep nginx
root      4887  0.0  0.0  45976  1288 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
www       4888  0.0  0.0  46416  2120 ?        S    02:58   0:00 nginx: worker process
root      7528  0.0  0.0  45988  3260 ?        S    03:51   0:00 nginx: master process /application/nginx/sbin/nginx
www       7529  0.0  0.0  46432  1872 ?        S    03:51   0:00 nginx: worker process
root      7531  0.0  0.0   9096   680 pts/0    S+   03:52   0:00 grep --color=auto nginx

現在是nginx的新老版本的進程共存的一種情況。
雖然現在舊版本的nginx進程還存在,但是已經不再接受用戶的請求了。除此之外,舊版本的nginx進程也依然處於監聽的狀態,我們通過lsof命令可以看到,比如:

[root@3ad794de6afb objs]# lsof -p 4887 | grep LISTEN
nginx   4887 root    6u  IPv4           60154699      0t0      TCP *:http (LISTEN)

# 雖然在監聽,但實際不會處理新連接,因為fd已經從epoll中移出了。另外,舊master是新master的父進程,所以新master才能共享打開的監聽端口。保留舊版本的master是為了方便回滾(當然你可以發信號QUIT或者直接殺掉進程)

3.5 關閉舊版本nginx(1.18.0)進程

[root@3ad794de6afb objs]# kill -WINCH  4887 # 進行舊服務進程的關閉,該pid號是舊版本的nginx的master進程的pid號 

再次查看當前nginx進程

[root@3ad794de6afb objs]# ps -aux | grep nginx
root      4887  0.0  0.0  45976  1288 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx       # 關閉了舊版nginx的進程后,就只剩下了舊版nginx的master進程了
root      7528  0.0  0.0  45988  3260 ?        S    03:51   0:00 nginx: master process /application/nginx/sbin/nginx
www       7529  0.0  0.0  46432  1872 ?        S    03:51   0:00 nginx: worker process
root      7540  0.0  0.0   9096   676 pts/0    S+   03:58   0:00 grep --color=auto nginx

# 可以看到現在的舊版本的nginx的worker進程已經全部被殺死了,只剩下的舊版本nginx的master進程
# 確定升級沒有任何問題的話,那么現在我們可以把這個master進程給殺死掉。可以用kill -QUIT把舊master進程殺掉。方法已經教給大家了,但是這里我先不殺死,因為我還要往下演示如何回退。

3.6 查看當前nginx版本

[root@3ad794de6afb objs]# /application/nginx/sbin/nginx -V 
nginx version: nginx/1.19.2                              # 可以看到  這里已經升級成功了
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: --user=www --group=www --prefix=/application/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module --with-pcre

# 測試訪問
[root@3ad794de6afb objs]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.19.2
Date: Mon, 31 Aug 2020 06:01:45 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 31 Aug 2020 02:58:06 GMT
Connection: keep-alive
ETag: "5f4c673e-264"
Accept-Ranges: bytes

4. nginx升級異常后的版本回退

這種情況主要是用於當新版本的nginx升級失敗之后,我們立馬回退到舊版本的nginx

4.1 將舊版本的nginx(1.18.0)二進制文件強行覆蓋新版的nginx(1.19.2)二進制文件

[root@3ad794de6afb objs]# cd /application/nginx-1.18.0/sbin/
[root@3ad794de6afb sbin]# ls
nginx  nginx.bak
[root@3ad794de6afb sbin]# mv nginx.bak nginx
mv: overwrite 'nginx'? y

# 查看進程
[root@3ad794de6afb sbin]# ps aux | grep nginx
root      4887  0.0  0.0  45976  1288 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
root      7528  0.0  0.0  45988  3260 ?        S    03:51   0:00 nginx: master process /application/nginx/sbin/nginx
www       7529  0.0  0.0  46432  2112 ?        S    03:51   0:00 nginx: worker process
root      7551  0.0  0.0   9096   680 pts/0    S+   06:04   0:00 grep --color=auto nginx

4.2 向舊版本nginx進程發送HUP信號

[root@3ad794de6afb sbin]# kill -HUP 4887 #注意這是舊版本的nginx進程pid號

# 說明一下:這個命令就相當與reload指令的作用,把舊的nginx的worker進程拉起來,但是咱們並不是直接使用reload的方式來執行,而是發送HUP信號,
# 它會在沒有worker進程時啟動worker進程,這點需要注意一下。此時再次查看進程

[root@3ad794de6afb sbin]# ps aux | grep nginx
root      4887  0.0  0.0  45976  1288 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
root      7528  0.0  0.0  45988  3260 ?        S    03:51   0:00 nginx: master process /application/nginx/sbin/nginx
www       7529  0.0  0.0  46432  2112 ?        S    03:51   0:00 nginx: worker process
www       7552  0.0  0.0  46416  1876 ?        S    06:06   0:00 nginx: worker process
root      7554  0.0  0.0   9096   676 pts/0    S+   06:07   0:00 grep --color=auto nginx

# 發現多了很多worker進程,多出來的部分是舊版本的nginx進程。

4.3 讓新版本的nginx服務停止接收用戶請求

[root@3ad794de6afb sbin]# kill -USR2 7528

# 此時,接收用戶請求的是舊版本的nginx進程。新版本的nginx進程不再接受用戶請求 

4.4 進行新版本服務進程的關閉

[root@3ad794de6afb sbin]# kill -WINCH 7528

# 再次查看進程,可以看到,新版本nginx的worker進程已經退出服務了
[root@3ad794de6afb sbin]# ps aux | grep nginx
root      4887  0.0  0.0  45976  1288 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
root      7528  0.0  0.0  45988  3260 ?        S    03:51   0:00 nginx: master process /application/nginx/sbin/nginx
www       7552  0.0  0.0  46416  1876 ?        S    06:06   0:00 nginx: worker process
root      7558  0.0  0.0   9096   680 pts/0    S+   06:11   0:00 grep --color=auto nginx

# 現在,舊版本已經回退成功了,我們可以把新版本的nginx的master進程發送QUIT進程將其退出。

4.5 kill掉新版本nginx進程

[root@3ad794de6afb sbin]# ps aux | grep nginx
root      4887  0.0  0.0  45976  1288 ?        Ss   02:58   0:00 nginx: master process /application/nginx/sbin/nginx
www       7552  0.0  0.0  46416  1876 ?        S    06:06   0:00 nginx: worker process
root      7560  0.0  0.0   9096   680 pts/0    S+   06:13   0:00 grep --color=auto nginx

4.6 訪問測試

[root@3ad794de6afb sbin]# curl -I localhost
HTTP/1.1 200 OK                         # 訪問成功
Server: nginx/1.18.0                    # 版本回退成功
Date: Mon, 31 Aug 2020 06:14:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 31 Aug 2020 02:58:06 GMT
Connection: keep-alive
ETag: "5f4c673e-264"

文章原文鏈接馬哥Linux運維,nginx升級與回退


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM