Nginx 从 1.20 升级到1.26 的完整记录

由于nginx1.20扫描出了安全漏洞,需要升级到1.26,另外openssl也因为版本较低进行了升级。在此记录一下整个过程。


准备工作

下载和解压 Nginx 源码

wget http://nginx.org/download/nginx-1.26.0.tar.gz
tar -zxvf nginx-1.26.0.tar.gz

作用: 下载 Nginx 最新版本的源码包,并解压以准备后续的编译工作。

备份现有的 Nginx 安装

cp /usr/sbin/nginx /usr/sbin/nginx.bak.1.20
cp -r /usr/lib64/nginx/modules /usr/lib64/nginx/modules.bak.1.20
cp -r /etc/nginx /etc/nginx.bak.1.20

作用: 在升级前,备份当前的 Nginx 可执行文件、模块文件和配置文件,以便在升级失败时可以快速回滚。


检查现有版本信息

nginx -V

作用: 查看当前 Nginx 的编译参数,确保后续编译新版本时能够保持一致性。


配置 Nginx 编译参数

cd nginx-1.26.0
./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 \
    --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 \
    --with-google_perftools_module \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_degradation_module \
    --with-http_flv_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_image_filter_module=dynamic \
    --with-http_mp4_module \
    --with-http_perl_module=dynamic \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v2_module \
    --with-http_xslt_module=dynamic \
    --with-mail=dynamic \
    --with-mail_ssl_module \
    --with-pcre \
    --with-pcre-jit \
    --with-stream=dynamic \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-threads \
    --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' \
    --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

作用: 根据当前 Nginx 的配置参数,对新版本进行配置,以确保功能一致。


升级 OpenSSL

备份旧版本的 OpenSSL

cp /usr/bin/openssl /usr/bin/openssl_old
cp -r /usr/lib64/openssl /usr/lib64/openssl_old

作用: 在升级前备份旧版本的 OpenSSL,防止版本兼容性问题导致不可用。

下载和解压 OpenSSL 源码

wget --no-check-certificate https://www.openssl.org/source/openssl-3.0.13.tar.gz
tar -zxvf openssl-3.0.13.tar.gz

作用: 获取最新版本的 OpenSSL。

编译安装新版本 OpenSSL

cd openssl-3.0.13
./config --prefix=/usr/local/openssl
make && make install

作用: 编译并安装 OpenSSL 新版本。

替换系统默认的 OpenSSL

rm /usr/bin/openssl
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
echo "/usr/local/openssl/lib64/" >> /etc/ld.so.conf
ldconfig -v

作用: 替换系统中默认的 OpenSSL 版本,并更新库路径以确保新版本生效。

验证新版本 OpenSSL

openssl version

作用: 确认 OpenSSL 的版本是否已经更新成功。


编译并安装新版本 Nginx

安装必要依赖

yum install pcre pcre-devel perl-core  pcre2-devel zlib-devel libxslt-devel  gd-devel libwebp-devel  gd-devel libwebp-devel  gperftools-devel -y

修改nginx-1.26.0/auto/lib/openssl/conf配置

找到以下配置项,从

CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"

改成

CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib64/libcrypto.a"

作用: 新版本的 OpenSSL发生了变化,所以更新一下配置中的路径,确保链接到正确的OpenSSL库。

使用 OpenSSL 路径重新配置 Nginx

cd ../nginx-1.26.0
./configure --with-openssl=/usr/local/openssl ... # 其余参数与上一次相同

作用: 将编译参数中的 OpenSSL 路径指定为新版本的 OpenSSL。

编译和替换 Nginx

make
cp -f objs/nginx /usr/sbin/nginx
cp objs/*.so /usr/lib64/nginx/modules/

作用: 编译生成新的 Nginx 可执行文件和模块文件,并替换旧版本。


验证新版本

测试 Nginx 配置

nginx -t

作用: 验证 Nginx 配置文件是否正确,避免配置问题导致启动失败。

启动 Nginx

nginx -s quit && nginx

作用: 停止正在运行的旧版本 Nginx,启动新版本。

验证版本信息

nginx -V

作用: 确认 Nginx 是否已经成功升级到新版本,并验证编译参数是否正确。


总结

通过以上步骤,成功将 Nginx 从 1.20 升级到 1.26,并完成了 OpenSSL 的升级。这里由于流量不大,没有进行平滑升级,只是优雅退出并立即重启。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值