在Mac电脑上,测试nginx的在线版本升级
因为mac系统的权限问题,做开发或者部署测试环境环境时,建议在当前用户的目录下,专门建立一个目录,专门用于存放开发环境。本例中目录为 /Users/weirongting/exp_lib, 如下为全部的操作过程命令。
cd /Users/weirongting/exp_lib
#下载两个版本的nginx,目标从1.10.3升级到1.15.8,并且保持服务不断开
wget http://nginx.org/download/nginx-1.10.3.tar.gz
wget http://nginx.org/download/nginx-1.15.8.tar.gz
#解压
tar zxvf nginx-1.10.3.tar.gz
tar zxvf nginx-1.15.8.tar.gz
#安装低版本
cd nginx-1.10.3/
./configure --prefix=/Users/weirongting/exp_lib/nginx
make && make install
#等待安装完成,然后修改配置文件
cd /Users/weirongting/exp_lib/nginx/conf
vi nginx
# 将#pid logs/nginx.pid; 修改为 pid logs/nginx.pid,保存退出
# 不修改 , 则pid会在默认安装目录,容易有权限问题
#启动nginx,
cd ../sbin
./nginx -t #检查配置文件
./nginx
ps -ef| grep nginx
MacBook-Pro:sbin weirongting$ ps -ef| grep nginx
501 69769 1 0 6:42上午 ?? 0:00.00 nginx: master process ./nginx
501 69770 69769 0 6:42上午 ?? 0:00.00 nginx: worker process
501 69771 69769 0 6:42上午 ?? 0:00.28 nginx: worker process
501 69772 69769 0 6:42上午 ?? 0:00.29 nginx: worker process
501 69773 69769 0 6:42上午 ?? 0:00.29 nginx: worker process
501 72508 69664 0 6:56上午 ttys001 0:00.00 grep nginx
#启动完成, 编译新版本
cd /Users/weirongting/exp_lib/nginx-1.15.8/
nginx -V #查看之前版本的编译参数,保存编译参数和之前低版本一致
./configure --prefix=/Users/weirongting/exp_lib/nginx
make #特别注意,我们是升级版本, 这一步就可以生成新版本的nginx可执行文件,不需要make install 安装
#备份旧版,复制新版本到旧版本目录
cp /Users/weirongting/exp_lib/nginx/sbin/nginx /Users/weirongting/exp_lib/nginx/sbin/nginx.old
cp objs/nginx /Users/weirongting/exp_lib/nginx/sbin/nginx
cd /Users/weirongting/exp_lib/nginx/sbin/nginx
#检查新版本
./nginx -t
nginx: the configuration file /Users/weirongting/exp_lib/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /Users/weirongting/exp_lib/nginx/conf/nginx.conf test is successful
#配置文件没有问题,继续执行指令,
kill -USR2 `cat /Users/weirongting/exp_lib/nginx/logs/nginx.pid`
ps -ef| grep nginx # 看到新版和旧版同时运行,同时旧版进程号备份为nginx.pid.oldbin
501 69769 1 0 6:42上午 ?? 0:00.00 nginx: master process ./nginx
501 69770 69769 0 6:42上午 ?? 0:00.00 nginx: worker process
501 69771 69769 0 6:42上午 ?? 0:00.31 nginx: worker process
501 69772 69769 0 6:42上午 ?? 0:00.31 nginx: worker process
501 69773 69769 0 6:42上午 ?? 0:00.31 nginx: worker process
501 72526 69769 0 6:57上午 ?? 0:00.01 nginx: master process ./nginx
501 72527 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72528 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72529 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72530 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72535 69664 0 6:57上午 ttys001 0:00.00 grep nginx
#向旧进程执行平缓关闭work进程
kill -WINCH `cat /Users/weirongting/exp_lib/nginx/logs/nginx.pid.oldbin`
ps -ef| grep nginx #查看进程,老进程仅仅保留master,这时可以检查新版本的工作情况,
#如果想回退,可以重新还原旧版的nginx可执行文件,然后执行
#kill -HUP `cat /Users/weirongting/exp_lib/nginx/logs/nginx.pid.oldbin`
#如果要结束老版本的master,执行 kill -QUIT `cat /Users/weirongting/exp_lib/nginx/logs/nginx.pid.oldbin`
501 69769 1 0 6:42上午 ?? 0:00.00 nginx: master process ./nginx
501 72526 69769 0 6:57上午 ?? 0:00.01 nginx: master process ./nginx
501 72527 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72528 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72529 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72530 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72564 69664 0 6:58上午 ttys001 0:00.00 grep nginx
#结束旧版
kill -QUIT `cat /Users/weirongting/exp_lib/nginx/logs/nginx.pid.oldbin`
#再看进程号
ps -ef| grep nginx
501 72526 1 0 6:57上午 ?? 0:00.01 nginx: master process ./nginx
501 72527 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72528 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72529 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72530 72526 0 6:57上午 ?? 0:00.00 nginx: worker process
501 72583 69664 0 6:59上午 ttys001 0:00.00 grep nginx
#此时,nginx 升级完成。