美文网首页
Nginx安装及升级

Nginx安装及升级

作者: A二十一画 | 来源:发表于2022-01-17 14:53 被阅读0次

(学习记录一下)

Yum安装

1、添加Nginx源

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2、安装Nginx

[root@localhost ~]# yum -y install nginx        

编译安装

1、安装相关依赖包

# nginx的正则rewrite会使用pcre; gzip会使用zlib,ssl会使用openssl
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc*

2、创建Nginx用户

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

3、编译安装

[root@localhost ~]# tar -zxvf nginx-1.21.5.tar.gz
[root@localhost ~]# cd nginx-1.21.5
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module 
[root@localhost ~]# make && make install

--prefix 指定 Nginx 的安装目录

--user 和 --group 指定 Nginx 运行用户和组

--with-http_stub_status_module 启动 http_stub_status_module模块支持状态统计

--with-http_ssl_module 支持https的ssl加密

平滑升级/添加模块

查看当前版本/当前模块

[root@localhost ~]# nginx -V
nginx version: nginx/1.21.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (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

1、重新编译Nginx

[root@localhost ~]# cd nginx-1.21.5
[root@localhost ~]# tar -zxvf nginx-1.21.5.tar.gz
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@localhost ~]# make

2、替换原有nginx二进制文件

[root@localhost ~]# cp -a /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@localhost ~]# cp -a nginx-1.21.5/objs/nginx /usr/local/nginx/sbin/nginx

3、发送USR2信号,Nginx会启动一个新版本的master进程

[root@localhost ~]# ps -aux | grep nginx
root      34665  0.0  0.0  46008  1132 ?        Ss   17:27   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    34667  0.0  1.5  75228 28628 ?        S    17:27   0:00 nginx: worker process
root      34718  0.0  0.0 112824   976 pts/9    R+   17:28   0:00 grep --color=auto nginx
[root@localhost ~]# kill -USR2 34665
[root@localhost ~]# ps -aux | grep nginx
root      34665  0.0  0.0  46008  1320 ?        Ss   17:27   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    34667  0.0  1.5  75228 28628 ?        S    17:27   0:00 nginx: worker process
root      34719  0.0  0.1  45996  3312 ?        S    17:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    34720  0.0  1.5  75216 28644 ?        S    17:29   0:00 nginx: worker process
root      34722  0.0  0.0 112824   980 pts/9    R+   17:29   0:00 grep --color=auto nginx

4、向旧的Nginx主进程(master)发送WINCH信号,它会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理

[root@localhost ~]# kill -WINCH 34665
[root@localhost ~]# ps -aux | grep nginx
root      34665  0.0  0.0  46008  1320 ?        Ss   17:27   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root      34719  0.0  0.1  45996  3312 ?        S    17:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    34720  0.0  1.5  75216 28644 ?        S    17:29   0:00 nginx: worker process
root      34726  0.0  0.0 112824   980 pts/9    R+   17:29   0:00 grep --color=auto nginx

5、发送QUIT信息

[root@localhost ~]# kill -QUIT 34665
[root@localhost ~]# ps -aux | grep nginx
root      34719  0.0  0.1  45996  3312 ?        S    17:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    34720  0.0  1.5  75216 28644 ?        S    17:29   0:00 nginx: worker process
root      34734  0.0  0.0 112824   980 pts/9    R+   17:30   0:00 grep --color=auto nginx

6、如果需要回滚,发送HUP信号(在第5步之前操作)

[root@localhost ~]# kill -HUP 34665

相关文章

网友评论

      本文标题:Nginx安装及升级

      本文链接:https://www.haomeiwen.com/subject/klpchrtx.html