LNMP架构-nginx编译安装/平滑升级

1. nginx源码编译安装

1.1 下载nginx安装包

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

1.2 下载nginx所需的依赖性

yum install -y gcc pcre-devel openssl-devel

1.3 解压下载的nginx安装包

[root@vm5 ~]# ls
nginx-1.22.0.tar.gz
[root@vm5 ~]# tar -zxvf nginx-1.22.0.tar.gz 
nginx-1.22.0/
nginx-1.22.0/auto/
nginx-1.22.0/conf/
nginx-1.22.0/contrib/
nginx-1.22.0/src/
.......

1.4 编译nginx,三部曲

[root@vm5 ~]# ls
nginx-1.22.0  nginx-1.22.0.tar.gz
[root@vm5 ~]# cd nginx-1.22.0/
[root@vm5 nginx-1.22.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@vm5 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
......
[root@vm5 nginx-1.22.0]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.22.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
.......
[root@vm5 nginx-1.22.0]# make install
make -f objs/Makefile install
make[1]: Entering directory `/root/nginx-1.22.0'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
	|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
	|| mv '/usr/local/nginx/sbin/nginx' \
.......

1.6 创建Nginx链接,启动nginx

[root@vm5 nginx-1.20.0]# ln -s /usr/local/nginx/sbin/nginx  /usr/sbin/
[root@vm5 nginx-1.20.0]# nginx           

1.5 访问主机ip,看是否可以可以访问到nginx的默认测试发布目录

[root@vm5 ~]# ip addr
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:ce:a8:b0 brd ff:ff:ff:ff:ff:ff
    inet 172.25.32.5/24 brd 172.25.32.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fece:a8b0/64 scope link 
       valid_lft forever preferred_lft forever
[root@vm5 ~]# curl 172.25.32.5



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

2. nginx平滑升级
2.1  下载nginx新版本软件,正常执行./configure 和make 但不要执行make install
​[root@vm5 ~]# wget http://nginx.org/download/nginx-1.20.0.tar.gz
[root@vm5 ~]# ls
nginx-1.20.0  nginx-1.20.0.tar.gz  nginx-1.22.0.tar.gz
[root@vm5 ~]# tar zxvf nginx-1.22.0.tar.gz
nginx-1.22.0/
nginx-1.22.0/auto/
nginx-1.22.0/conf/
nginx-1.22.0/contrib/
......
[root@vm5 ~]# ls
nginx-1.20.0  nginx-1.20.0.tar.gz  nginx-1.22.0  nginx-1.22.0.tar.gz
[root@vm5 ~]# cd nginx-1.22.0/
[root@vm5 nginx-1.22.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@vm5 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
.....
[root@vm5 nginx-1.22.0]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.22.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
.....
2.2 备份原程序
[root@vm5 ~]# cd /usr/local/nginx/sbin/
[root@vm5 sbin]# cp nginx nginx.old
[root@vm5 sbin]# ls
nginx  nginx.old

2.3  拷贝新程序

[root@vm5 ~]# cd nginx-1.22.0/
[root@vm5 nginx-1.22.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@vm5 nginx-1.22.0]# cd objs/
[root@vm5 objs]# ls
autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@vm5 objs]# ./nginx  -v
nginx version: nginx/1.22.0
[root@vm5 objs]# \cp -f nginx /usr/local/nginx/sbin/nginx

2.4  获取当前nginx主进程pid

[root@vm5 objs]# ps ax|grep nginx
26380 ?        Ss     0:00 nginx: master process nginx
26381 ?        S      0:00 nginx: worker process
26411 pts/0    R+     0:00 grep --color=auto nginx

2.5 升级新程序

[root@vm5 objs]# kill -USR2 26380
[root@vm5 objs]# ps ax|grep nginx
26380 ?        Ss     0:00 nginx: master process nginx
26381 ?        S      0:00 nginx: worker process
26412 ?        S      0:00 nginx: master process nginx
26413 ?        S      0:00 nginx: worker process
26415 pts/0    R+     0:00 grep --color=auto nginx
2.6 关闭原worker进程但保留主进程:为了回退
 
[root@vm5 objs]# kill -WINCH 26380

2.7 访问nginx,查看nginx版本,已经由原来的1.20版本升级至1.22版本

[root@vm5 objs]# curl  localhost -I
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Thu, 14 Jul 2022 07:15:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 14 Jul 2022 02:16:43 GMT
Connection: keep-alive
ETag: "62cf7c8b-264"
Accept-Ranges: bytes

3. nginx版本回退

3.1  还原nginx程序
[root@vm5 sbin]# \cp -f nginx.old nginx

3.2 唤醒原进程

[root@vm5 sbin]# ps ax|grep nginx
26380 ?        Ss     0:00 nginx: master process nginx
26412 ?        S      0:00 nginx: master process nginx
26413 ?        S      0:00 nginx: worker process
26436 pts/0    S+     0:00 grep --color=auto nginx
[root@vm5 sbin]#  kill -HUP 26380

3.3 回收新版本的worker进程

[root@vm5 sbin]# kill -WINCH 26413

3.4 关闭新版本主进程

[root@vm5 sbin]# kill -QUIT 26412

3.5  访问nginx,查看nginx版本,已经由原来的1.22版本升级至1.20版本

[root@vm5 sbin]# curl  localhost -I
HTTP/1.1 200 OK
Server: nginx/1.20.0
Date: Thu, 14 Jul 2022 07:46:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 14 Jul 2022 02:16:43 GMT
Connection: keep-alive
ETag: "62cf7c8b-264"
Accept-Ranges: bytes

你可能感兴趣的:(nginx,架构,nginx,架构)