nginx平滑升级(源码才可以)

前机器nginx版本

[root@localhost nginx-1.20.2]# nginx  -v
nginx version: nginx/1.20.2

平滑升级的过程:

下载新版本的源码包,解压,清除可能存在的编译和配置信息,准备configure配置

[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.2  nginx-1.20.2.tar.gz  nginx-1.24.0.tar.gz
[root@localhost ~]# tar -xf nginx-1.24.0.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.2  nginx-1.20.2.tar.gz  nginx-1.24.0  nginx-1.24.0.tar.gz
[root@localhost ~]# cd nginx-1.24.0

查看原来老版本编译配置信息 并在新版本执行同样配置

注意:如果需要新的模块功能需要添加,也可以根据对应的configure配置

[root@localhost nginx-1.24.0]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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_auth_request_module --with-http_random_index_module

主要看configure arguments

使用同样的配置在新版本执行配置

[root@localhost nginx-1.24.0]# ./configure   --prefix=/usr/local/nginx --with-http_ssl_module --with-http_auth_request_module --with-http_random_index_module 

编译(无需安装make install),编译完成后新版的ningx程序在源码目录下的 objs

如果不懂-j4的用法直接使用make即可

[root@localhost nginx-1.24.0]#  make -j4

备份旧版本的nginx服务程序,拷贝新版本的nginx程序

[root@localhost nginx-1.24.0]# cp  /usr/local/nginx/sbin/nginx    /usr/local/nginx/sbin/nginx.old

这里因为nginx在运行,所以需要 -f 强制

[root@localhost nginx-1.24.0]# cp -f  objs/nginx   /usr/local/nginx/sbin/

检测是否已经升级

[root@localhost nginx-1.24.0]# nginx  -v
nginx version: nginx/1.24.0
[root@localhost nginx-1.24.0]# curl -I 192.168.100.16
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Tue, 19 Sep 2023 01:15:04 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 18 Sep 2023 12:36:49 GMT
Connection: keep-alive
ETag: "65084461-264"
Accept-Ranges: bytes

此时 nginx服务程序已经是 新版本1.24,但是 master work提供服务的进程依然是老版本的1.20的进程

我们需要重新启动新版本的master work进程,结束老版本的 master work进程

[root@localhost nginx-1.24.0]# ps  -ajx | grep nginx
 70595  59912  70595  70595 ?            -1 S       99   0:00 nginx: worker process
 60610  60643  60642  60610 pts/0     60642 S+       0   0:00 grep --color=auto nginx
     1  70595  70595  70595 ?            -1 Ss       0   0:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.24.0]# kill -USR2 70595
[root@localhost nginx-1.24.0]# ps  -ajx | grep nginx
 70595  59912  70595  70595 ?            -1 S       99   0:00 nginx: worker process
 70595  60644  70595  70595 ?            -1 S        0   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 60644  60645  70595  70595 ?            -1 S       99   0:00 nginx: worker process
 60610  60647  60646  60610 pts/0     60646 S+       0   0:00 grep --color=auto nginx
     1  70595  70595  70595 ?            -1 Ss       0   0:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.24.0]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid  nginx.pid.oldbin
[root@localhost nginx-1.24.0]# ls /usr/local/nginx/logs/
access.log        error.log         nginx.pid         nginx.pid.oldbin  
[root@localhost nginx-1.24.0]# curl   -I 192.168.100.16
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Tue, 19 Sep 2023 01:22:03 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 18 Sep 2023 12:36:49 GMT
Connection: keep-alive
ETag: "65084461-264"
Accept-Ranges: bytes

向老版本的 master进程发送 WINCH信号,使老版本的work进程完成工作结束

[root@localhost nginx-1.24.0]# kill -WINCH 70595
[root@localhost nginx-1.24.0]# ps  -ajx | grep nginx
 70595  60644  70595  70595 ?            -1 S        0   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 60644  60645  70595  70595 ?            -1 S       99   0:00 nginx: worker process
 60610  60651  60650  60610 pts/0     60650 S+       0   0:00 grep --color=auto nginx
     1  70595  70595  70595 ?            -1 Ss       0   0:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.24.0]# curl   -I 192.168.100.16
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 19 Sep 2023 01:24:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 18 Sep 2023 12:36:49 GMT
Connection: keep-alive
ETag: "65084461-264"
Accept-Ranges: bytes

这里可以看到已经升级到1.24版本在工作了

但是我们还是需要退出老版本的master进程

[root@localhost nginx-1.24.0]# kill -QUIT 70595
[root@localhost nginx-1.24.0]# ps  -ajx | grep nginx
     1  60644  70595  70595 ?            -1 S        0   0:00 nginx: master process /usr/local/nginx/sbin/nginx
 60644  60645  70595  70595 ?            -1 S       99   0:00 nginx: worker process
 60610  60654  60653  60610 pts/0     60653 S+       0   0:00 grep --color=auto nginx
[root@localhost nginx-1.24.0]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid

平滑升级完成,如果在版本添加新的模块,也可以同样步骤平滑处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值