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升级与回退

posted @ 2020-08-31 14:25  三花  阅读(1958)  评论(0编辑  收藏  举报