》》
》》
在服务器运行时,如果我们要升级Nginx的版本,是不是要把Nginx 服务停掉呢,但是停掉服务,对生产环境老说损失挺大的,我们要学习一下平滑升级。
Nginx 平滑升级
可以在不间断服务器的前提表进行版本的升级,叫作平滑升级(热升级)
热数据: 经常描写缓存层的数据,一直在被改写的数据
热升级前提:
1)确保有更新版本的Nginx服务器
2)确保有nginx旧版本
3)确保Nginx在运行中
平滑升级
- 解压更高版本包
[root@localhost ~]# tar -zxf nginx-1.11.1.tar.gz -C /usr/src/
- 编译配置
[root@localhost ~]# cd /usr/src/nginx-1.11.1/
[root@localhost nginx-1.11.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
- 直接make 不需要make install
[root@localhost nginx-1.11.1]# make
- 将原有的nginx启动脚本移动至其他位置
[root@localhost nginx-1.11.1]# mv /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx_old
- NGinx源码目录中 objs/nginx启动脚本复制到指定位置(
[root@localhost nginx-1.11.1]# cp /usr/src/nginx-1.11.1/objs/nginx /usr/local/nginx/sbin/
- 查看logs下nginx.pid 文件后 将原有的pid文件的进程干掉
[root@localhost nginx-1.11.1]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid
[root@localhost nginx-1.11.1]# cat /usr/local/nginx/logs/nginx.pid
5526
[root@localhost nginx-1.11.1]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
[root@localhost nginx-1.11.1]# cat /usr/local/nginx/logs/nginx.pid
21411
- 实现pid无缝升级
[root@localhost nginx-1.11.1]# nginx -v
nginx version: nginx/1.11.1
编写nginx启动脚本的目的
1)将nginx放入/etc/init.d/下,可以将nginx服务添加至服务项中,使用systemctl进行管理
2)方便nginx的使用,将nginx进行更直观的操作,start | stop | restart
# $0 表示为第一个参数
# $1 表示为第二个参数
[root@localhost ~]# vim /etc/init.d/nginx
#! /bin/bash
#welcome to user my nginx
#chkconfig: 2345 55 23
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "nginx start>>>>>>"
;;
stop)
kill -s QUIT $( cat $PIDF)
echo " nginx closed>>>>>>>>"
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUHP $( cat $PIDF)
echo "nginx already reload"
;;
*)
echo "please usage $0 { start | stop | restart | reload }"
exit 1
esac
exit 0
- 添加至系统服务并赋予 脚本执行权限
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chmod 755 /etc/init.d/nginx
- 启动服务
[root@localhost ~]# /etc/init.d/nginx start
nginx start>>>>>>
[root@localhost ~]# netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21747/nginx: master
- 关闭服务
[root@localhost ~]# /etc/init.d/nginx stop
nginx closed>>>>>>>>
[root@localhost ~]# netstat -anpt | grep 80