nginx编译安装+平滑升级实战

发布时间:2026/7/19 16:00:26
nginx编译安装+平滑升级实战 目录安装篇1.拉取包2.解压3.配置4.编译和编译安装5.准备启动6.通过systemd管理服务systemd服务来启动平滑升级实战安装篇前提关闭防火墙[rootnginx ~]# systemctl stop firewalld[rootnginx ~]# systemctl disabled firewalld[rootnginx ~]# setenforce 0[rootnginx ~]# sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config[rootnginx ~]#wget https://nginx.org/download/nginx-1.28.3.tar.gz可以去官网下载自己所需要的版本在上传至系统1.拉取包下载工具包[rootnginx ~]# yum install -y vim bash-completion yum-utils wget tar bzip2 net-tools gcc zlib-devel pcre-devel openssl openssl-devel建议一个一个下会有纰漏[rootnginx ~]# useradd -s /sbin/nologin nginx[rootnginx ~]# mkdir -p /apps/nginx[rootnginx ~]# lsanaconda-ks.cfg nginx-1.28.3.tar.gz2.解压tar -zxvf nginx-1.28.3.tar.gzcd nginx-1.28.33.配置./configure --prefix/apps/nginx \--usernginx \--groupnginx \--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_module4.编译和编译安装[rootnginx nginx-1.28.3]# make make install5.准备启动[rootnginx nginx-1.28.3]# chown -R nginx:nginx /apps/nginx/[rootnginx nginx-1.28.3]#lltotal 4drwxr-xr-x 2 nginx nginx 4096 Jun 21 21:02 confdrwxr-xr-x 2 nginx nginx 40 Jun 21 21:02 htmldrwxr-xr-x 2 nginx nginx 6 Jun 21 21:02 logsdrwxr-xr-x 2 nginx nginx 19 Jun 21 21:02 sbin[rootnginx nginx-1.28.3]# ln -s /apps/nginx/sbin/nginx /usr/sbin/[rootnginx nginx-1.28.3]# nginx -v ##查看版本[rootnginx nginx-1.28.3]# nginx ##启动nginx服务[rootnginx nginx-1.28.3]# ss -tulp |grep nginxtcp LISTEN 0 511 0.0.0.0:http 0.0.0.0:* users:((nginx,pid22387,fd6),(nginx,pid22386,fd6))6.通过systemd管理服务[rootnginx nginx-1.28.3]# cat EOF /usr/lib/systemd/system/nginx.service[Unit]Descriptionnginx - high performance web serverDocumentationhttp://nginx.org/en/docsAfternetwork-online.target remote-fs.target nss-lookup.targetWantsnetwork-online.target[Service]TypeforkingPIDFile/apps/nginx/run/nginx.pidExecStart/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.confExecReload/bin/kill -s HUP $MAINPIDExecStop/bin/kill -s TERM $MAINPIDLimitNOFILE100000[Install]WantedBymulti-user.targetEOF[rootnginx nginx-1.28.3]# mkdir /apps/nginx/run[rootnginx nginx-1.28.3]# vim /apps/nginx/conf/nginx.conf# 取消注释修改配置文件pid /apps/nginx/run/nginx.pid;systemd服务来启动[rootnginx nginx-1.28.3]# systemctl daemon-reload[rootnginx nginx-1.28.3]# systemctl enable nginxCreated symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.[rootnginx nginx-1.28.3]# systemctl status nginx平滑升级实战平滑升级的实用和优势能在不中断服务、业务、不影响客户端连接的情况下将nginx主程序升级到新版本。这是nginx在生产环境的一个重要特性。特点新连接交给新版本就连接交给旧版本不会抢占请求。升级过程中1.网站继续访问2.API继续相应3.TCP连接不断开4.用户几乎感觉不到升级前提需要编译安装好了的新旧版本的nginx作者所使用的旧版本是1.26.3替换的新版本是1.30.3。资源包可以去官网下载nginx: download1.30.3nginx版本节点编译安装[rootlocalhost ~]# cd /apps/nginx/[rootlocalhost nginx]# lsclient_body_tempfastcgi_templogsrunscgi_tempconfhtmlproxy_tempsbinuwsgi_temp[rootlocalhost nginx]# cd sbin/[rootlocalhost sbin]# lsnginx[rootlocalhost sbin]# cp nginx nginx.bak将编译好的新版本的nginx拷贝到旧节点1.26.3nginx编译安装上scp nginx.bak 192.168.57.105:/apps/nginx/sbin/在旧版本的节点上将刚刚传送过来的文件替换mv nginx.bak nginxmv: overwrite nginx? y[rootlocalhost sbin]# lsnginx[rootlocalhost sbin]# chown nginx.nginx nginx[rootlocalhost sbin]# lltotal 5812-rwxr-xr-x 1 nginx nginx 5948936 Jul 16 23:01 nginx此时查看版本发现版本从1.26.3从1.30.3[rootlocalhost sbin]# nginx -Vnginx version: nginx/1.30.3built by gcc 11.5.0 20240719 (Red Hat 11.5.0-14) (GCC)built with OpenSSL 3.5.5 27 Jan 2026TLS SNI support enabledconfigure arguments: --prefix/apps/nginx --usernginx --groupnginx --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至此已完成升级