`Laravel`版`小丑路人社区`改版中,与`Hyperf版小丑路人社区`数据互动,此版本改版中……尚未彻底完结!

Q:

平滑升级Nginx

平滑升级Nginx版本为1.30.0

  • 以下执行的目录位于home

1.升级先备份,随时回滚

  • 全量备份tar -zcvf /home/nginx-backup-$(date +%F).tar.gz /usr/local/nginx/
  • 备份二进制文件:sudo cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

2.查看老版本的配置

  • cd /usr/local/nginx/sbin && ./nginx -V
  • 复制打印出来的configure arguments配置信息

    --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1a --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc
    • 移除 --with-openssl=../openssl-1.1.1a,加载http_ssl_module模块即可

3.1 依赖

  • pcre
    • wget https://twds.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz
    • tar -zxvf pcre-8.42.tar.gz

3.2 下载nginx并编译新版本

  • 下载版本v1.30.0wget https://nginx.org/download/nginx-1.30.0.tar.gz
  • 解压:tar -zxvf nginx-1.30.0.tar.gz
  • 进入目录:cd nginx-1.30.0
  • 配置编译选项:./configure {上述的configure arguments配置信息}

    ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc --with-pcre=../pcre-8.42
    
    make

    会输出< man/nginx.8 > objs/nginx.8

  • 千万不要 make install,避免直接覆盖现有 Nginx!!!

3.3 查看 Nginx 运行中的进程

  • ps -ef | grep nginx
    root     10945     1  0 4月17 ?       00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    work     10946 10945  0 4月17 ?       00:00:02 nginx: worker process
    work     10947 10945  0 4月17 ?       00:00:01 nginx: worker process
    work     10948 10945  0 4月17 ?       00:00:06 nginx: worker process
    work     10950 10945  0 4月17 ?       00:02:40 nginx: worker process
    work     10951 10945  0 4月17 ?       00:04:13 nginx: worker process
    work     10952 10945  0 4月17 ?       00:01:43 nginx: worker process
    work     10953 10945  0 4月17 ?       00:00:28 nginx: worker process
    work     10954 10945  0 4月17 ?       00:00:24 nginx: worker process
    root     19010 26133  0 16:51 pts/0    00:00:00 grep --color nginx

4.1 替换二进制文件(当前处于nginx 1.30.0目录)

  • cp -rf objs/nginx /usr/local/nginx/sbin/

4.2 检测nginx服务是否正常

  • /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /xxx/nginx.conf syntax is ok
    nginx: configuration file /xxx/nginx.conf test is successful

4.2 向 master 进程发送 USR2 信号,Nginx 会启动一个新版本的 master 进程和相对应的 worker 进程,和旧版本的 master 进程一起处理请求。

  • 发送 USR2 信号给旧版本的主进程,让它启动一个新的主进程,并加载新版本的二进制文件
    • sudo kill -USR2 $(cat /var/run/nginx.pid)
      • 根据自己环境找到nginx.pid的目录

4.3 查询是否存在两个主进程:一个旧版本的主进程和一个新版本的主进程

  • ps aux | grep nginx
    • 会存在两组00:00:00 nginx: master process ./sbin/nginx -c conf/nginx.conf

4.4 向旧版本的 master 进程发送 WINCH 信号,并逐步关闭自己的工作进程(master 进程不退出),这时所有的用户请求都会由新版本的 master 进程处理。

  • sudo kill -WINCH $(cat /var/run/nginx.pid.oldbin)
    • 根据自己环境找到nginx.pid的目录

4.5 如果需要回退版本继续使用旧版本的 Nginx,可向旧版本的 master 进程发送 HUP 信号,它会重新启动工作进程,而且仍使用旧版配置文件,再使用信号(QUIT、TERM 或 KILL)将新版本的 Nginx 进程杀死。

4.6 确认新版本运行正常时,可继续进行平滑升级的操作,则可以对旧版本的 master 进程发送信号(QUIT、TERM 或 KILL),使旧版本的 master 进程退出

  • sudo kill -QUIT $(cat /var/run/nginx.pid.oldbin)
    • 根据自己环境找到nginx.pid的目录

5.验证Nginx服务版本信息

  • nginx -V
    • nginx version: nginx/1.30.0
Linux
订阅

评论记录


评论/回复