Nginx的平滑升级图文搭配

9 篇文章 0 订阅

原理
当需要将正在运行中的nginx升级,添加/删除服务模块时,可以在不中断服务的情况下,使用新版本,重编译的Nginx可执行程序替换旧版本的可执行程序,步骤如下:
• 使用新的可执行程序替换旧的可执行程序,对于编译安装的Nginx,可以将新版本编译安装到旧版本的nginx安装路径中.替换之前,最好备份一下旧的可执行程序
• 发送以下指令: Kill –USR2 旧版本的nginx主进程号
• 旧版本的主进程将重命名它的pid文件为.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程.
• 此时,新,旧版本的nginx实例会同时运行,共同处理输入的请求.要逐步停止旧版本的nginx实例,你必须发送WINCH信号给旧的主进程,然后,它的工作进程就将开始从容关闭:kill –WINCH 旧版本的Nginx主进程号
• 一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了.
• 这时候,我们可以决定是使用新版本,还是恢复到旧的版本;
Kill –HUP 旧的主进程号:Nginx将在不重载配置文件的情况下启动它的工作进程;
Kill –QUIT 新的主进程号:从容关闭其他工作进程(woker process);
Kill –TERM 新的主进程号:强制退出;
Kill 新的主进程号或旧的主进程号:如果因为某些原因新的工作进程不能退出,则向其发送kill信号.
新的主进程退出后,旧的主进程会移除.oldbin前缀,恢复为他的.pid文件,这样,一切就都恢复到升级之前了,如果尝试升级成功,而你也希望保留新的服务器时,可发送QUIT信号给旧的主进程,使其退出而只留新的服务器运行。
下面我们具体来操作一下:
1、 查看当前版本:
nginx -v
2、 然后在官网下载新版本 wget http://nginx.org/download/nginx-1.18.0.tar.gz
3、 tar -xzvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
4、 ./configure
在这里遇到了失败的问题,
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
这里需要下载一个插件 yum -y install pcre-devel 来解决这个问题,当在此执行时又遇到了问题,问题是这样的,
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module --with-openssl= options.
上网查了查,需要下载关于openssl的相关软件,yum -y install openssl openssl-devel 这样再次执行./configure --user=nginx --group=nginx --prefix=/usr/local/nginx18 --with-http_stub_status_module --with-http_ssl_module && make && make install
这条语句就可以了。
5、查看进程号
ps aux | grep nginx
在这里插入图片描述
6、替换旧的程序
mv /usr/local/nginx/sbin/nginx{,.bak}
cp /usr/local/nginx18/sbin/nginx /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
[root@localhost ~]# cat /usr/local/nginx/logs/nginx.pid
68595
[root@localhost ~]# kill -USR2 68595
[root@localhost ~]# cat /usr/local/nginx/logs/nginx.pid.oldbin
68595
[root@localhost ~]# kill -WINCH 68595
[root@localhost ~]# ps aux |grep ngin[x]
root 58943 0.0 0.3 45940 3260 ? S 13:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 58944 0.0 0.1 46388 1888 ? S 13:34 0:00 nginx: worker process
root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值