nginx 之热升级流程(平滑升级)

目录

1、安装nginx 1.14.2 

2、编译新版本的nginx 

按照第一步操作,下载新版本的nginx 

3、热升级nginx


1、将旧nginx二进制文件换成新的nginx二进制 文件(注意备份)

2、向master进程发送USR2信号.

3、master进程修改pid文件名,加后缀.olidbin

4、master进程用新nginx文件启动新master进程

5、向老master进程发送QUIT信号,关闭老master进程

6、回滚: 向老master发送HUP,向新master发送QUIT

下面我们来演示一下 ,将nginx 从1.14.2 平滑升级到1.1.6.1版本

1、安装nginx 1.14.2 

首先我们下载一个nginx-1.14.2 的版本

wget https://nginx.org/download/nginx-1.14.2.tar.gz

解压之后,进行nginx 目录,运行如下命令,首先保证/soft/nginx 目录存在

./configure  --prefix=/soft/nginx 

打印如下:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/soft/nginx"
  nginx binary file: "/soft/nginx/sbin/nginx"
  nginx modules path: "/soft/nginx/modules"
  nginx configuration prefix: "/soft/nginx/conf"
  nginx configuration file: "/soft/nginx/conf/nginx.conf"
  nginx pid file: "/soft/nginx/logs/nginx.pid"
  nginx error log file: "/soft/nginx/logs/error.log"
  nginx http access log file: "/soft/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"

完成之后,执行如下命令

make
make install 

可以看到/soft/nginx 已经出现 编译后的nginx 文件了

[root@zk02 nginx]# pwd
/soft/nginx
[root@zk02 nginx]# ls 
conf  html  logs  sbin

在/etc/profile 文件配置如下

#nginx 
export NGINX_HOME=/soft/nginx
export PATH=$PATH:$NGINX_HOME/sbin

配置完成之后,source 一下.

source  /etc/profile 

查看版本

[root@zk02 ~]# nginx -v 
nginx version: nginx/1.14.2

以绝对启动nginx 

[root@zk02 sbin]# /soft/nginx/sbin/nginx 

2、编译新版本的nginx 

按照第一步操作,下载新版本的nginx 

wget  https://nginx.org/download/nginx-1.16.1.tar.gz

编译该版本的源码

./configure  --prefix=/soft/nginx 

编译

[root@zk02 nginx-1.16.1]# pwd
/root/nginx_download/nginx-1.16.1
[root@zk02 nginx-1.16.1]# make

在obj目下下可以看到编译好的nginx 文件

3、热升级nginx

我们看下老的nginx 进程 ,master 进程号为6965

[root@zk02 sbin]# ps -ef | grep nginx 
root      6965     1  0 08:21 ?        00:00:00 nginx: master process /soft/nginx/sbin/nginx
nobody    6966  6965  0 08:21 ?        00:00:00 nginx: worker process
root      7017  6938  0 08:24 pts/1    00:00:00 grep --color=auto nginx

首先备份老的nginx 二进制文件,回滚的时候会用到

[root@zk02 sbin]# cp nginx  nginx.old 
[root@zk02 sbin]# ll
total 7320
-rwxr-xr-x 1 root root 3746440 Jan 28 07:28 nginx
-rwxr-xr-x 1 root root 3746440 Jan 28 07:50 nginx.old

 将新的nginx 文件覆盖旧的nginx 文件.

[root@zk02 objs]#  cp -f /root/nginx_download/nginx-1.16.1/objs/nginx  /soft/nginx/sbin/
cp: overwrite ?.soft/nginx/sbin/nginx?. y

  给master 进程 执行USR2信号,会生成新的master和worker进程.

[root@zk02 sbin]# kill -USR2 6965
[root@zk02 sbin]# ps -ef | grep nginx 
root      6965     1  0 08:21 ?        00:00:00 nginx: master process /soft/nginx/sbin/nginx
nobody    6966  6965  0 08:21 ?        00:00:00 nginx: worker process
root      7021  6965  0 08:25 ?        00:00:00 nginx: master process /soft/nginx/sbin/nginx
nobody    7022  7021  0 08:25 ?        00:00:00 nginx: worker process

可以看到pid文件名已更改

給老master进程发送QUIT 信号,优雅的退出

[root@zk02 sbin]# kill  -QUIT 6965 
[root@zk02 sbin]# ps -ef | grep nginx 
root      7021     1  0 08:25 ?        00:00:00 nginx: master process /soft/nginx/sbin/nginx
nobody    7022  7021  0 08:25 ?        00:00:00 nginx: worker process
root      7054  6938  0 08:34 pts/1    00:00:00 grep --color=auto nginx

至此,热升级完成.

 

注意: 升级的过程(kill -USR2 6965),可能报如下错误. 

2020/01/28 08:03:06 [alert] 6819#0: execve() failed while executing new binary process "nginx" (2: No such file or directory)
以绝对路径执行nginx 文件即可.

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值