1、环境准备
rocky建议提前安装:yum install -y gcc pcre-devel openssl-devel zlib zlib-devel make gcc-c++ openss perl-ExtUtils-Embed
ubuntu建议提前安装: apt install -y gcc libpcre3-dev libssl-dev
如果提示缺少zlib library,请到http://www.zlib.net/ 下载zlib编译安装即可
2、Rocky安装
[root@nginx ~]# useradd -s /sbin/nologin nginx
[root@nginx ~]# tar xf nginx-1.22.0.tar.gz -C /usr/local/
[root@nginx ~]# cd /usr/local/
[root@nginx local]# cd nginx-1.22.0/
[root@nginx nginx-1.22.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx nginx-1.22.0]# make && make install
[root@nginx nginx-1.22.0]# chown -R nginx. /apps/nginx #更改权限
[root@nginx nginx-1.22.0]# cd /apps/nginx/
[root@nginx nginx]# ln -s /apps/nginx/sbin/nginx /usr/sbin/ #创建软连接
[root@nginx nginx]# nginx #启动测试
[root@nginx nginx]# ss -ntl #查看端口
[root@nginx nginx]# nginx -s stop 停止nginx
[root@nginx nginx]# ss -ntl
[root@nginx nginx]# vim /usr/lib/systemd/system/nginx.service #创建service文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
[root@nginx nginx]# mkdir /apps/nginx/run/ #创建sevices例pid指定的目录
[root@nginx nginx]# vim /apps/nginx/conf/nginx.conf #修改配置文件
pid /apps/nginx/run/nginx.pid;
[root@nginx nginx]# systemctl daemon-reload
[root@nginx nginx]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx nginx]# systemctl status nginx.service
遇到的问题:
./configure: error: C compiler cc is not found
解决办法:
[root@nginx nginx-1.22.0]# yum install -y gcc
遇到的问题:
./configure: error: the HTTP rewrite module requires the PCRE library.
解决办法:
[root@nginx nginx-1.22.0]# yum install pcre-devel -y
遇到的问题:
./configure: error: SSL modules require the OpenSSL library.
解决办法:
[root@nginx nginx-1.22.0]# yum install -y openssl-devel
3、平滑升级和回滚
[root@nginx ~]# tar xf nginx-1.23.1.tar.gz
[root@nginx ~]# cd nginx-1.23.1
[root@nginx nginx-1.23.1]# nginx -V #查看旧版本编译信息
[root@nginx nginx-1.23.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx nginx-1.23.0]# make #千万不要make install
[root@nginx nginx-1.23.1]# cp /apps/nginx/sbin/nginx /opt/ #拷贝旧得nginx文件到opt目录下
[root@nginx nginx-1.23.1]# cp objs/nginx /apps/nginx/sbin/nginx -f #强制覆盖旧的nginx
cp: overwrite '/apps/nginx/sbin/nginx'? y
[root@nginx nginx-1.23.1]# nginx -v
nginx version: nginx/1.23.1
[root@nginx nginx-1.23.1]# kill -USR2 `cat /apps/nginx/run/nginx.pid` #发送信号USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx
[root@nginx nginx-1.23.1]# ps auxf | grep nginx
root 39282 0.0 0.0 12140 1188 pts/0 S+ 21:11 0:00 | \_ grep --color=auto nginx
root 36127 0.0 0.1 42572 2836 ? Ss 20:06 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 36128 0.0 0.2 74680 4980 ? S 20:06 0:00 \_ nginx: worker process
root 39277 0.0 0.3 42580 6068 ? S 21:11 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 39278 0.0 0.2 74768 4904 ? S 21:11 0:00 \_ nginx: worker process
[root@nginx nginx-1.23.1]# kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
##向原Nginx主进程发送WINCH信号,它会逐步关闭旗下的工作进程(主进程不退出),这时所有请都会由新版Nginx处理
#这里做个快照,一会回滚旧版本使用。
[root@nginx nginx-1.23.1]# kill -QUIT `cat /apps/nginx/run/nginx.pid.oldbin` #发送QUIT信号,退出老的master
[root@nginx nginx-1.23.1]# nginx -v #查看版本是不是已经是新版了
nginx version: nginx/1.23.1
[root@nginx nginx-1.23.1]# curl -I 127.0.0.1 #查看版本是不是已经是新版了
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Tue, 13 Sep 2022 13:24:15 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 13 Sep 2022 11:35:45 GMT
Connection: keep-alive
ETag: "63206b11-267"
Accept-Ranges: bytes
=================================================================
# 回滚
[root@nginx ~]# kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin` #发送HUP信号,重新拉起旧版本的worker
[root@nginx ~]# kill -QUIT `cat /apps/nginx/run/nginx.pid` #关闭新版的master
[root@nginx ~]# mv /opt/nginx /apps/nginx/sbin/ #恢复旧版的文件
mv: overwrite '/apps/nginx/sbin/nginx'? y
[root@nginx ~]# nginx -v #查看版本是不是已经恢复
nginx version: nginx/1.22.0
[root@nginx ~]# curl -I 127.0.1 #查看版本是不是已经恢复
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Tue, 13 Sep 2022 13:29:21 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 13 Sep 2022 11:35:45 GMT
Connection: keep-alive
ETag: "63206b11-267"
Accept-Ranges: bytes
4、Ubuntu安装
[root@nginx-ubuntu ~]# tar xf nginx-1.22.0.tar.gz -C /usr/local/
[root@nginx-ubuntu ~]# cd /usr/local/
[root@nginx-ubuntu nginx-1.22.0]# cd nginx-1.22.0/
[root@nginx-ubuntu nginx-1.22.0]# useradd -s /sbin/nologin nginx
[root@nginx-ubuntu nginx-1.22.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx-ubuntu nginx-1.22.0]# make && make install
[root@nginx-ubuntu nginx-1.22.0]# chown -R nginx. /apps/nginx
[root@nginx-ubuntu nginx-1.22.0]# cd /apps/nginx
[root@nginx-ubuntu nginx]#vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
[root@nginx-ubuntu nginx]# mkdir /apps/nginx/run
[root@nginx-ubuntu nginx]# vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
[root@nginx-ubuntu nginx]# systemctl daemon-reload
[root@nginx-ubuntu nginx]# systemctl enable --now nginx.service
[root@nginx-ubuntu nginx]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
[root@nginx-ubuntu nginx]# ln -s /apps/nginx/sbin/nginx /usr/local/sbin/
[root@nginx-ubuntu nginx]# nginx -v
nginx version: nginx/1.22.0
[root@nginx-ubuntu nginx]# curl -I 127.1
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Tue, 13 Sep 2022 13:47:22 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 13 Sep 2022 12:33:38 GMT
Connection: keep-alive
ETag: "632078a2-267"
Accept-Ranges: bytes
========================================================================
# 平滑升级
[root@nginx-ubuntu ~]# tar xf nginx-1.23.1.tar.gz
[root@nginx-ubuntu ~]# cd nginx-1.23.1/
[root@nginx-ubuntu nginx-1.23.1]# nginx -V #查看旧版本编译信息
[root@nginx-ubuntu nginx-1.23.1]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@nginx-ubuntu nginx-1.23.1]# make #不要写make install
[root@nginx-ubuntu nginx-1.23.1]#cp /apps/nginx/sbin/nginx /opt/ #备份旧得nginx文件
[root@nginx-ubuntu nginx-1.23.1]#cp objs/nginx /apps/nginx/sbin/ -f #强行覆盖旧得nginx
[root@nginx-ubuntu nginx-1.23.1]#nginx -v
nginx version: nginx/1.23.1
[root@nginx-ubuntu nginx-1.23.1]#kill -USR2 `cat /apps/nginx/run/nginx.pid` #发送信号USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的nginx
[root@nginx-ubuntu nginx-1.23.1]#ps auxf|grep nginx #查看进程
root 42275 0.0 0.0 9392 720 pts/0 S+ 21:58 0:00 | \_ grep --color=auto nginx
root 38580 0.0 0.0 8604 836 ? Ss 21:40 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 38581 0.0 0.1 9296 3428 ? S 21:40 0:00 \_ nginx: worker process
root 42265 0.0 0.3 8600 6136 ? S 21:58 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 42266 0.0 0.1 9288 3352 ? S 21:58 0:00 \_ nginx: worker process
[root@nginx-ubuntu nginx-1.23.1]#kill -WINCH `cat /apps/nginx/run/nginx.pid`
[root@nginx-ubuntu nginx-1.23.1]#kill -QUIT `cat /apps/nginx/run/nginx.pid`
[root@nginx-ubuntu nginx-1.23.1]#ps auxf|grep nginx
root 42337 0.0 0.0 9392 660 pts/0 S+ 22:00 0:00 | \_ grep --color=auto nginx
root 38580 0.0 0.0 8604 836 ? Ss 21:40 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 38581 0.0 0.1 9296 3428 ? S 21:40 0:00 \_ nginx: worker process
#### 有时候不能平滑切换
[root@nginx-ubuntu nginx-1.23.1]#curl -I 127.0.1
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Tue, 13 Sep 2022 14:03:31 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 13 Sep 2022 12:33:38 GMT
Connection: keep-alive
ETag: "632078a2-267"
Accept-Ranges: bytes
=======================================================
#回滚
[root@nginx-ubuntu ~]#kill -HUP `cat /apps/nginx/run/nginx.pid`
[root@nginx-ubuntu ~]#kill -QUIT `cat /apps/nginx/run/nginx.pid`
[root@nginx-ubuntu ~]#mv /opt/nginx /apps/nginx/sbin/
[root@nginx-ubuntu ~]#nginx -v
nginx version: nginx/1.22.0
[root@nginx-ubuntu ~]#curl -I 127.0.1
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Tue, 13 Sep 2022 14:08:40 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 13 Sep 2022 12:33:38 GMT
Connection: keep-alive
ETag: "632078a2-267"
Accept-Ranges: bytes
遇到的问题:
./configure: error: C compiler cc is not found
解决办法:
[root@nginx-ubuntu nginx-1.22.0]#apt install -y gcc
遇到的问题:
./configure: error: the HTTP rewrite module requires the PCRE library.
解决办法:
[root@nginx-ubuntu nginx-1.22.0]#apt install -y libpcre3-dev
遇到的问题:
./configure: error: SSL modules require the OpenSSL library.
解决办法:
[root@nginx-ubuntu nginx-1.22.0]#apt install libssl-dev
遇到的问题:
./configure: error: the HTTP gzip module requires the zlib library.
解决办法:
http://www.zlib.net/ 下载zlib
[root@nginx-ubuntu ~]#tar xf zlib-1.2.12.tar.gz
[root@nginx-ubuntu ~]#cd zlib-1.2.12/
[root@nginx-ubuntu zlib-1.2.12]#./configure
[root@nginx-ubuntu zlib-1.2.12]#make && make install