CentOS实现Nginx1.10.3平滑升级Nginx1.11.10(热部署或热加载)

CentOS实现Nginx1.10.3平滑升级Nginx1.11.10(热部署或热加载)
如果要对当前的Nginx服务器进行版本升级,应用新模块,如果用户访问量比较大的时候,如果需要在不影响客户的情况下进行升级的话,这时候就得考虑平滑升级了。
平缓停止Nginx服务
平缓重启Nginx服务

创建用户目录和赋予权限
id -u www >/dev/null 2>&1
[ $? -ne 0 ] && useradd -M -s /sbin/nologin www;
mkdir -p /usr/local/nginx && chown www.www -R /usr/local/nginx;

yum -y install gcc make screen python wget git pcre-devel zlib-devel openssl-devel

CentOS源码编译安装Nginx1.10.3
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz;cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module \
--with-http_secure_link_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module
make
make install

CentOS源码编译Nginx1.11.10
wget http://nginx.org/download/nginx-1.11.10.tar.gz
tar -zxvf nginx-1.11.10.tar.gz;cd nginx-1.11.10
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-http_stub_status_module \
--with-http_secure_link_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module
make

1、重命名低版本的nginx
# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old

2、然后拷贝一份新编译的二进制文件:
[root@www.zhangfangzhou.cn nginx-1.11.10]# cp objs/nginx /usr/local/nginx/sbin/

3、在源码目录执行make upgrade开始升级,因为nginx.pid的位置,造成自动升级会报错误
[root@www.zhangfangzhou.cn nginx-1.11.10] # make upgrade

[root@www.zhangfangzhou.cn nginx-1.11.10]# cat Makefile

default: build

clean:
rm -rf Makefile objs

build:
$(MAKE) -f objs/Makefile

install:
$(MAKE) -f objs/Makefile install

modules:
$(MAKE) -f objs/Makefile modules

upgrade:
/usr/local/nginx/sbin/nginx -t

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin

kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

#下面是手动升级
[root@www.zhangfangzhou.cn nginx-1.11.10]# kill -USR2 `cat /var/run/nginx.pid;`
[root@www.zhangfangzhou.cn nginx-1.11.10]# sleep 1
[root@www.zhangfangzhou.cn nginx-1.11.10]# test -f /usr/local/nginx/logs/nginx.pid.oldbin
[root@www.zhangfangzhou.cn nginx-1.11.10]# kill -QUIT `cat /var/run/nginx.pid.oldbin`

平缓升级Nginx服务:使用新版本的Nginx文件启动服务,之后平缓停止原有的Nginx进程
# kill -USR2 `cat /nginx/logs/nginx.pid`

平缓停止Nginx服务:平缓停止是指允许Nginx服务将当前正在处理的网络请求处理完成,但不再接受新的请求,之后关闭连接,停止工作
# kill -QUIT `cat /nginx/logs/nginx.pid`

平缓重启Nginx服务:Nginx服务进程接受到信号后,首先读取新的Nginx配置文件,如果配置语法正确,则启动新的Nginx服务,然后平缓关闭旧的服务进程,如果新的Nginx配置文件有问题,将显示错误,仍然使用旧的Nginx进程提供服务
# kill -HUP `cat /nginx/logs/nginx.pid`

评论已关闭。