centos

怎样升级centos的nginx版本

小樊
43
2025-08-14 17:15:44
栏目: 智能运维

要升级 CentOS 上的 Nginx 版本,你可以按照以下步骤操作:

  1. 首先,备份你的 Nginx 配置文件和网站数据。这是一个非常重要的步骤,因为在升级过程中可能会出现问题,备份可以确保你的数据安全。
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
sudo cp -r /usr/share/nginx/html /usr/share/nginx/html.backup
  1. 移除当前的 Nginx 版本:
sudo yum remove nginx
  1. 安装 EPEL (Extra Packages for Enterprise Linux) 仓库,因为 Nginx 的最新版本可能不在 CentOS 的默认仓库中:
sudo yum install epel-release
  1. 安装 Nginx 的源码包。首先,安装编译 Nginx 所需的依赖包:
sudo yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  1. 下载 Nginx 的最新源码包。访问 Nginx 官方网站 查找最新版本,然后使用 wget 命令下载:
wget http://nginx.org/download/nginx-1.xx.x.tar.gz

1.xx.x 替换为你想安装的 Nginx 版本。

  1. 解压源码包并进入目录:
tar -zxvf nginx-1.xx.x.tar.gz
cd nginx-1.xx.x
  1. 配置编译选项。这里是一个示例配置,你可以根据需要进行调整:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v3_module
  1. 编译并安装 Nginx:
make
sudo make install
  1. 创建一个新的 systemd 服务文件:
sudo vi /etc/systemd/system/nginx.service

将以下内容粘贴到文件中,然后保存并退出:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target
  1. 重新加载 systemd 配置并启动 Nginx:
sudo systemctl daemon-reload
sudo systemctl start nginx
  1. 设置 Nginx 开机自启:
sudo systemctl enable nginx
  1. 检查 Nginx 版本以确认升级成功:
nginx -V

现在,你已经成功升级了 CentOS 上的 Nginx 版本。请确保测试你的网站以确保一切正常运行。

0
看了该问题的人还看了