社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

nginx升级与回退

马哥Linux运维 • 4 年前 • 590 次点击  

nginx1.16.1的部署安装我参考的这个博客https://www.cnblogs.com/FengGeBlog/p/13534156.html先看下这个文章,然后一会我们对这个进行版本升级

注意:下文将1.16.1版本成为旧版本nginx,1.18.0成为新版本nginx

1、先确认旧的nginx进程是已经存在的

旧版本nginx启动进程

[root@master2 nginx]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1496 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
nobody   17441  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17442  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17443  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17444  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17445  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process

旧版本的nginx我已经启动了,设置5个worker进程。

2、开始编译安装新版本1.18.0的nginx

编译的步骤如下所示:

wget http://nginx.org/download/nginx-1.18.0.tar.gz
mv nginx-1.18.0.tar.gz /usr/local
cd /usr/local
tar xf nginx-1.18.0.tar.gz

先获取旧版本nginx编译的选项

[root@master2 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 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

拿到这个选项,然后放在新版本nginx里面。执行configure

[root@master2 nginx-1.18.0]# pwd
/usr/local/nginx-1.18.0
[root@master2 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
checking for GeoIP IPv6 support ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/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@master2 nginx-1.18.0]# make

执行完成之后不要执行make install指令,这点需要注意。

3、平滑升级

3.1、先备份旧的nginx二进制可执行程序

[root@master2 nginx-1.18.0]# cd /usr/local/nginx/sbin
[root@master2 sbin]# ls
nginx
[root@master2 sbin]# cp nginx{,.bak}
[root@master2 sbin]# ls
nginx  nginx.bak

3.2、查看未升级前的nginx版本

[root@master2 sbin]# ./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC
xxxx
xxx

3.3、找到nginx-1.18.0新版本的nginx的二进制执行程序

[root@master2 sbin]# cd /usr/local/nginx-1.18.0/
[root@master2 nginx-1.18.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@master2 nginx-1.18.0]# cd objs
[root@master2 objs]# ls
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src

上面的这个nginx就是我们要拿到的新版本的nginx可执行程序文件。

3.4、使用nginx-1.18.0的二进制文件将nginx-1.16.1的二进制文件进行强制覆盖

[root@master2 objs]# cp -f nginx /usr/local/nginx/sbin/nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

3.5、设定旧的服务不再接收用户请求(下线),新服务启动子进程接收用户请求(上线)

先查看当前未升级的nginx进程(这是旧版本的nginx进程)

[root@master2 objs]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1496 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
nobody   17441  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17442  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17443  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17444  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17445  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process

找到nginx父进程的pid号,现在对其发送USR2信号

[root@master2 objs]# kill -USR2 17440                       #设定新的子进程开始接收用户的访问请求,旧的不再接受用户的访问请求

再次查看进程

[root@master2 objs]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1496 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
nobody   17441  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17442  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17443  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17444  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17445  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
root     21081  0.5  0.1  79740  4080 ?        S    20:00   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21083  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21086  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process

现在是nginx的新老版本的进程共存的一种情况。虽然现在旧版本的nginx进程还存在,但是已经不再接受用户的请求了。除此之外,旧版本的nginx进程也依然处于监听的状态,我们通过lsof命令可以看到,比如:




    
[root@master2 objs]# lsof -p 17440 | grep LISTEN                      # 这里的pid号是旧版本的nginx的master进程的pid号
nginx   17440 root    6u  IPv4             120768      0t0      TCP *:http (LISTEN)

虽然在监听,但实际不会处理新连接,因为fd已经从epoll中移出了。另外,旧master是新master的父进程,所以新master才能共享打开的监听端口。保留旧版本的master是为了方便回滚(当然你可以发信号QUIT或者直接杀掉进程)

3.6、进行旧服务进程的关闭

[root@master2 objs]# kill -WINCH 17440                       # 进行旧服务进程的关闭,该pid号是旧版本的nginx的master进程的pid号

再次查看当前nginx进程

[root@master2 objs]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    20:00   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21083  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21086  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process

可以看到现在的旧版本的nginx的worker进程已经全部被杀死了,只剩下的旧版本nginx的master进程

确定升级没有任何问题的话,那么现在我们可以把这个master进程给杀死掉。可以用kill -QUIT把旧master进程杀掉。方法已经教给大家了,但是这里我先不杀死,因为我还要往下演示如何回退。

3.7、查看当前nginx的版本

[root@master2 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: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

可以看到现在已经升级成功了。

还可以访问一下

[root@master2 nginx]# curl http://192.168.50.129
html>


Welcome to nginx!
xxx
xxx
xxx

4、现在我们演示一下如何回退

这种情况主要是用于当新版本的nginx升级失败之后,我们立马回退到旧版本的nginx

4.1、将旧版本的nginx二进制文件强行覆盖

[root@master2 sbin]# mv nginx.bak  nginx
mv: overwrite ‘nginx’? y

查看进程

[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    Aug20   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21083  0.0  0.1  80192  2452 ?        S    Aug20   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   41895  0.0  0.0  80192  2200 ?        S    10:53   0:00 nginx: worker process

4.2、向旧版本nginx进程发送HUP信号

[root@master2 sbin]# kill -HUP 17440                              #注意这是旧版本的nginx进程pid号

说明一下:这个命令就相当与reload指令的作用,把旧的nginx的worker进程拉起来,但是咱们并不是直接使用reload的方式来执行,而是发送HUP信号,它会在没有worker进程时启动worker进程,这点需要注意一下。此时再次查看进程

[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    Aug20   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21083  0.0  0.1  80192  2452 ?        S    Aug20   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   41895  0.0  0.0  80192  2200 ?        S    10:53   0:00 nginx: worker process
nobody   42070  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42071  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42072  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42073  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42074  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process

发现多了很多worker进程,多出来的部分是旧版本的nginx进程。

4.3、让新版本的服务停止接收用户请求

[root@master2 sbin]# kill -USR2 21081

此时,接收用户请求的是旧版本的nginx进程。新版本的nginx进程不再接受用户请求

4.4、进行新版本服务进程的关闭

[root@master2 sbin]# kill -WINCH 21081

查看一下进程

[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    Aug20   0:00 nginx: master process sbin/nginx
nobody   42070  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42071  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42072  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42073  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42074  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process

现在,旧版本已经回退成功了,我们可以把新版本的nginx的master进程发送QUIT进程将其退出。

4.5、kill掉新版本nginx进程

[root@master2 sbin]# kill -QUIT 21081
[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
nobody   42070  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42071  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42072  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42073  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42074  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process

现在已经回退成功了



ZooKeeper 是一个典型的分布式数据一致性解决方案,作为一种协调分布式应用高性能的调度服务,实际的应用场景也非常广泛。

想要了解更多ZooKeeper干货知识,锁定9月1日晚20:00,60分钟带你走进ZooKeeper!


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/73005
 
590 次点击