nginx版本升级

选取nginx稳定版本1.20.2,wget下载。

wget http://nginx.org/download/nginx-1.20.2.tar.gz

1、先确认旧的nginx进程是已经存在的

旧版本nginx启动进程

[root@master2 nginx]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1496 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
nobody   17441  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17442  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17443  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17444  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17445  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process

2、开始编译安装新版本1.20.2的nginx

编译的步骤如下所示:
mv nginx-1.20.2.tar.gz /usr/local
cd /usr/local
tar xf nginx-1.20.2.tar.gz
先获取旧版本nginx编译的选项

[root@master2 nginx]# sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

拿到这个选项,然后放在新版本nginx里面。执行configure,没有内容,直接执行,./configure

[root@master2 nginx-1.20.2]# pwd
/usr/local/nginx-1.20.2
[root@master2 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
checking for GeoIP IPv6 support ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

注意:有些人可能有疑惑,新下载的nginx在执行./configure的时候–prefix指定的目录是需要指向旧的nginx所指向的prefix目录还是随便指向一个就行,答案是需要指向旧版本的nginx的安装目录
执行make命令

[root@master2 nginx-1.20.2]# make

执行完成之后不要执行make install指令,这点需要注意。

3、平滑升级

3.1、先备份旧的nginx二进制可执行程序

[root@master2 nginx-1.20.2]# cd /usr/local/nginx/sbin
[root@master2 sbin]# ls
nginx
[root@master2 sbin]# cp nginx{,.bak}
[root@master2 sbin]# ls
nginx  nginx.bak

3.2、查看未升级前的nginx版本

[root@master2 sbin]# ./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC
xxxx
xxx

3.3、找到nginx-1.20.2新版本的nginx的二进制执行程序

[root@master2 sbin]# cd /usr/local/nginx-1.20.2/
[root@master2 nginx-1.20.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@master2 nginx-1.20.2]# cd objs
[root@master2 objs]# ls
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src

上面的这个nginx就是我们要拿到的新版本的nginx可执行程序文件。

3.4、使用nginx-1.20.2的二进制文件将nginx-1.16.1的二进制文件进行强制覆盖
[root@master2 objs]# cp -f nginx /usr/local/nginx/sbin/nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
3.5、设定旧的服务不再接收用户请求(下线),新服务启动子进程接收用户请求(上线)
先查看当前未升级的nginx进程(这是旧版本的nginx进程)

[root@master2 objs]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1496 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
nobody   17441  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17442  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17443  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17444  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17445  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process

找到nginx父进程的pid号,现在对其发送USR2信号

[root@master2 objs]# kill -USR2 17440 #设定新的子进程开始接收用户的访问请求,旧的不再接受用户的访问请求
再次查看进程

[root@master2 objs]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1496 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
nobody   17441  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17442  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17443  0.0  0.0  80120  2212 ?        S    11:42   0:00 nginx: worker process
nobody   17444  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
nobody   17445  0.0  0.1  80120  2456 ?        S    11:42   0:00 nginx: worker process
root     21081  0.5  0.1  79740  4080 ?        S    20:00   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21083  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21086  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process

现在是nginx的新老版本的进程共存的一种情况。虽然现在旧版本的nginx进程还存在,但是已经不再接受用户的请求了。除此之外,旧版本的nginx进程也依然处于监听的状态,我们通过lsof命令可以看到,比如:

[root@master2 objs]# lsof -p 17440 | grep LISTEN # 这里的pid号是旧版本的nginx的master进程的pid号

nginx   17440 root    6u  IPv4             120768      0t0      TCP *:http (LISTEN)

虽然在监听,但实际不会处理新连接,因为fd已经从epoll中移出了。另外,旧master是新master的父进程,所以新master才能共享打开的监听端口。保留旧版本的master是为了方便回滚(当然你可以发信号QUIT或者直接杀掉进程)

3.6、进行旧服务进程的关闭
[root@master2 objs]# kill -WINCH 17440 # 进行旧服务进程的关闭,该pid号是旧版本的nginx的master进程的pid号
再次查看当前nginx进程

[root@master2 objs]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   11:42   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    20:00   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21083  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process
nobody   21086  0.0  0.0  80192  2200 ?        S    20:00   0:00 nginx: worker process

可以看到现在的旧版本的nginx的worker进程已经全部被杀死了,只剩下的旧版本nginx的master进程
确定升级没有任何问题的话,那么现在我们可以把这个master进程给杀死掉。可以用kill -QUIT把旧master进程杀掉。方法已经教给大家了,但是这里我先不杀死,因为我还要往下演示如何回退。

3.7、查看当前nginx的版本

[root@master2 nginx]# sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

可以看到现在已经升级成功了。
还可以访问一下

[root@master2 nginx]# curl http://192.168.50.129
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
xxx
xxx
xxx

4、现在我们演示一下如何回退

这种情况主要是用于当新版本的nginx升级失败之后,我们立马回退到旧版本的nginx

4.1、将旧版本的nginx二进制文件强行覆盖
[root@master2 sbin]# mv nginx.bak nginx
mv: overwrite ‘nginx’? y
查看进程

[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    Aug20   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21083  0.0  0.1  80192  2452 ?        S    Aug20   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   41895  0.0  0.0  80192  2200 ?        S    10:53   0:00 nginx: worker process

4.2、向旧版本nginx进程发送HUP信号
[root@master2 sbin]# kill -HUP 17440 #注意这是旧版本的nginx进程pid号
说明一下:这个命令就相当与reload指令的作用,把旧的nginx的worker进程拉起来,但是咱们并不是直接使用reload的方式来执行,而是发送HUP信号,它会在没有worker进程时启动worker进程,这点需要注意一下。
此时再次查看进程

[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    Aug20   0:00 nginx: master process sbin/nginx
nobody   21082  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21083  0.0  0.1  80192  2452 ?        S    Aug20   0:00 nginx: worker process
nobody   21084  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   21085  0.0  0.0  80192  2200 ?        S    Aug20   0:00 nginx: worker process
nobody   41895  0.0  0.0  80192  2200 ?        S    10:53   0:00 nginx: worker process
nobody   42070  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42071  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42072  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42073  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42074  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process

发现多了很多worker进程,多出来的部分是旧版本的nginx进程。

4.3、让新版本的服务停止接收用户请求

[root@master2 sbin]# kill -USR2 21081

此时,接收用户请求的是旧版本的nginx进程。新版本的nginx进程不再接受用户请求

4.4、进行新版本服务进程的关闭

[root@master2 sbin]# kill -WINCH 21081

查看一下进程

[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
root     21081  0.0  0.1  79740  4080 ?        S    Aug20   0:00 nginx: master process sbin/nginx
nobody   42070  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42071  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42072  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42073  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42074  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process

现在,旧版本已经回退成功了,我们可以把新版本的nginx的master进程发送QUIT进程将其退出。

4.5、kill掉新版本nginx进程

[root@master2 sbin]# kill -QUIT 21081
[root@master2 sbin]# ps aux | grep nginx
root     17440  0.0  0.0  79732  1652 ?        Ss   Aug20   0:00 nginx: master process sbin/nginx
nobody   42070  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42071  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42072  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42073  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process
nobody   42074  0.0  0.0  80120  2208 ?        S    15:42   0:00 nginx: worker process

现在已经回退成功了

5,只编译安装,没有旧版本

这边有个不错的编译安装的步骤,来记录一下
1、安装预处理环境

[root@localhost ~]# dnf install -y lrzsz  psmisc lsof wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel  nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed gd-devel GeoIP GeoIP-devel

2、下载包并解压

wget -O /usr/local/nginx-1.16.1.tar.gz http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf /usr/local/nginx-1.16.1.tar.gz -C /usr/local

3、编译选项

cd nginx-1.16.1
./configure --prefix=/usr/local/nginx  --with-http_ssl_module \
            --with-http_v2_module  \
            --with-http_realip_module \
            --with-http_addition_module  \
            --with-http_image_filter_module \
            --with-http_geoip_module \
            --with-http_gunzip_module \
            --with-http_stub_status_module \
            --with-http_gzip_static_module \
            --with-pcre \
            --with-stream \
            --with-stream_ssl_module \
            --with-stream_realip_module

4、编译及安装

[root@localhost nginx-1.16.1]# make && make install #创建目录,并将生成的模块和文件件复制到相应的目录:
5、添加nginx用户

[root@master2 nginx-1.16.1]# useradd nginx -s /sbin/nologin -u 2000 #以普通用户启动nginx
[root@master2 nginx-1.16.1]# chown nginx.nginx -R /usr/local/nginx #授权
nginx安装完毕之后,在/usr/local/nginx目录下面能够看到几个目录

[root@master2 nginx-1.16.1]# cd /usr/local/nginx
[root@master2 nginx]# ls
conf  html  logs  sbin

conf:该目录中保存了nginx所有的配置文件,其中nginx.conf是nginx服务器的最核⼼最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用时将其复制为并将default去掉即可。
html:该目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
logs:该目录用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
sbin:该目录用来保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
6、验证版本

[root@master2 nginx]# sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

7、启动nginx并访问

[root@master2 nginx]# sbin/nginx 

一些排错问题
这是后续慢慢自己总结的一些。
1、今天遇到一个学员的问题。在执行make install 的时候,报了错误,详细的错误是Error: mkdir: cannot create directory ‘/srv/nginx --with-file-aio File name too long就是这种类似的,这是在make install 阶段。一般来说,make install阶段出现问题主要有权限问题,比如使用普通用户进行make install可能写不进去prefix指定的目录,改成使用root用户就可以了。但是这次不一样,报的错误是不能创建目录,其实在make install阶段报错只是把问题表现出来了,实际上configure阶段就有问题了,只是没有明显的错误,执行echo $?你会发现也是显示结果为0,表示成功。经过后续的仔细研究,发现在configure的命令中出现了可疑字符问题。如下所示:

[root@localhost nginx-1.16.1]# cat -A a.sh
./configure \$
--prefix=/srv/nginxM-BM- \$
--with-file-aioM-BM- \$
--with-http_auth_request_moduleM-BM-  \$
--with-http_ssl_module \$
--with-http_addition_moduleM-BM- \$
--with-http_xslt_module=dynamicM-BM- \$
--with-http_geoip_module=dynamicM-BM- \$

也就是里面的M-BM-字符,这种字符会导致我们命令直接不可用,但是看不到的,只能使用cat -A把所有的字符都显示出来才行。所以我们要去掉,很简单,如下命令所示:

sed -i 's/\xc2\xa0/ /g' a.sh

这样就可以去掉特殊字符了、

6,Windows安装

下载链接 http://nginx.org/download/nginx-1.24.0.zip
解压,解压完进入根目录 cmd ,启动 输入命令:start nginx.exe

验证是否启动成功,成功界面如下
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值