
需求:升级nginx到最新版本
目前最新版本是:nginx-1.30.8
https://nginx.org/en/download.html
1、查看当前nginx版本和安装模块信息
/data/server/nginx/sbin/nginx -V
#安装模块信息如下,后面要用到
configure arguments: --prefix=/data/server/nginx --user=autoops --group=autoops --without-http_memcached_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_sub_module --http-client-body-temp-path=/data/server/nginx/client --http-proxy-temp-path=/data/server/nginx/proxy --http-fastcgi-temp-path=/data/server/nginx/fcgi --http-uwsgi-temp-path=/data/server/nginx/uwsgi --with-openssl=/data/server/nginx/packages/openssl-3.6.1 --with-zlib=/data/server/nginx/packages/zlib-1.3.2 --with-pcre=/data/server/nginx/packages/pcre2-10.47
2、使用新版本安装包进行配置configure和编译make(切记不执行安装操作 make install)
cd /data/server/nginx/packages
tar zxvf nginx-1.30.8.tar.gz
cd nginx-1.30.8
./configure --prefix=/data/server/nginx --user=autoops --group=autoops --without-http_memcached_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_sub_module --http-client-body-temp-path=/data/server/nginx/client --http-proxy-temp-path=/data/server/nginx/proxy --http-fastcgi-temp-path=/data/server/nginx/fcgi --http-uwsgi-temp-path=/data/server/nginx/uwsgi --with-openssl=/data/server/nginx/packages/openssl-3.6.1 --with-zlib=/data/server/nginx/packages/zlib-1.3.2 --with-pcre=/data/server/nginx/packages/pcre2-10.47
make -j$(nproc) #无限制并发,自动用CPU核心数
make #单线程编译,不加并发
make -j4 #4个并发任务编译(最常用,适配4核CPU)
#如果编译报错,需要安装依赖包,部分服务器安全基线优化后会卸载gcc、tar等依赖包
sudo yum install make gcc gcc-c++ perl zlib-devel glibc-devel glibc-static tar automake ghostscript
sudo yum reinstall gcc #重新安装gcc
sudo yum reinstall gcc-c++ #重新安装gcc-c++
sudo yum remove gcc
sudo yum install gcc
sudo ln -s /usr/bin/automake-1.16 /usr/bin/automake-1.15
sudo ln -s /usr/bin/automake-1.16 /usr/bin/aclocal-1.15
sudo ln -s /usr/bin/automake /usr/bin/automake-1.15
3、备份旧版二进制文件,用新版替换旧版
#备份旧文件
mv /data/server/nginx/sbin/nginx /data/server/nginx/sbin/nginx.old
#使用新文件替换旧文件
cp /data/server/nginx/packages/nginx-1.30.8/objs/nginx /data/server/nginx/sbin/nginx
4、检查nginx配置文件是否正常运行
/data/server/nginx/sbin/nginx -t
cd /data/server/nginx/
sh nginx.sh restart
5、查找nginx主进程,向master进程发送USER2信号,使新旧进程同时存在
ps -ef|grep nginx
autoops 12621 1 0 Jun22 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
autoops 12622 12621 0 Jun22 ? 00:18:42 nginx: worker process
autoops 12623 12621 0 Jun22 ? 00:00:34 nginx: cache manager process
autoops 2422521 2376476 0 10:40 pts/0 00:00:00 grep --color=auto nginx
#执行下面的操作,向master进程发送USER2信号
kill -USR2 12621
说明:
#旧版nginx的master进程将/data/server/nginx/logs/nginx.pid重命名为/data/server/nginx/logs/nginx.pid.oldbin
#新版nginx启动新的master进程,重新生成新的nginx.pid文件,此时新的nginx进程和旧的nginx进程同时存在,之前旧的请求继续在旧版本运行,新的请求在新版本运行
ps -ef|grep nginx
autoops 12621 1 0 Jun22 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
autoops 12622 12621 0 Jun22 ? 00:18:42 nginx: worker process
autoops 12623 12621 0 Jun22 ? 00:00:34 nginx: cache manager process
autoops 2424165 12621 0 10:43 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
autoops 2424171 2424165 0 10:43 ? 00:00:00 nginx: worker process
autoops 2424172 2424165 0 10:43 ? 00:00:00 nginx: cache manager process
autoops 2424173 2424165 0 10:43 ? 00:00:00 nginx: cache loader process
autoops 2424266 2376476 0 10:43 pts/0 00:00:00 grep --color=auto nginx
检查业务、监控、日志是否正常
tail /data/server/nginx/logs/error.log
6、平滑升级和回退操作
6.1平滑升级
#通知旧 master,让它的 worker 进程优雅退出,不再接收新连接
#旧 master 进程本身不会退出,会一直保留,等待存量连接全部跑完
#旧 master 还持有 nginx.pid.oldbin,保留回退能力
#不会杀掉 master,只有 kill -QUIT 才会让旧 master 正常退出
#WINCH 不会退出 master,master 会一直存在,直到 QUIT
kill -WINCH 12621
ps -ef|grep nginx
autoops 12621 1 0 Jun22 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
autoops 2424165 12621 0 10:43 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
autoops 2424171 2424165 0 10:43 ? 00:00:00 nginx: worker process
autoops 2424172 2424165 0 10:43 ? 00:00:00 nginx: cache manager process
autoops 2425387 2376476 0 10:45 pts/0 00:00:00 grep --color=auto nginx
#再观察一段时间(一般几分钟~半小时),业务无异常,再优雅关闭旧 master
#退出旧版master进程,旧版文件/data/server/nginx/logs/nginx.pid.oldbin自动删除
kill -QUIT 12621
ps -ef|grep nginx
autoops 2424165 1 0 10:43 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
autoops 2424171 2424165 0 10:43 ? 00:00:00 nginx: worker process
autoops 2424172 2424165 0 10:43 ? 00:00:00 nginx: cache manager process
autoops 2428493 2376476 0 10:47 pts/0 00:00:00 grep --color=auto nginx
#查看版本,nginx已经平滑升级到新版本
/data/server/nginx/sbin/nginx -V
6.2回退到旧版本
#如果新版本有问题,同样可以平滑回退到旧版本
ps -ef|grep nginx
root 80841 1 0 05:37 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process
root 80850 80841 0 05:38 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80851 80850 0 05:38 ? 00:00:00 nginx: worker process
root 80856 7675 0 05:39 pts/0 00:00:00 grep --color=auto nginx
#查看旧版本的/data/server/nginx/logs/nginx.pid.oldbin
cat /data/server/nginx/logs/nginx.pid.oldbin
cat /data/server/nginx/logs/nginx.pid.oldbin
80841
#启动旧版worker进程
kill -s HUP 80841
ps -ef|grep nginx
root 80841 1 0 05:37 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process
root 80850 80841 0 05:38 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80851 80850 0 05:38 ? 00:00:00 nginx: worker process
www 80858 80841 0 05:40 ? 00:00:00 nginx: worker process
root 80860 7675 0 05:40 pts/0 00:00:00 grep --color=auto nginx
#查看新版本的/data/server/nginx/logs/nginx.pid
cat /data/server/nginx/logs/nginx.pid
cat /data/server/nginx/logs/nginx.pid
80850
#关闭新版本worker进程
kill -WINCH 80850
ps -ef|grep nginx
root 80841 1 0 05:37 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process
root 80850 80841 0 05:38 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80858 80841 0 05:40 ? 00:00:00 nginx: worker process
root 80863 7675 0 05:41 pts/0 00:00:00 grep --color=auto nginx
#关闭新版master进程,旧版文件/data/server/nginx/logs/nginx.pid.oldbin自动覆盖新版文件nginx.pid
kill -QUIT 80850
ps -ef|grep nginx
root 80841 1 0 05:37 ? 00:00:00 nginx: master process /data/server/nginx/sbin/nginx
www 80842 80841 0 05:37 ? 00:00:00 nginx: worker process
www 80858 80841 0 05:40 ? 00:00:00 nginx: worker process
root 80866 7675 0 05:41 pts/0 00:00:00 grep --color=auto nginx
#删除新版本二进制文件
rm -rf /data/server/nginx/sbin/nginx
#恢复旧版本二进制文件
mv /data/server/nginx/sbin/nginx.old /data/server/nginx/sbin/nginx
#查看版本,nginx已经平滑回退到旧版本
/data/server/nginx/sbin/nginx -V
至此,Linux下平滑升级nginx版本与回退完成。

②190706903
③203744115


