详细完整记录 一次 nginx 平滑升级

4,927 阅读3分钟

今天突然收到百度云邮件提醒,发现高危漏洞。吓得我赶紧登陆百度云查看,原来是nginx 的旧版本有漏洞,需要升级

问题是服务器上运行着很多服务,如何平滑的进行升级呢?接下来看我表演,哈哈

第一步 准备工作

1 查看当前的版本信息:

执行如下命令:/usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.1.10
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module

显示当前版本为1.1.10 ,最后面有configure arguments: 参数比较重要,接下来新版本需要用到,保证没有漏掉任何一个模块

2 下载要升级的版本

我下载的是1.14.2 版本

第二步 在源码目录下执行 ./configure +第一步看到的configure arguments 后面的参数

以下是我执行的内容

./configure --prefix=/usr/local/nginx \
--with-http_addition_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_dav_module --with-http_v2_module 

第三步 在源码目录下执行 make,切记不要make install

此时make编译完后会在安装目录下生成一个objs目录且在该目录下有一个nginx执行文件。

[root@xxxxx nginx-1.14.2]# make
make -f objs/Makefile
make[1]: Entering directory `/usr/local/download/nginx-1.14.2'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \
	src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_palloc.o \
	src/core/ngx_palloc.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_array.o \
	src/core/ngx_array.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_list.o \
	src/core/ngx_list.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_hash.o \
	src/core/ngx_hash.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_buf.o \
	src/core/ngx_buf.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \

第四步 备份原来老的nginx文件

主要是为了回退

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

第五步 将新编译的目录copy 到 原来的安装目录下

以下是我本机的操作命令

cp objs/nginx /usr/local/nginx/sbin/

第六步 在源码目录下使用make upgrade替换老的nginx进程

[root@xxx nginx-1.14.2]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
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`

第七步 执行/usr/local/nginx/sbin/nginx -V查看nginx最新的版本及编译的参数

[root@xxxx nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module

好了,大功告成

看完两件事

如果你觉得这篇内容对你挺有启发,我想邀请你帮我2个小忙:

  1. 点赞,让更多的人也能看到这篇内容(收藏不点赞,都是耍流氓 -_-)
  2. 关注公众号「面试bat」,不定期分享原创知识,原创不易,请多支持(里面还提供刷题小程序哦)。