概述

本文记录在 CentOS 7.9 上,把源码安装的 Nginx 1.25.3 升级为 1.30.5,并改用 systemd 管理的完整过程。整个升级只有停老进程到启动新进程之间的几秒钟中断,老版本目录全程保留,可以随时回退。

为什么用源码编译:CentOS 7 已于 2024 年 6 月停止维护,nginx 官方仓库也不再为 EL7 提供新版 RPM。1.30.5 是 1.30 稳定分支在 2026 年 9 月发布的安全更新版本。系统自带的 OpenSSL 是 1.0.2k,不支持 TLS 1.3,因此改为把 OpenSSL 3.5 LTS 静态编译进 nginx,不替换系统的 OpenSSL。

升级前的环境

项目 升级前 升级后
版本 1.25.3 1.30.5(OpenSSL 3.5.9)
安装目录 /usr/local/nginx-1.25.3 /usr/local/nginx
主配置 /usr/local/nginx-1.25.3/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
站点配置 /etc/nginx/conf.d/*.conf 不变
启动方式 手动执行 / rc.local systemd(开机自启)
worker 用户 nobody nobody(显式配置)

整体步骤

  1. 修复 yum 源,安装编译依赖
  2. 下载 nginx 与 OpenSSL 源码
  3. 编译安装到新目录 /usr/local/nginx
  4. 把老版本的配置迁移到新目录并检查
  5. 创建 systemd 服务,停掉老进程,启动新版本
  6. 清理老的自启动方式,验证

一、准备工作

以下命令均以 root 执行。

1. 修复 yum 源

CentOS 7 停止维护后,默认的 mirror.centos.org 已失效,需要改为 vault 归档源:

sed -i 's/^mirrorlist=/#mirrorlist=/g; s|^#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*.repo
yum clean all && yum makecache

国内服务器可以把 vault.centos.org 换成 mirrors.aliyun.com/centos-vault。

2. 安装编译依赖

yum install -y gcc gcc-c++ make pcre-devel zlib-devel wget
yum install -y perl perl-core

perl-core 不能省略:OpenSSL 3.5 的构建脚本依赖 Time::Piece、IPC::Cmd 等 Perl 模块,CentOS 7 默认没有安装,缺少时会在 make 阶段报错(见文末常见问题)。

3. 确认现有版本和运行状态

ps -ef | grep nginx | grep -v grep
ls -l /proc/$(pgrep -o nginx)/exe       # 正在运行的 nginx 二进制位置
which nginx && ls -l $(which nginx)      # nginx 命令指向哪里

本次环境中,运行的二进制是 /usr/local/nginx-1.25.3/sbin/nginx,master 进程以 root 运行,worker 进程用户为 nobody。

二、下载源码

使用 OpenSSL 3.5 LTS 系列的最新版 3.5.9(2026-09-29 发布,支持到 2030 年 4 月)。下载前请先到 OpenSSL 下载页 确认 3.5 系列的最新版本号。

cd /usr/local/src
wget https://nginx.org/download/nginx-1.30.5.tar.gz
curl -LO https://github.com/openssl/openssl/releases/download/openssl-3.5.9/openssl-3.5.9.tar.gz
tar zxf nginx-1.30.5.tar.gz
tar zxf openssl-3.5.9.tar.gz

OpenSSL 下载不下来时

OpenSSL 官网的下载链接本身就指向 GitHub Releases,换成官网地址无济于事。可以按顺序尝试:

  1. 用 curl -LO 代替 wget。CentOS 7 自带的 wget 较旧,处理 GitHub 的跳转时容易失败。
  2. 使用国内镜像,先打开目录确认有对应版本:
    • https://repo.huaweicloud.com/openssl/source/
    • https://mirrors.cloud.tencent.com/openssl/source/
  3. 在能访问外网的电脑上下载,再上传到服务器:scp openssl-3.5.9.tar.gz root@服务器IP:/usr/local/src/

校验文件完整性

sha256sum openssl-3.5.9.tar.gz

把输出与官方下载页上同名的 .sha256 文件内容对比,二者必须一致。

三、编译安装

新版本安装到 /usr/local/nginx,与老版本目录 /usr/local/nginx-1.25.3 互不影响。此步骤不会中断线上服务。

1. 创建运行用户(编译时 --user 指定的默认用户,后面会在配置中改回 nobody,但先建好无妨)

useradd -r -s /sbin/nologin nginx

2. 配置

cd /usr/local/src/nginx-1.30.5
./configure \
  --prefix=/usr/local/nginx \
  --user=nginx --group=nginx \
  --with-openssl=/usr/local/src/openssl-3.5.9 \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_v3_module \
  --with-http_realip_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module \
  --with-stream --with-stream_ssl_module \
  --with-threads --with-file-aio

--with-openssl 填的是 OpenSSL 源码解压目录,不是安装目录。如果老版本还编译了其他模块,可以执行 /usr/local/nginx-1.25.3/sbin/nginx -V 查看老版本的编译参数,把需要的模块补上。

configure 结束时的摘要里应能看到 using OpenSSL library: /usr/local/src/openssl-3.5.9。

3. 编译并安装

make -j$(nproc)
make install

make 会先编译 OpenSSL,需要几分钟。确认最后没有 Error 再执行 make install。如果 make 中途失败,修复问题后直接重新 make 即可,不要执行 make clean,它会删除 objs/Makefile,需要重新 configure。

4. 验证

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

输出中应包含 nginx version: nginx/1.30.5 和 built with OpenSSL 3.5.9。

四、迁移配置

新目录里只有默认配置,需要把老版本的配置迁移过来。关键是检查相对路径:nginx 按安装目录解析相对路径,换目录后会去 /usr/local/nginx/ 下查找。

1. 检查主配置和站点配置中的路径

# 主配置
grep -rn 'nginx-1.25.3' /usr/local/nginx-1.25.3/conf/
grep -rnE '^\s*(root|include|ssl_certificate|ssl_certificate_key|pid|access_log|error_log|user)\b' /usr/local/nginx-1.25.3/conf/

# 站点配置
grep -rnE '^\s*(root|alias|include|ssl_certificate|ssl_certificate_key|access_log|error_log)\b' /etc/nginx/conf.d/
grep -rn 'nginx-1.25.3' /etc/nginx/conf.d/

本次环境的检查结论:

配置项 写法 处理
pid logs/nginx.pid(相对路径) 新路径为/usr/local/nginx/logs/nginx.pid,与 systemd 服务文件一致,无需修改
include mime.types 相对路径 随 conf 目录一起拷贝
主配置中的证书 /etc/web/ssl/...(绝对路径) 无需修改
站点配置 include /etc/nginx/conf.d/*.conf; 继续沿用,无需修改
站点的 root / alias / 证书 / 日志 全部为绝对路径,无引用老目录 无需修改
主日志 logs/access.log、logs/error.log 以后写入新目录,老日志留在原处
user 未配置(老版本以 nobody 运行) 新增user nobody;,保持一致

只有 .conf 结尾的站点文件会被加载,.bak、.back 等备份文件不受影响。

2. 拷贝配置

mv /usr/local/nginx/conf /usr/local/nginx/conf.default.bak
cp -a /usr/local/nginx-1.25.3/conf /usr/local/nginx/conf
cp -a /usr/local/nginx-1.25.3/html/. /usr/local/nginx/html/

3. 保持运行用户为 nobody

老配置没有 user 指令,老版本以默认的 nobody 运行;新版本编译时的默认用户是 nginx。为避免网站目录权限不足导致 403,显式指定为 nobody:

sed -i '1i user nobody;' /usr/local/nginx/conf/nginx.conf

4. 用新版本检查配置

/usr/local/nginx/sbin/nginx -t

务必使用完整路径。确认输出显示的是 /usr/local/nginx/conf/nginx.conf,并且 test is successful。

五、切换到 systemd

make install 不会自动生成 systemd 服务,需要手动创建。本节的第 4 步会有几秒钟服务中断,建议在业务低峰时执行。

1. 创建服务文件

cat > /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

注意两点:

  • PIDFile 必须与 nginx.conf 中 pid 的实际路径一致。
  • 不要加 PrivateTmp=true。本环境有多个站点把日志写在 /tmp 下,还有站点用 root /tmp/; 直接提供 /tmp 中的文件。开启后 nginx 会使用隔离的 /tmp,导致日志看不到、文件返回 404。

2. 把 nginx 命令指向新版本

which nginx
ls -l $(which nginx)
ln -sf /usr/local/nginx/sbin/nginx $(which nginx)    # 仅当上一步显示它是软链接时执行
nginx -v                                            # 应显示 nginx/1.30.5

如果 nginx 命令是通过 PATH 加入的,就修改 /etc/profile 或 ~/.bash_profile 中对应的路径。

3. 停止老版本

/usr/local/nginx-1.25.3/sbin/nginx -s quit
sleep 2
ps -ef | grep nginx | grep -v grep        # 确认已没有 nginx 进程

如果进程停不掉,可以用 kill -QUIT <master 进程 PID>。

4. 启动新版本并设置开机自启

systemctl enable --now nginx
systemctl status nginx

5. 验证

ls -l /proc/$(pgrep -o nginx)/exe           # 应指向 /usr/local/nginx/sbin/nginx
ps -ef | grep nginx | grep -v grep          # worker 用户应为 nobody
curl -I http://127.0.0.1                    # 响应头 Server: nginx/1.30.5
curl -kI https://你的域名                    # 逐个抽查 HTTPS 站点

systemctl status 应显示 active (running),并且服务状态为 enabled。

六、收尾

1. 清理老的自启动方式

老版本是通过 rc.local 开机启动的。如果保留,开机时它会和 systemd 抢着启动 nginx:rc.local 先启动时,nginx 不受 systemd 管理,systemd 随后会因端口被占用而启动失败。

cat /etc/rc.d/rc.local
crontab -l

本环境 rc.local 中有一行 /usr/local/nginx/sbin/nginx,将其注释掉:

cp /etc/rc.d/rc.local /etc/rc.d/rc.local.bak
sed -i 's|^\s*/usr/local/nginx/sbin/nginx\s*$|# &   (已改由 systemd 管理)|' /etc/rc.d/rc.local
tail -3 /etc/rc.d/rc.local

crontab 中如有 @reboot 启动 nginx 的任务,也一并删除。

2. 重启验证(可选,建议在维护窗口进行)

reboot
# 重启后
systemctl status nginx

确认 nginx 已随系统自动启动,状态为 active (running)。

3. 处理老版本目录

/usr/local/nginx-1.25.3 建议保留一段时间,作为回退手段。为避免误改老配置,可以先改名,确认新版本运行稳定后再删除:

mv /usr/local/nginx-1.25.3 /usr/local/nginx-1.25.3.old

改名后,下一节回退命令中的路径要相应调整。

七、日常管理与回退

配置文件位置

  • 全局配置(运行用户、日志格式、默认证书等):/usr/local/nginx/conf/nginx.conf
  • 站点配置:仍然在 /etc/nginx/conf.d/*.conf,新增或修改站点都在这里
  • /usr/local/nginx-1.25.3/conf/ 已不再生效,不要在那里修改

常用命令

nginx -t && systemctl reload nginx    # 修改配置后检查并重载
systemctl restart nginx
systemctl stop nginx
systemctl status nginx
journalctl -u nginx -n 50             # 查看启动失败原因

以后统一用 systemctl 启停,不要再直接执行 nginx 或 nginx -s stop。这样启动的进程不归 systemd 管理,会导致状态不一致。

回退到 1.25.3

新版本出现问题时,可以立即切回老版本:

systemctl stop nginx
/usr/local/nginx-1.25.3/sbin/nginx
ps -ef | grep nginx | grep -v grep

回退后,老版本不受 systemd 管理。排查完问题、准备再次升级时,先用 /usr/local/nginx-1.25.3/sbin/nginx -s quit 停掉它,再执行 systemctl start nginx。

以后再升级小版本

安装目录已经固定为 /usr/local/nginx,后续升级(例如 1.30.x)更简单:用相同的 configure 参数编译后,只执行 make,不要执行 make install,手动替换二进制更可控,也方便保留备份。然后替换二进制并重启:

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -t && systemctl restart nginx

八、常见问题

现象 原因 解决
make 时报Can't locate Time/Piece.pm in @INC OpenSSL 3.5 构建依赖的 Perl 模块缺失 yum install -y perl-Time-Piece(或 perl-core),然后在 nginx 源码目录直接重新 make,无需重新 configure
GitHub 上的 OpenSSL 源码下载失败 网络限制,或旧版 wget 处理跳转失败 改用curl -LO、国内镜像,或本地下载后 scp 上传;OpenSSL 官网链接同样指向 GitHub
nginx -t 检查的是老配置 nginx 命令仍指向老版本 用完整路径/usr/local/nginx/sbin/nginx -t,或按第五节修改软链接
systemctl start nginx 失败,提示端口被占用 老进程未停止,或被 rc.local 抢先启动 停掉所有 nginx 进程后再用 systemctl 启动,并注释 rc.local 中的启动命令
/tmp 下的日志不更新,或从 /tmp 提供的文件返回 404 服务文件开启了PrivateTmp=true 删除该行,执行systemctl daemon-reload 和 systemctl restart nginx
升级后站点返回 403 worker 用户从 nobody 变为 nginx,无权读取网站目录 在 nginx.conf 顶部加user nobody;,或给 nginx 用户授予目录读权限
systemctl status 显示失败,但 nginx 实际在运行 进程是手动启动的,或PIDFile 与配置中的 pid 路径不一致 手动停掉进程后改用 systemctl 启动;核对PIDFile 与 pid 指令