nginx之热部署,以及版本回滚

热部署的概念:当从老版本替换为新版本的nginx的时候,如果不热部署的话,会需要取消nginx服务并重启服务才能替换成功,这样的话会使正在访问的用户在断开连接,所以为了不影响用户的体验,且需要版本升级,就需要热部署来升级版本
版本回滚的概念:当新版本上线之后出现问题,需要回到老版本,这时候就需要做版本回滚,其实就是在你做版本升级的时候,将老版本备份以下,然后替换新版本,之后杀死新版本的进程便可以
实验步骤:1:先从官网上拷贝nginx-1.14.2版本的链接,且需要源码部署
2:解压在/opt/目录下
3:创建nginx的进程 useradd -u 998 -s /sbin/nologin nginx
4:cd /opt/nginx-1.14.2进行预编译和编译安装,编译的话需要先安装devlopment或者安装gcc,gcc-c++,yum -y groupinstall devlopment或者yum -y gcc gcc-c++前面时间需要久一点,后面快一写
5:预编译的命令是$./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
$make
$make install
6:用echo $?检查有没有安装成功,成功的话则在下面设置控制命令nginxctl
7:$vi /usr/bin/nginxctl
#!/usr/bin/env bash
case $1 in
stop)
/usr/local/nginx/sbin/nginx -s quit
;;
start)
/usr/local/nginx/sbin/nginx
;;
reload)
/usr/local/nginx/sbin/nginx -s reload
;; *)
echo "please input (start|stop|reload)"
;;
esac

~ 8:chmod a+x /usr/bin/nginxctl #设置文件的执行权限
9:nginxctl start #启动nginx服务
10:这时候从浏览器上输入ip地址,便会显示nginx访问首页
11:现在开始进行热部署,将版本升级到nginx-16.0.1,从官网上拷贝下载链接
12:跟之前安装一样,不过到make之后就不一样了,不需要make install
13:[root@web3 nginx-1.16.1]# mv /usr/local/nginx/sbin/{nginx,nginx.old}#备份一下nginx-14.0.2版本,防止版本需要回退
[root@web3 nginx-1.16.1]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@web3 nginx-1.16.1]# cd objs #新版本的nginx命令在objs里
nginx ngx_auto_config.h ngx_modules.c
nginx.8 ngx_auto_headers.h ngx_modules.o
[root@web3 nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/nginx #需要将新版本的nginx命令替换掉原先版本的
[root@web3 nginx-1.16.1]# ps aux|grep nginx #此时查看nginx进程,看到有nginx进程在运行,还是老版本的进程
root 51891 0.0 0.0 20544 600 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 51893 0.0 0.1 20988 1560 ? S 21:49 0:00 nginx: worker process
root 66744 0.0 0.0 112708 984 pts/0 R+ 22:06 0:00 grep --color=auto nginx
[root@web3 nginx-1.16.1]# kill -USR2 51891 #优雅的关闭老版本进程,热部署主要就是依靠这条命令,正在访问的用户依然访问老版本,新用户访问则会访问到新版本的nginx
[root@web3 nginx-1.16.1]# ps aux |grep nginx #这时候看进程发现有4个进程,新老版本都有
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 51893 0.0 0.1 20988 1560 ? S 21:49 0:00 nginx: worker process
root 67798 0.1 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1328 ? S 22:07 0:00 nginx: worker process
root 68006 0.0 0.0 112708 984 pts/0 R+ 22:07 0:00 grep --color=auto nginx
[root@web3 nginx-1.16.1]# kill -WINCH 51891 #这条命令的意思是让老版本不要去检测新用户访问的响应
[root@web3 nginx-1.16.1]# ps aux|grep nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1328 ? S 22:07 0:00 nginx: worker process
root 69592 0.0 0.0 112708 980 pts/0 R+ 22:09 0:00 grep --color=auto nginx
[root@web3 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -v #这时候检测版本就是1.16.1
nginx version: nginx/1.16.1
2版本回滚:
版本回滚和之前差不多,只是有一个问题需要注意,因为新版本不行,所以直接用kill命令就好,如果再需要升级版本,再进行热部署一遍即可
[root@web3 nginx-1.16.1]# vi /usr/bin/nginxctl
[root@web3 nginx-1.16.1]# mv /usr/local/nginx/sbin/{nginx,nginx.old-1.16.1}
[root@web3 nginx-1.16.1]# ps aux |grep nginx
root 21161 0.0 0.0 112708 984 pts/0 R+ 23:37 0:00 grep --color=auto nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1572 ? S 22:07 0:00 nginx: worker process
[root@web3 nginx-1.16.1]# mv /usr/local/nginx/sbin/{nginx.old,nginx}
[root@web3 nginx-1.16.1]# ps aux|grep nginx
root 22307 0.0 0.0 112708 984 pts/0 R+ 23:39 0:00 grep --color=auto nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1572 ? S 22:07 0:00 nginx: worker process
[root@web3 nginx-1.16.1]# kill -USR2 51891
[root@web3 nginx-1.16.1]# kill -9 67799
[root@web3 nginx-1.16.1]# ps aux|grep nginx
nginx 23560 1.1 0.1 21004 1328 ? S 23:40 0:00 nginx: worker process
root 23638 0.0 0.0 112708 984 pts/0 R+ 23:41 0:00 grep --color=auto nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@web3 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2