热升级介绍
也叫热部署,平滑升级,也就是在不停止nginx服务的情况下,完成nginx的升级。
需要用到USR2信号跟WINCH信号。
升级方式
我此时的nginx版本是1.16.1,安装路径在/usr/local/nginx
下,使用的绝对路径启动的nginx。
第一种方式
先查看内存运行中的nginx的进程号
$ ps -ef | grep nginx
root 88457 1 0 15:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 88458 88457 0 15:34 ? 00:00:00 nginx: worker process
root 92141 74104 0 15:58 pts/1 00:00:00 grep --color=auto nginx
$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.16.1
备份nginx二进制文件跟动态模块文件
#备份二进制文件
$ cd /usr/local/nginx/sbin
$ mv nginx nginx.old-1.16.1
#备份动态模块文件,如果没用到动态模块直接跳过
$ cd /usr/local/nginx/modules
$ mkdir nginx-1.16.1
$ mv *.so nginx-1.16.1
#将.so文件修改为.so.old-1.16.1文件
$ for name in `ls *.so`;do mv $name $name.old-1.16.1;done
#或者使用rename so so.old-1.16.1 *
安装新版本
注意:
编译安装时路径需要跟1.16.1版本安装路径相同。
使用nginx -V
查看Nginx开启的模块,安装1.18.0时复制即可
执行make即可,无需执行make install
替换老版本nginx二进制文件跟动态模块文件
如果没有开启动态模块,忽略即可。
$ cd /root/soft/nginx-1.18.0/nginx-1.18.0/objs
$ cp nginx /usr/local/nginx/sbin/
$ cp *.so /usr/local/nginx/modules/
此时nginx的版本已为1.18.0,但内存中还是1.16.1
$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
$ ps -ef | grep nginx
root 88457 1 0 15:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 88458 88457 0 15:34 ? 00:00:00 nginx: worker process
root 92141 74104 0 15:58 pts/1 00:00:00 grep --color=auto nginx
启动新版本进程
向老版本master进程发送USR2信号之后,会启动一个新的进程,因nginx二进制文件被替换,所以启动的新进程即为新版本,此时老版本新版本在内存中共存。
$ kill -USR2 88457
$ ps -ef | grep nginx
root 88457 1 0 15:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 88458 88457 0 15:34 ? 00:00:00 nginx: worker process
root 92158 88457 0 16:03 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 92159 92158 0 16:03 ? 00:00:00 nginx: worker process
root 92161 74104 0 16:03 pts/1 00:00:00 grep --color=auto nginx
停止老版本进程
老版本的master进程接收到WINCH信号后,会停止老版本的worker进程,但是老版本的master进程并不会停止。
$ kill -WINCH 88457
$ ps -ef | grep nginx
root 88457 1 0 15:34 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 92158 88457 0 16:03 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 92159 92158 0 16:03 ? 00:00:00 nginx: worker process
root 95958 74104 0 16:41 pts/1 00:00:00 grep --color=auto nginx
此时,刷新浏览器,ningx版本已从1.16.1升级为1.18.0,留下老版本的master进程方便回滚,如生产环境一切正常,即可停止老版本进程。
$ kill -QUIT 88457
如果生产环境出错,向老版本master进程发送HUP信号生成worker进程,再向新版本master进程发送QUIT信号,优雅的停止即可。
$ kill -HUP 88457
$ kill -QUIT 92158
第二种方式
编译完成新版本,替换老版本文件后,在新版本的编译目录中执行
$ make upgrade
此种方式相当于执行了编译目录中Makefile文件的一段脚本
cat Makefile
default: build
clean:
rm -rf Makefile objs
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
#执行了下面的脚本
upgrade:
/usr/local/nginx/sbin/nginx -t
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
需要注意事项
1.备份
2.yum源安装的nginx路径跟编译安装指定安装路径不一样,需要注意
$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
学习来自:朱老师博客-Nginx短篇(10)
今天的学习是为了以后的工作更加的轻松!