1. Nginx编译安装

Ps: 其他机器yum安装好nginx1.18 —-> 模拟生产环境使用的环境及配置。

1.1 获取原有环境的编译参数

  1. [root@web04 ~]# nginx -V #获取老板要求的坏境 <----
  2. nginx version: nginx/1.18.1
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  4. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

1.2 新的环境重新下载软件包安装

  1. 准备nginx用户
  2. [root@nfs nginx-1.16.1]# groupadd nginx
  3. [root@nfs nginx-1.16.1]# useradd nginx -g nginx
  4. #真实情况是先编译后会报缺少的依赖软件,这里提前安装已知依赖
  5. [root@nfs nginx-1.16.1]# yum install gcc redhat-rpm-config libxslt-devel gd-devel perl-ExtUtils-Embed
  6. geoip-devel gperftools-devel pcre-devel openssl-devel -y
  7. #制安装前请先去官网下载编译安装包,新机器模拟环境安装1.16的nginx
  8. [root@nfs ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
  9. [root@nfs ~]# tar xf nginx-1.14.2.tar.gz
  10. [root@nfs ~]# cd nginx-1.14.2
  11. [root@nfs ~]# ./configure --prefix=/复制粘贴老板要求的配置

./configure: error: the HTTP XSLT module requires the libxml2/libxslt libraries. You can either do not enable the module or install the libraries. [root@nfs01 ~/nginx-1.14.2]# yum install -y libxml2 # yum search xx提示 依次找到库 [root@nfs01 ~/nginx-1.14.2]# yum -y install libxslt-devel [root@nfs01 ~/nginx-1.14.2]# yum install GeoIP-devel ——-> 处理完报错记得重新执行 ./configure —xxx命令哦

1.3 编译安装步骤: 三部曲

./configure 定义软件的路径\定义软件模块\ —-> Makefile文件
make 读取清单,然后进行编译的操作 [生成可执行的二进制文件]
make install 将配置文件\二进制文件拷贝到指定的目录

1.4 启动Nginx 停止Nginx

[root@nfs nginx-1.16.1]# nginx -c /etc/nginx/nginx.conf #停止 nginx -s stop
[root@nfs01 ~/nginx-1.14.2]#nginx -v
nginx version: nginx/1.14.2


1. 平滑升级


Nginx升级.pdf

1.1Nginx平滑升级概述

1.什么是平滑升级

在进行服务版本升级的时候,对于用户访问体验无感知、不 会造成服务中断。

2.Nginx进行平滑升级的原理
image.png

总结:

  • 在不停掉老进程的情况下启动新进程。
  • 老进程负责处理仍然没有请求完的请求,但不再接收处理请求。
  • 新进程接收新请求。
  • 老进程处理完所有请求后,关闭所有连接,停止。

2. 平滑升级 1.14 —-> 1.18

信号 含义
QUIT 优雅关闭、 quit
HUP 优雅重启 reload
USR1 重新打开日志文件、 reopen
USR2 平滑升级可执行的二进制程序
WINCH 平滑关闭Worker进程
思路:
1.下载新版本Nginx
2.了解原旧版本Nginx编译参数
3.将旧的nginx二进制文件进行备份,然后替换成为新的
nginx二进制文件

4.向旧的Nginx的Master进程发送USR2信号
    ---->  旧的master进程的pid文件添加后缀.oldbin
    ---->  master进程会用新nginx二进制文件启动新的
master进程。

5.向旧的master进程发送WINCH信号,旧的worker子进
程优雅退出。
6.向旧的master进程发送QUIT信号,旧的master进程就
退出了。


0. 准备一个nginx服务,提供一个下载的站点**
(我们需要一个请求,平滑升级的全过程都连接,以便查看结果)
_
[root@web01 ~]# dd if=/dev/zero of=/etc/nginx/html/bigdata bs=500M count=1
[root@nfs01 /etc/nginx]# vim /etc/nginx/nginx.conf

...
server {
     listen       80 default_server;
     server_name  localhost;

     #charset koi8-r;

     #access_log  logs/host.access.log  main;

     location / {
         root   html;
         limit_rate 200k;
         index  index.html index.htm;
     }
...

[root@nfs01 /etc/nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [emerg] mkdir() “/var/cache/nginx/client_temp” failed (2: No such file or directory) [root@nfs01 /etc/nginx]# mkdir /var/cache/nginx/client_temp -p [root@nfs01 /etc/nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@nfs01 /etc/nginx]# nginx -c /etc/nginx/nginx.conf

  • 拉起请求(浏览器—-> 10.0.0.31/bigdata)

image.png


1.下载新版本Nginx 1.18
[root@web01 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@web01 ~]# cd nginx-1.18.0/
\
2.了解原版本Nginx编译参数

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

[root@web01 ~]# make #生成新的二进制文件 ( 仅make即可,不需要make install )

3.将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件
**
[root@nfs ]# mv /usr/sbin/{nginx,nginx-1.14}
[root@nfs01 ~/nginx-1.18.0]# cp ./objs/nginx /usr/sbin/nginx
[root@nfs01 ~/nginx-1.18.0]# nginx -v
nginx version: nginx/1.18.0

4.向旧的Nginx的Master进程发送USR2信号

  • 旧的master进程的pid文件添加后缀.oldbin
  • master进程会用新nginx二进制文件启动新的master进程。

———->>
[root@nfs01 ~]# ps -ef |grep nginx #1.14

root       1309      1  0 16:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c  /etc/nginx/nginx.conf
www        1787   1309  0 19:08 ?        00:00:00 nginx: worker process
www        1788   1309  0 19:08 ?        00:00:00 nginx: worker process
www        1789   1309  0 19:08 ?        00:00:00 nginx: worker process
www        1790   1309  0 19:08 ?        00:00:00 nginx: worker process

[root@nfs01 ~]# kill -USR2 旧版程序的主进程号或进程文件名 # 发送USR2信号,平滑升级Nginx
[root@nfs01 ~]# ps -ef |grep nginx

root       1309      1  0 16:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        1787   1309  0 19:08 ?        00:00:00 nginx: worker process
www        1788   1309  0 19:08 ?        00:00:00 nginx: worker process
www        1789   1309  0 19:08 ?        00:00:00 nginx: worker process
www        1790   1309  0 19:08 ?        00:00:00 nginx: worker process

root       5400   1309  0 19:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5401   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5402   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5403   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5404   5400  0 19:15 ?        00:00:00 nginx: worker process
  • 5.向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。

[root@nfs01 ~]# kill -WINCH 1309
[root@nfs01 ~]#ps -ef |grep nginx

root       1309      1  0 16:53 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root       5400   1309  0 19:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5401   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5402   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5403   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5404   5400  0 19:15 ?        00:00:00 nginx: worker process
  • 6.向旧的master进程发送QUIT信号,旧的master进程就退出了

[root@nfs01 ~]# kill -QUIT 1309
[root@nfs01 ~]#ps -ef |grep nginx

root       5400      1  0 19:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5401   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5402   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5403   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5404   5400  0 19:15 ?        00:00:00 nginx: worker process

3. 平滑降级 1.18 —-> 1.14

1.替换二进制文件
[root@nfs01 ~]# mv /usr/sbin/nginx /usr/sbin/nginx_1.18
[root@nfs01 ~]# mv /usr/sbin/nginx_1.18 /usr/sbin/nginx

2.执行USR2信号,平滑过度
[root@nfs01 ~]# ps -ef |grep nginx            (旧版本的Nginx1.19)
root       5400      1  0 19:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5401   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5402   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5403   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5404   5400  0 19:15 ?        00:00:00 nginx: worker process

[root@nfs01 ~]# kill -USR2 5400
[root@nfs01 ~]#ps -ef |grep nginx
root       5400      1  0 19:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5401   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5402   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5403   5400  0 19:15 ?        00:00:00 nginx: worker process
www        5404   5400  0 19:15 ?        00:00:00 nginx: worker process
root       5459   5400  1 19:24 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5460   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5461   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5462   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5463   5459  0 19:24 ?        00:00:00 nginx: worker process


3.将1.19版本的Nginx的Work关闭
[root@nfs01 ~]# kill -WINCH 5400
[root@nfs01 ~]# ps -ef |grep nginx
root       5400      1  0 19:15 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root       5459   5400  0 19:24 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5460   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5461   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5462   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5463   5459  0 19:24 ?        00:00:00 nginx: worker process


4.将1.19版本的Nginx的Master关闭
[root@nfs01 ~]# kill -QUIT 5400
[root@nfs01 ~]# ps -ef |grep nginx            # 1.18 
root       5459      1  0 19:24 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www        5460   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5461   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5462   5459  0 19:24 ?        00:00:00 nginx: worker process
www        5463   5459  0 19:24 ?        00:00:00 nginx: worker process