Nginx1.12升级到Nginx1.15稳定版本
- 环境说明
服务器目前正在运行Nginx1.12版本,现在想将当前运行的Nginx服务的版本进行升级到最新的稳定版,(从1.12升级到1.15,版本的跨度不要太大,容易造成服务的崩溃),且在服务不停止的前提下经行升级。
1. 在不停掉老进程的情况下,启动新进程。
2. 老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
3. 新进程接受新请求。
4. 老进程处理完所有请求,关闭所有连接后,停止。
实现步骤:
1、获取nginx的安装目录
[root@localhost ~]# rpm -ql nginx | grep sbin
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx
- 如果安装时,有指定安装位置,可使用以下命令:
[root@localhost ~]# ps -ef | grep nginx | grep master | awk -F" " '{print $11}'
/usr/local/nginx/sbin/nginx
2、查看升级之前的nginx版本,同时获取旧nginx版本的编译参数
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V //注意是大写V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments:
如果安装时,有指定模块条件等,会出现如下信息:
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module
3、到官网下载1.15.8版本的nginx (下载地址:http://nginx.org/en/download.html)
[root@localhost ~]# wget http://nginx.org/download/nginx-1.15.8.tar.gz
4、解压下载好的文件,并进入文件夹进行编译(只编译不安装)
[root@localhost ~]# tar xf nginx-1.15.8.tar.gz
[root@localhost ~]# cd nginx-1.15.8
[root@localhost ~]# ./configure
[root@localhost nginx-1.15.8]# make
如果安装时,有指定条件等,再重新编译时,需按照我们上述获取的信息来指定使用
[root@localhost ~]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module
[root@localhost nginx-1.15.8]# make
5、备份旧版本的nginx的执行程序
[root@localhost nginx-1.15.8]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx12.old
[root@localhost nginx-1.15.8]# ls /usr/local/nginx/sbin/
nginx12.old
6、替换旧的Nginx的执行程序
[root@localhost nginx-1.15.8]# cp objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.15.8]# ls /usr/local/nginx/sbin/
nginx nginx12.old
7、发送USR2信号给旧版本主进程号,使nginx的旧版本停止接收请求,用nginx新版本接替,且老进程处理完所有请求,关闭所有连接后,停止
[root@localhost nginx-1.15.8]# ls /usr/local/nginx/logs/
access.log error.log nginx.pid shunjie.access.log
[root@localhost nginx-1.15.8]# cd /usr/local/nginx/logs/
[root@localhost logs]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` //此为反撇号或者分开打,先查询pid号,再中断
8、查看nginx pid目录,多了个nginx.pid.oldbin文件,存放了旧版本nginx的pid号
[root@localhost logs]# ls
access.log error.log nginx.pid nginx.pid.oldbin
#查看服务运行状态
[root@localhost logs]# netstat -aupt | grep nginx
tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN 15823/nginx: master
9、从容关闭旧进程
[root@localhost logs]# kill -QUIT `cat nginx.pid.oldbin`
[root@localhost logs]# ls
access.log error.log nginx.pid shunjie.access.log
10、查看升级后的版本
[root@localhost logs]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.15.8
平滑升级完毕!
附:如果是yum安装的nginx,升级就很简单了,直接yum update nginx即可安装到仓库有的最新版,不过通常库更新的比较慢。
rpm包安装的话,就直接rpm -Uvh nginx-1.16.0.rpm即可