目录
安装篇
1.拉取包
2.解压
3.配置
4.编译和编译安装
5.准备启动
6.通过systemd管理服务
systemd服务来启动
平滑升级实战
安装篇
前提:关闭防火墙
[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# systemctl disabled firewalld
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@nginx ~]#wget https://nginx.org/download/nginx-1.28.3.tar.gz
可以去官网下载自己所需要的版本在上传至系统
1.拉取包
下载工具包
[root@nginx ~]# yum install -y vim bash-completion yum-utils wget tar bzip2 net-tools gcc zlib-devel pcre-devel openssl openssl-devel(建议一个一个下,会有纰漏)
[root@nginx ~]# useradd -s /sbin/nologin nginx
[root@nginx ~]# mkdir -p /apps/nginx
[root@nginx ~]# ls
anaconda-ks.cfg nginx-1.28.3.tar.gz
2.解压
tar -zxvf nginx-1.28.3.tar.gz
cd nginx-1.28.3
3.配置
./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
4.编译和编译安装
[root@nginx nginx-1.28.3]# make && make install
5.准备启动
[root@nginx nginx-1.28.3]# chown -R nginx:nginx /apps/nginx/
[root@nginx nginx-1.28.3]#ll
total 4
drwxr-xr-x 2 nginx nginx 4096 Jun 21 21:02 conf
drwxr-xr-x 2 nginx nginx 40 Jun 21 21:02 html
drwxr-xr-x 2 nginx nginx 6 Jun 21 21:02 logs
drwxr-xr-x 2 nginx nginx 19 Jun 21 21:02 sbin
[root@nginx nginx-1.28.3]# ln -s /apps/nginx/sbin/nginx /usr/sbin/
[root@nginx nginx-1.28.3]# nginx -v ##查看版本
[root@nginx nginx-1.28.3]# nginx ##启动nginx服务
[root@nginx nginx-1.28.3]# ss -tulp |grep nginx
tcp LISTEN 0 511 0.0.0.0:http 0.0.0.0:* users:(("nginx",pid=22387,fd=6),("nginx",pid=22386,fd=6))
6.通过systemd管理服务
[root@nginx nginx-1.28.3]# cat << EOF > /usr/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
EOF
[root@nginx nginx-1.28.3]# mkdir /apps/nginx/run
[root@nginx nginx-1.28.3]# vim /apps/nginx/conf/nginx.conf
# 取消注释,修改配置文件
pid /apps/nginx/run/nginx.pid;
systemd服务来启动
[root@nginx nginx-1.28.3]# systemctl daemon-reload
[root@nginx nginx-1.28.3]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx nginx-1.28.3]# systemctl status nginx
平滑升级实战
平滑升级的实用和优势:能在不中断服务、业务、不影响客户端连接的情况下,将nginx主程序升级到新版本。这是nginx在生产环境的一个重要特性。特点:新连接交给新版本,就连接交给旧版本,不会抢占请求。
升级过程中:
1.网站继续访问
2.API继续相应
3.TCP连接不断开
4.用户几乎感觉不到升级
前提:需要编译安装好了的新旧版本的nginx,作者所使用的旧版本是1.26.3,替换的新版本是1.30.3。资源包可以去官网下载nginx: download
1.30.3nginx版本节点(编译安装)
[root@localhost ~]# cd /apps/nginx/
[root@localhost nginx]# ls
client_body_tempfastcgi_templogsrunscgi_temp
confhtmlproxy_tempsbinuwsgi_temp
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# cp nginx nginx.bak
将编译好的新版本的nginx拷贝到旧节点(1.26.3nginx编译安装)上
scp nginx.bak 192.168.57.105:/apps/nginx/sbin/
在旧版本的节点上,将刚刚传送过来的文件替换
mv nginx.bak nginx
mv: overwrite 'nginx'? y
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# chown nginx.nginx nginx
[root@localhost sbin]# ll
total 5812
-rwxr-xr-x 1 nginx nginx 5948936 Jul 16 23:01 nginx
此时查看版本,发现版本从1.26.3从1.30.3
[root@localhost sbin]# nginx -V
nginx version: nginx/1.30.3
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-14) (GCC)
built with OpenSSL 3.5.5 27 Jan 2026
TLS SNI support enabled
configure arguments: --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
至此已完成升级