nginx平滑升级(热部署)
nginx的平滑升级:可以升级nginx的版本,也可以对当前版本进行加模块。我们需要在nginx编译环境的基础上实现,如果没有实现nginx源码编译,请访问https://blog.csdn.net/SarahYYP/article/details/102522020进行源码编译nginx,在本实验中,我们将不再编译。
实验环境:
操作系统:CentOS 7
服务器:192.168.184.137
nginx版本:nginx-1.16.1
nginx的平滑升级步骤如下:
1、上传并解压nginx的更高或相同版本,并进行编译
[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@localhost ~]# tar xf nginx-1.16.1.tar.gz
[root@localhost ~]# cd nginx-1.16.1
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V #-V 表示查看模块
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module #再添加三个模块
2、并编译 — make (在此不编译安装,即不执行make install)
[root@localhost ~]# make
3、将在/usr/local/nginx/sbin/下的nginx文件剪切走,并进入新版本的nginx的objs目录下
将目录下的nginx拷贝到 /usr/local/nginx/sbin/下
[root@localhost nginx-1.16.1]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx nginx.old
[root@localhost sbin]# mv nginx /tmp/
[root@localhost sbin]# cd /root/nginx-1.16.1/objs/
[root@localhost objs]# cp -r nginx /usr/local/nginx/sbin/
4、查看原有的nginx进程号,发送热升级命令(USR2),再查看升级后的nginx的模块及版本
[root@localhost objs]# ps aux |grep nginx
root 15012 0.0 0.0 20600 1408 ? Ss 02:27 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 17633 0.0 0.0 21028 1600 ? S 02:40 0:00 nginx: worker process
root 21125 0.0 0.0 112712 960 pts/0 R+ 05:17 0:00 grep --color=auto nginx
[root@localhost objs]# kill -USR2 15012 #-USR2是热升级信号,数值是查到的nginx的master的PID
[root@localhost objs]# ps aux |grep nginx #可以查看到有两个master
root 15012 0.0 0.0 20600 1408 ? Ss 02:27 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 17633 0.0 0.0 21028 1600 ? S 02:40 0:00 nginx: worker process
root 21127 0.0 0.0 20556 1584 ? S 05:18 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21128 0.0 0.0 21008 1324 ? S 05:18 0:00 nginx: worker process
root 21130 0.0 0.0 112712 964 pts/0 R+ 05:18 0:00 grep --color=auto nginx
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V #再次查看模块,多了添加的三个,即升级成功
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
此时nginx平滑升级完成。