一、备份原配置文件

  • 备份原配置文件到/root/nginx

    cp -r /usr/local/nginx/conf /root/nginx

二、编译nginx1.24

  • 下载新版nginx

https://nginx.org/en/download.html

  • 上传并解压nginx-1.24.0.tar.gz安装包

    tar -zxvf nginx-1.24.0.tar.gz
  • 检查原nginx配置信息,新旧参数需保持一直

    root@redis:~# /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.16.1
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --add-module=../naxsi-0.56/naxsi_src/
  • 进入nginx-1.24.0目录执行参数配置,安装目录为/etc/nginx,且启用了naxsi模块,根据实际情况配置

    ./configure --prefix=/etc/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --add-module=/root/naxsi-0.56/naxsi_src
  • 编译

    make
  • 安装

    make install
  • 检查是否安装成功

    root@redis:~# /etc/nginx/sbin/nginx -V
    nginx version: nginx/1.24.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/etc/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --add-module=/root/naxsi-0.56/naxsi_src

三、替换旧配置

  • 替换nginx主配置文件(只替换主配置文件即可,然后什么都不更改,就可以通过1.24运行原来的配置了)

    cp /root/nginx/conf/nginx.conf /etc/nginx/conf/
  • 检查配置文件是否有错误

    root@redis:~#  /etc/nginx/sbin/nginx -t
    nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/conf/nginx.conf test is successful

    四、配置软链接并加入systemctl管理

  • 首先停止旧版nginx

    /usr/local/nginx/sbin/nginx -s stop
  • 配置软链接到系统变量

    ln -s /etc/nginx/sbin/nginx /usr/sbin/nginx
  • 配置新的nginx.conf文件,启用自定义PID

    root@redis:~#  vi /etc/nginx/conf/nginx.conf
    worker_processes  1;
    worker_rlimit_nofile 65535;
    pid /run/nginx.pid;     #创建这一行,启动nginx后会自动创建pid文件
  • 配置nginx.service文件(注意nginx二进制文件目录即可)

    root@redis:~#   vi /etc/systemd/system/nginx.service 
    [Unit]
    Description=A high performance web server and a reverse proxy server
    Documentation=man:nginx(8)
    After=network-online.target remote-fs.target nss-lookup.target
    Wants=network-online.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/sbin/nginx -t -q -g 'daemon on; master_process on;'
    ExecStart=/sbin/nginx -g 'daemon on; master_process on;'
    ExecReload=/sbin/nginx -g 'daemon on; master_process on;' -s reload
    ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
    TimeoutStopSec=5
    KillMode=mixed
    
    [Install]
    WantedBy=multi-user.target

    五、启动1.24版nginx

  • 启动新版nginx

    systemctl restart nginx
  • 查看nginx状态

    root@redis:~# systemctl status nginx
    ● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: disabled)
     Active: active (running) since 五 2023-12-29 10:21:50 CST; 36min ago
       Docs: man:nginx(8)
    Process: 54505 ExecStart=/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 54503 ExecStartPre=/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
     Main PID: 54508 (nginx)
      Tasks: 2
     Memory: 21.8M
     CGroup: /system.slice/nginx.service
             ├─54508 nginx: master process /sbin/nginx -g daemon on; master_process on;
             └─54509 nginx: worker process
    
    12月 29 10:21:50 host-172-22-14-90 systemd[1]: Starting A high performance web server and a reverse proxy server...
    12月 29 10:21:50 host-172-22-14-90 systemd[1]: Started A high performance web server and a reverse proxy server.