【Linux基础服务教程】Nginx平滑升级

第一期教程Nginx的安装教程

一、查看旧Nginx的参数

[root@master ~]# nginx -V
nginx version: nginx/1.23.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-http_secure_link_module --with-threads --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/

注意--prefix后面的所有参数,我们升级的时候都要带上!

二、平滑升级新Nginx操作

1.以相同的配置,配置新版本Nginx

下载新版本Nginx

[root@master ~]# wget http://nginx.org/download/nginx-1.24.0.tar.gz

解压新版本Nginx

[root@master ~]# tar xf nginx-1.24.0.tar.gz 
[root@master ~]# cd nginx-1.24.0/
[root@master nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

编译新版本

请勿执行make install

[root@master nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-http_secure_link_module --with-threads --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/

[root@master nginx-1.24.0]# make

会在当前目录下objs目录下生成新版本

[root@master nginx-1.24.0]# ls objs/
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@master nginx-1.24.0]# objs/nginx -v
nginx version: nginx/1.24.0

备份旧版本,拷贝新版本

[root@master nginx-1.24.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@master nginx-1.24.0]# cp objs/nginx /usr/local/nginx/sbin/
[root@master nginx-1.24.0]# ls /usr/local/nginx/sbin/
nginx  nginx.bak

2.启动新版本

[root@master nginx-1.24.0]# kill -USR2 $(cat /usr/local/nginx/logs/nginx.pid)
[root@master nginx-1.24.0]# ps -aux | grep nginx
root       7724  0.0  0.1  46016  3312 ?        S    14:58   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      7725  0.0  0.1  46480  2148 ?        S    14:58   0:00 nginx: worker process
root      14194  0.0  0.1  46032  3344 ?        S    18:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     14195  0.0  0.1  46496  1900 ?        S    18:54   0:00 nginx: worker process
root      14197  0.0  0.0 112824   988 pts/0    R+   18:54   0:00 grep --color=auto nginx
[root@master nginx-1.24.0]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid  nginx.pid.oldbin	#可以发现多了一个pid文件nginx.pid.oldbin存着旧版本的pid
[root@master nginx-1.24.0]# cat /usr/local/nginx/logs/nginx.pid
14194	#新版本的进程ID
[root@master nginx-1.24.0]# cat /usr/local/nginx/logs/nginx.pid.oldbin 
7724	#旧版本的进程ID

关闭旧的工作进程,并且查看仅剩旧版本主进程未关闭

[root@master nginx-1.24.0]# kill -WINCH $(cat /usr/local/nginx/logs/nginx.pid.oldbin)
[root@master nginx-1.24.0]# ps -aux | grep nginx
root       7724  0.0  0.1  46016  3312 ?        S    14:58   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root      14194  0.0  0.1  46032  3344 ?        S    18:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     14195  0.0  0.1  46496  1900 ?        S    18:54   0:00 nginx: worker process
root      14215  0.0  0.0 112824   988 pts/0    R+   18:58   0:00 grep --color=auto nginx

关闭旧版本主进程

[root@master nginx-1.24.0]# kill 7724
[root@master nginx-1.24.0]# ps -aux | grep nginx
root      14194  0.0  0.1  46032  3344 ?        S    18:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     14195  0.0  0.1  46496  1900 ?        S    18:54   0:00 nginx: worker process
root      14218  0.0  0.0 112824   984 pts/0    R+   19:00   0:00 grep --color=auto nginx

清理旧版本

[root@master nginx-1.24.0]# rm -rf /usr/local/nginx/sbin/nginx.bak

查看升级后的版本信息和配置信息

[root@master nginx-1.24.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-file-aio --with-http_secure_link_module --with-threads --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ --http-scgi-temp-path=/var/tmp/nginx/scgi/