热部署就是常说的在线手升级,不影响线上的服务升级,nginx热部署前提是你已经安装好了nginx,已经在运行了。有的是nginx需要添加新的模块,有的是nginx版本需要升级。
添加新的模块,或者升级版本,要参考以前编译的模块,如果不添加,那么以前的模块就不能使用了。
添加模块(添加echo模块)
(1)使用-V查看编译参数
[root@www src]# /usr/local/nginx/sbin/nginx -V --查看nginx版本详细信息并且拿到之前的编译参数,这一步是必须要做的,因为这些配置参数全部都需要重新利用,如果不重新利用,那么之前的模块也就没有了
nginx version: JFWS/3.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx
(2)将之前的环境清理,然后重新编译
[root@www nginx-1.16.1]# pwd
/usr/src/nginx-1.16.1
[root@www nginx-1.16.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@www nginx-1.16.1]# make clean --来到之前源码目录下面,清理之前环境
rm -rf Makefile objs
(3)添加新的模块
[root@www src]# ls --需要添加的echo模块的包准备好
debug echo-nginx-module-0.61.tar.gz kernels nginx-1.16.1 nginx-1.16.1.tar.gz
[root@www src]# tar -xvf echo-nginx-module-0.61.tar.gz --解压展开包
[root@www nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
--add-module=../echo-nginx-module-0.61 --添加新的模块
记住,以前把编译参数要重新加上去,就是使用-V选项看到的那些参数,我这只有
--prefix=/usr/local/nginx这个参数,实际生产环境有很多参数,都需要加上去,模块后面可以跟相对路径也可以跟着绝对路径+模块包名
adding module in ../echo-nginx-module-0.61
+ ngx_http_echo_module was configured
(4)使用make命令将新模块编译到二进制文件里
[root@www nginx-1.16.1]# make build --make将echo模块编译到二进制文件里面
make[1]: Leaving directory `/usr/src/nginx-1.16.1'
(5)make之后要不要执行make install ???? 怕覆盖配置文件或者html发布目录????
下面是在make install之前,查看一下nginx的一些信息,看make install之后会不会覆盖原来的配置文件或者覆盖html目录呢??????
[root@www nginx-1.16.1]# head -2 /usr/local/nginx/conf/nginx.conf --这里先看一下还没有make install之前的nginx的配置文件内容
user nginx;
worker_processes auto;
[root@www nginx-1.16.1]# ls /usr/local/nginx/html/ --再来看一下还没有make install之前的发布目录下面的文件,404.html是自己创建的文件
html 50x.html index.html metalink.xml updateinfo.xml
[root@www nginx-1.16.1]# make install --现在开始安装
make[1]: Leaving directory `/usr/src/nginx-1.16.1'
[root@www nginx-1.16.1]# head -2 /usr/local/nginx/conf/nginx.conf --可以看到make install之后,发布目录和配置文件都没有被覆盖
user nginx;
worker_processes auto;
[root@www nginx-1.16.1]# ls /usr/local/nginx/html/
html 50x.html index.html metalink.xml updateinfo.xml
make install干的事情就是来测试安装目录有没有,如果目录没有就会帮你创建一个目录,如果目录存在了,就不会创建目录。同样如果有的文件已经存在比如nginx的配置文件存在了就不会去覆盖其配置文件,同理发布目录也一样.
总结就是make install不会覆盖之前的nginx的nginx.conf配置文件和html发布目录的,make install只是将之前的二进制文件备份了,然后产生了新的nginx文件,所以可以执行make install命令
[root@www nginx-1.16.1]# ll /usr/local/nginx/sbin/
total 7848
-rwxr-xr-x 1 root root 4207360 Apr 23 22:45 nginx --加了echo模块新的二进制文件
-rwxr-xr-x 1 root root 3825496 Apr 23 21:00 nginx.old --老的二进制文件,只是备份了一下
[root@www nginx-1.16.1]# ls objs/ --这个nginx是make之后产生的新二进制文件,即加了echo模块的二进制文件
addon autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@www nginx-1.16.1]# ll /usr/local/nginx/sbin/ total 7848 --这个nginx是从上面的/objs目录下面拷贝过去的,即最新的添加echo模块的二进制文件
nginx.old是没有添加echo模块老的二进制文件
-rwxr-xr-x 1 root root 4207360 Mar 9 20:34 nginx
-rwxr-xr-x 1 root root 3825496 Mar 9 19:53 nginx.old
[root@www nginx-1.16.1]# ps -ef | grep nginx --升级之前查看一下nginx进程pid
root 14875 1 0 07:19 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 22419 14875 0 11:16 ? 00:00:00 nginx: worker process
(6)make install之后的make upgrade进行升级,这样产生的新的worker进程会使用新的nginx二进制文件
[root@www nginx-1.16.1]# 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`
[root@www nginx-1.16.1]# ps -ef | grep nginx --可以看到master和worker进程全部被替换了
root 37551 1 0 14:49 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 37553 37551 0 14:49 ? 00:00:00 nginx: worker process
[root@www nginx-1.16.1]# /usr/local/nginx/sbin/nginx -V --可以看到新的模块已经添加了
nginx version: JFWS/3.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx --add-module=../echo-nginx-module-0.61
这样添加模块的方法很简单,但是有弊端,弊端在于将老的master和worker进程给杀掉了
如果需要回退 将下面两个文件名互换nginx改为nginx.old nginx.old改为nginx
[root@www nginx-1.16.1]# ll /usr/local/nginx/sbin/
total 7848
-rwxr-xr-x 1 root root 4207360 Mar 9 20:34 nginx
-rwxr-xr-x 1 root root 3825496 Mar 9 19:53 nginx.old