Nginx不停机版本升级
  lxSY9k0Slakj 2023年11月19日 32 0

Nginx版本升级

查看当前版本

[root@react.com sbin]# ./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=app01 --group=app01 --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-threads --with-stream --with-stream_ssl_module
[root@react.com sbin]#

准备新版本

nginx-1.24.0.tar.gz

解压缩

tar zxvf nginx-1.22.1.tar.gz

设置configure

[root@react.com liqing]# cd nginx-1.24.0/
[root@react.com nginx-1.24.0]#./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=app01 \
--group=app01 \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module

编译

[root@react.com nginx-1.24.0]# make

升级过程,只需要编译,不需要安装,否则会覆盖已经安装的版本

查看新编译的版本

[root@react.com nginx-1.24.0]# cd objs
[root@react.com objs]# ll
total 9300
-rw-r--r-- 1 root root   20934 Nov 15 14:38 autoconf.err
-rw-r--r-- 1 root root   61809 Nov 15 14:38 Makefile
-rwxr-xr-x 1 root root 9297848 Nov 15 14:41 nginx
-rw-r--r-- 1 root root    5500 Nov 15 14:41 nginx.8
-rw-r--r-- 1 root root    9069 Nov 15 14:38 ngx_auto_config.h
-rw-r--r-- 1 root root     657 Nov 15 14:38 ngx_auto_headers.h
-rw-r--r-- 1 root root   10770 Nov 15 14:38 ngx_modules.c
-rw-r--r-- 1 root root   96112 Nov 15 14:41 ngx_modules.o
drwxr-xr-x 9 root root      91 Nov 15 14:38 src

可以看到,该目录下也存在一个nginx文件,运行nginx -v查看

[root@react.com objs]# ./nginx -v
nginx version: nginx/1.24.0

备份原nginx可执行文件

如果后续升级失败,还可以进行版本回滚

[root@react.com objs]# cd /usr/sbin
[root@react.com sbin]# mv nginx nginx.bak

复制新的nginx可执行文件进行替换

[root@react.com sbin]# cd -
/opt/liqing/nginx-1.24.0/objs
[root@react.com objs]# ll
total 9300
-rw-r--r-- 1 root root   20934 Nov 15 14:38 autoconf.err
-rw-r--r-- 1 root root   61809 Nov 15 14:38 Makefile
-rwxr-xr-x 1 root root 9297848 Nov 15 14:41 nginx
-rw-r--r-- 1 root root    5500 Nov 15 14:41 nginx.8
-rw-r--r-- 1 root root    9069 Nov 15 14:38 ngx_auto_config.h
-rw-r--r-- 1 root root     657 Nov 15 14:38 ngx_auto_headers.h
-rw-r--r-- 1 root root   10770 Nov 15 14:38 ngx_modules.c
-rw-r--r-- 1 root root   96112 Nov 15 14:41 ngx_modules.o
drwxr-xr-x 9 root root      91 Nov 15 14:38 src
[root@react.com objs]# cp nginx /usr/sbin

查看当前nginx进程

[root@react.com objs]# ps -ef|grep nginx
root      5312 26914  0 14:51 pts/2    00:00:00 grep --color=auto nginx
root     30195     1  0 13:31 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
app01    30196 30195  0 13:31 ?        00:00:00 nginx: worker process

执行升级命令

[root@react.com objs]# kill -s USR2 `more /var/run/nginx.pid`

再次查看nginx进程,可以看到创建了新的master进程和worker进程,与此同时,也是将旧版本的nginx的pid保存到nginx.pid.oldbin中。

[root@react.com objs]# ps -ef|grep nginx
root      5444 30195  0 14:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
app01     5445  5444  0 14:53 ?        00:00:00 nginx: worker process
root      5447 26914  0 14:53 pts/2    00:00:00 grep --color=auto nginx
root     30195     1  0 13:31 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
app01    30196 30195  0 13:31 ?        00:00:00 nginx: worker process

我们可以查看一下这个文件,确实,其记录了旧版本的master进程pid

[root@react.com objs]# cat /var/run/nginx.pid.oldbin
30195

此时,所有的worker进程(包括新的和旧的)继续接收请求,我们可以向旧版本的master进程发送WINCH命令以关闭其worker进程。

[root@react.com objs]# kill -s winch `cat /var/run/nginx.pid.oldbin`

再次查看nginx进程,旧版本的worker进程已经优雅退出,只剩下master进程。

[root@react.com objs]# ps -ef|grep nginx
root      5444 30195  0 14:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
app01     5445  5444  0 14:53 ?        00:00:00 nginx: worker process
root      5699 26914  0 14:59 pts/2    00:00:00 grep --color=auto nginx
root     30195     1  0 13:31 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

如果新版本的worker进程运行正常,那么我们就可以向旧版本的master进程发送QUIT命令优雅退出。

[root@react.com objs]# kill -s quit `cat /var/run/nginx.pid.oldbin`
[root@react.com objs]# ps -ef|grep nginx
root      5444     1  0 14:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
app01     5445  5444  0 14:53 ?        00:00:00 nginx: worker process
root      5868 26914  0 15:01 pts/2    00:00:00 grep --color=auto nginx

查看当前版本

[root@react.com ~]# nginx -v
nginx version: nginx/1.24.0
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
lxSY9k0Slakj