平滑升级 nginx1.20.1 到最新版本 1.25.4
平滑升级,即不中断请求的情况下完成 nginx 版本的升级
由于我的系统 openssl 版本为 1.0.2k-fips,已不符合安全要求,需要下载较新的版本,并且编译 nginx 时指定 –with-openss 选项。如果你的系统 openssl 版本较新,则可忽略有关 –with-openss 的操作,编译时不需指定 –with-openssl ,会默认使用系统的 openssl。
下载官方nginx包和openssl包
nginx-1.25.4.tar.gzhttps://nginx.org/en/download.html
openssl-1.1.1w.tar.gzhttps://www.openssl.org/source/old/1.1.1/index.html
备份配置文件(conf)
当前 nginx 是 yum 安装的,配置文件的位置是 /etc/
[root@VM-3-78-centos etc]# cp -r /etc/nginx /etc/nginx.backup
[root@VM-3-78-centos etc]# ls | grep nginx
nginx
nginx.backup
保存原信息
nginx -V
用记事本保存,一会儿升级要用到
[root@VM-3-78-centos etc]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio ...(省略)
编译
将开头下载的包上传到 /opt/
,解压
[root@VM-3-78-centos opt]# pwd
/opt
[root@VM-3-78-centos opt]# ls
nginx-1.25.4 nginx-1.25.4.tar.gz openssl-1.1.1w openssl-1.1.1w.tar.gz
进入 nginx-1.25.4
[root@VM-3-78-centos opt]# cd nginx-1.25.4/
[root@VM-3-78-centos nginx-1.25.4]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
初始化,该步是沿用当前 nginx 的配置信息,复制之前保存的 configure 参数,如需指定 openssl 则后面加上 --with-openssl=/opt/openssl-1.1.1w
(刚才解压的 openssl-1.1.1w 文件夹路径)
命令行执行:
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy ..(略) --with-openssl=/opt/openssl-1.1.1w
可能会发生错误,要么解决错误,要么把错误对应的参数项去掉,后重新执行。
上步执行成功后,命令行执行:(不要 make install)
make
该步是编译,生成的绿色文件名为 nginx 的可执行性文件在 /opt/nginx-1.25.4/objs/
下,
[root@VM-3-78-centos objs]# ls
autoconf.err nginx.8 ngx_mail_module_modules.c ngx_modules.c ngx_stream_module_modules.o
Makefile ngx_auto_config.h ngx_mail_module_modules.o ngx_modules.o ngx_stream_module.so
nginx ngx_auto_headers.h ngx_mail_module.so ngx_stream_module_modules.c src
平滑升级
打开新的终端,找到当前 nginx 的执行目录,里面是当前版本的 nginx 执行文件,将它备份(不会影响 nginx 的运行)
[root@VM-3-78-centos etc]# which nginx
/usr/sbin/nginx
[root@VM-3-78-centos etc]# cd /usr/sbin/nginx
[root@VM-3-78-centos sbin]# ls | grep nginx
nginx
[root@VM-3-78-centos sbin]# mv nginx nginx.old
[root@VM-3-78-centos sbin]# ls | grep nginx
nginx.old
将编译后的 nginx 可执行文件复制到该目录
[root@VM-3-78-centos objs]# cp /opt/nginx-1.25.4/objs/nginx /usr/sbin/
[root@VM-3-78-centos sbin]# ls | grep nginx
nginx
nginx.old
进入 /opt/nginx-1.25.4
准备执行平滑升级命令,在此之前,最好确认下 pid 文件是否正常,否则会升级失败
[root@VM-3-78-centos etc]# pstree -apn | grep nginx
| | | `-grep,25072 --color=auto nginx
|-nginx,15233
| |-nginx,1130
| `-nginx,1131
[root@VM-3-78-centos etc]# cat /run/nginx.pid
15233
正常,在 /opt/nginx-1.25.4
下执行 make upgrade
[root@VM-3-78-centos nginx-1.25.4]# pwd
/opt/nginx-1.25.4
[root@VM-3-78-centos nginx-1.25.4]# make upgrade
/usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
kill -USR2 `cat /run/nginx.pid`
sleep 1
test -f /run/nginx.pid.oldbin
kill -QUIT `cat /run/nginx.pid.oldbin`
可能会发生错误:execve() failed while executing new binary process “nginx” (2: No such file or directory) ,搜索网上得来的答案大多是 “命令执行过程中是识别不到设置的环境变量,所以找不到指定的新的可执行文件。”,原因是启动 nginx 时不是绝对路径启动,尝试各种方法我没有解决这个问题,只能先 kill <旧PID>,然后启动 nginx。(systemctl restart nginx 不行)
没有错误,检查一下网站是否正常,nginx 版本是否升级成功
[root@VM-3-78-centos nginx-1.25.4]# nginx -V
nginx version: nginx/1.25.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1w 11 Sep 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx ..
[root@VM-3-78-centos nginx-1.25.4]# pstree -apn | grep nginx
| | | `-grep,26222 --color=auto nginx
|-nginx,25166
| |-nginx,25167
| `-nginx,25168
[root@VM-3-78-centos logs]# cat /run/nginx.pid
25166
查看 http 响应头
[root@VM-3-78-centos nginx-1.25.4]# curl -I http://127.0.0.1
HTTP/1.1 404 Not Found
Server: nginx/1.25.4
Date: Fri, 01 Mar 2024 07:35:40 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
查看 nginx 的 error.log
等
end
本作品采用《CC 协议》,转载必须注明作者和本文链接
详细且实用 :+1: