Linux升级nginx版本

前言

项目中经常会遇到因为漏洞修补需要升级nginx版本,当无法直接使用yum升级软件版本时就需要自行下载代码编译升级版本了,这里记录一次我的升级过程,以供以后参考

操作步骤

1.下载最新稳定版nginx,目前是1.30.5

访问官方网站 https://nginx.org/en/download.html ,选择下载Stable version。

2.上传压缩包至服务器并解压

将下载的压缩包上传至服务器后使用命令tar -zxvf nginx-1.30.5.tar.gz解压到当前目录

3.备份当前nginx

找到当前nginx的执行文件,我的安装路径是/usr/local/nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.backup

4.查看当前版本的nginx配置参数

/usr/local/nginx/sbin/nginx -V

image

5.使用查询出的参数configure arguments来配置新版本nginx

进入前面解压出来的nginx目录然后执行./configure 参数列表
image

这里提供一个相对通用的配置,可以按需使用

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-stream --with-stream_ssl_module --with-file-aio

6.执行编译

make

image

7.验证编译结果

./objs/nginx -t
./objs/nginx -V

image

8.停止当前nginx服务,并且拷贝新版本

systemctl stop nginx

或者直接找到当前nginx服务路径后直接停止

/usr/local/nginx/sbin/nginx -s stop

当服务停止后执行复制命令覆盖当前版本的nginx执行文件

cp ./objs/nginx /usr/local/nginx/sbin/

image

9.验证版本并启动服务

启动服务

systemctl start nginx

如果没有安装服务可以参考后面的注册nginx服务进行安装

验证当前执行文件版本

/usr/local/nginx/sbin/nginx -V

image

注册nginx服务

cd /etc/systemd/system/
touch nginx.service
vim nginx.service

编辑文件内容如下

[Unit]
Description=nginx service
After=nginx.service

[Service]
Type=forking
User=nginx
Group=nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
Restart=on-failure
RestartSec=5s
PrivateTmp=true

[Install]
WantedBy=multi-user.target

加载服务

systemctl daemon-reload

设置开机启动

systemctl enable nginx.service

参考资料

posted @ 2026-09-18 16:17  安培昌浩  阅读(34)  评论(0)    收藏  举报