nginx服务的帮助文档:http://nginx.org和https://docs.nginx.com/
在企业中,nginx服务必须时时处于开启状态,即使想要升级更新,那么也必须是在线升级更新,这就是平滑升级,热部署的意思。
1、selinux和firewalld状态为disabled
2、各主机信息如下:
主机 | ip |
---|---|
server1 | 172.25.83.1 |
(1)下载nginx1.14对应的压缩包,并进行解压
[root@server1 ~]# ls
nginx-1.14.2.tar.gz
[root@server1 ~]# tar zxf nginx-1.14.2.tar.gz
[root@server1 ~]# ls
nginx-1.14.2 nginx-1.14.2.tar.gz
(2)下载安装nginx需要的依赖包
[root@server1 ~]# yum install gcc pcre-devel zlib-devel -y
(3)修改相应的文件,并源码编译nginx
[root@server1 ~]# cd nginx-1.14.2
[root@server1 nginx-1.14.2]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.14.2]# vim auto/cc/gcc #将172行注释掉
171 # debug
172 #CFLAGS="$CFLAGS -g"
[root@server1 nginx-1.14.2]# pwd
/root/nginx-1.14.2
[root@server1 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
[root@server1 nginx-1.14.2]# make && make install
(4)启动nginx服务,查看该nginx服务的版本号,并查看nginx服务相应的进程
[root@server1 ~]# /usr/local/nginx/sbin/nginx
[root@server1 ~]# /usr/local/nginx/sbin/nginx -v #查看nginx版本信息
nginx version: nginx/1.14.2
[root@server1 ~]# /usr/local/nginx/sbin/nginx -V #查查看nginx版本信息,编译版本,和配置参数
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@server1 ~]# ps aux | grep nginx
root 14186 0.0 0.0 20488 608 ? Ss 19:53 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 14187 0.0 0.1 20932 1332 ? S 19:53 0:00 nginx: worker process
root 14189 0.0 0.0 112648 956 pts/0 R+ 19:54 0:00 grep --color=auto nginx
我们可以看到有两个nginx进程,一个是master进程,另外一个是worker进程。其中master进程是主进程,用来派生worker程,worker进程是实际工作的进程。
这里只有一个worker进程,这是由nginx服务的主配置文件(/usr/local/nginx/conf/nginx.conf)决定的。
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf 3 worker_processes 1;
下面,我们修改nginx服务的主配置文件/usr/local/nginx/conf/nginx.conf文件,将worker的进程数,设置为自动(即由cpu的个数来决定,有几个cpu就会开启几个worker进程)
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf #将第三行原来的1该为auto
3 worker_processes auto;
[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload #修改完nginx配置文件之后,重载nginx服务
[root@server1 sbin]# ps -ef | grep nginx #利用-ef参数可以查看其父子关系
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 2074 2050 0 20:07 ? 00:00:00 nginx: worker process
nobody 2075 2050 0 20:07 ? 00:00:00 nginx: worker process
root 4676 2018 0 21:05 pts/0 00:00:00 grep --color=auto nginx
此时我们看到的worker数是两个。这是因为cpu有两个。
[root@server1 ~]# lscpu CPU(s): 2
下面我们进行配置,使得查看nginx服务的主配置文件(/uar/local/nginx/conf/nginx.conf)会有彩色呈现。
从上图,我们可以看到利用vim查看该配置文件,只有两种颜色出现(黑色和蓝色)。那么能不能进行相应的配置,使其利用vim查看时呈现多种颜色呢?我们进行下面的配置:
[root@server1 ~]# cp -r nginx-1.14.2/contrib/vim/* ~/.vim
[root@server1 ~]# ls ~/.vim
ftdetect ftplugin indent synta
从上图,我们可以看到呈现出多种颜色。表示我们配置成功。
(1)下载nginx1.15对应的压缩包,并进行解压
[root@server1 ~]# ll -d nginx-1.15.8.tar.gz
-rw-r--r-- 1 root root 1027862 Apr 23 20:23 nginx-1.15.8.tar.gz
[root@server1 ~]# tar zxf nginx-1.15.8.tar.gz
[root@server1 ~]# ll -d nginx-1.15.8
drwxr-xr-x 8 1001 1001 158 Dec 25 22:53 nginx-1.15.8
(2)下载安装nginx需要的依赖包——上面已经安装过,这里不必再次安装。
(3)修改相应的文件,并源码编译nginx
[root@server1 ~]# cd nginx-1.15.8
[root@server1 nginx-1.15.8]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.15.8]# vim auto/cc/gcc #将172行注释掉
171 # debug
172 #CFLAGS="$CFLAGS -g"
[root@server1 nginx-1.15.8]# pwd
/root/nginx-1.15.8
[root@server1 nginx-1.15.8]# ./configure --prefix=/usr/local/nginx/ #编译目录仍为/usr/local/nginx。相当于覆盖了之前编译的nginx1.14。
[root@server1 nginx-1.15.8]# make #只进行预编译make,不进行编译make install
(1)查看nginx-1.15.8/objs/nginx文件,同时查看/usr/local/nginx/sbin/nginx文件,来判断哪个是nginx1.14生成的nginx二进制文件,哪个是nginx1.15生成的nginx二进制文件(这两个文件的作用是一样的)
[root@server1 nginx-1.15.8]# ll objs/nginx
-rwxr-xr-x 1 root root 813152 Apr 23 20:28 objs/nginx
[root@server1 nginx-1.15.8]# ll /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 803880 Apr 23 19:51 /usr/local/nginx/sbin/nginx
通过查看两个文件的具体信息(主要是查看时间),我们发现nginx-1.15.8/objs/nginx文件是nginx1.15生成的;而/usr/local/nginx/sbin/nginx文件是nginx1.14生成的(这是因为在安装nginx1.15时没有进行安装的操作——make install)。
(2)进行平滑升级
[root@server1 nginx-1.15.8]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# cp nginx nginx.old #备份一份原来的nginx二进制文件,以免后续进行nginx服务的版本回退
[root@server1 sbin]# ls
nginx nginx.old
[root@server1 sbin]# cp -f ~/nginx-1.15.8/objs/nginx . #使用-f参数是因为/usr/local/nginx/sbin/nginx二进制文件正在运行
cp: overwrite ‘./nginx’? y
[root@server1 sbin]# ll -d nginx
-rwxr-xr-x 1 root root 813152 Apr 23 20:39 nginx
[root@server1 sbin]# /usr/local/nginx/sbin/nginx -v #此时nginx的版本已经由原来的1.14升级到了1.15
nginx version: nginx/1.15.8
[root@server1 sbin]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.8
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx/
[root@server1 sbin]# ps -ef | grep nginx
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 2074 2050 0 20:07 ? 00:00:00 nginx: worker process
nobody 2075 2050 0 20:07 ? 00:00:00 nginx: worker process
root 4676 2018 0 21:05 pts/0 00:00:00 grep --color=auto nginx
[root@server1 sbin]# kill -USR2 2050 #启动新的master进程和worker进程(是由nginx1.15启动的)
[root@server1 sbin]# ps -ef | grep nginx
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 2074 2050 0 20:07 ? 00:00:00 nginx: worker process
nobody 2075 2050 0 20:07 ? 00:00:00 nginx: worker process
root 4677 2050 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 4678 4677 0 21:05 ? 00:00:00 nginx: worker process
nobody 4679 4677 0 21:05 ? 00:00:00 nginx: worker process
root 4681 2018 0 21:05 pts/0 00:00:00 grep --color=auto nginx
[root@server1 sbin]# kill -WINCH 2050 #关闭旧的worker进程(由nginx1.14启动的worker进程)
[root@server1 sbin]# ps -ef | grep nginx
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 4677 2050 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 4678 4677 0 21:05 ? 00:00:00 nginx: worker process
nobody 4679 4677 0 21:05 ? 00:00:00 nginx: worker process
root 4687 2018 0 21:06 pts/0 00:00:00 grep --color=auto nginx
[root@server1 sbin]# ls
nginx nginx.old
[root@server1 sbin]# cp -f nginx.old nginx #使用-f参数是因为/usr/local/nginx/sbin/nginx二进制文件正在运行
cp: overwrite ‘nginx’? y
[root@server1 sbin]# ll -d nginx
-rwxr-xr-x 1 root root 803880 Apr 23 21:15 nginx
[root@server1 sbin]# ./nginx -v #此时nginx的版本已经由原来的1.15回退到了1.14
nginx version: nginx/1.14.2
[root@server1 sbin]# ./nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@server1 sbin]# ps -ef | grep nginx
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 4677 2050 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 4678 4677 0 21:05 ? 00:00:00 nginx: worker process
nobody 4679 4677 0 21:05 ? 00:00:00 nginx: worker process
root 4701 2018 0 21:16 pts/0 00:00:00 grep --color=auto nginx
[root@server1 sbin]# kill -HUP 2050 #启动新worker进程(是由nginx1.14启动的)
[root@server1 sbin]# ps -ef | grep nginx
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 4677 2050 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 4678 4677 0 21:05 ? 00:00:00 nginx: worker process
nobody 4679 4677 0 21:05 ? 00:00:00 nginx: worker process
nobody 4702 2050 0 21:16 ? 00:00:00 nginx: worker process
nobody 4703 2050 0 21:16 ? 00:00:00 nginx: worker process
root 4705 2018 0 21:17 pts/0 00:00:00 grep --color=auto nginx
[root@server1 sbin]# kill -WINCH 4677 #关闭旧的worker进程(由nginx1.15启动的worker进程)
[root@server1 sbin]# ps -ef | grep nginx
root 2050 1 0 20:00 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root 4677 2050 0 21:05 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 4702 2050 0 21:16 ? 00:00:00 nginx: worker process
nobody 4703 2050 0 21:16 ? 00:00:00 nginx: worker process
root 4708 2018 0 21:17 pts/0 00:00:00 grep --color=auto nginx