nginx安装与升级
nginx安装与升级
1. 安装
在 Linux 中安装软件有三种方式:
- rpm 方式安装
- dnf 方式安装
- 源码安装
1.1 rpm 安装
rpm -i --install 安装-v --verbose 显示-e --erase 删除-q 查询-a 查询软件包-l 查询软件安装位置-c 查询软件的配置文件所在目录
使用:通过 rpm 方式来安装 nginx
1)下载安装文件
# 1. 把 rpm 安装文件下载到本地
[root@client ~]# wget https://nginx.org/packages/rhel/9/x86_64/RPMS/nginx-1.26.3-1.el9.ngx.x86_64.rpm
--2025-11-02 15:37:08-- https://nginx.org/packages/rhel/9/x86_64/RPMS/nginx-1.26.3-1.el9.ngx.x86_64.rpm
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2600::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1020824 (997K) [application/x-redhat-package-manager]
Saving to: ‘nginx-1.26.3-1.el9.ngx.x86_64.rpm’nginx-1.26.3-1.el9.ngx.x86_64.r 100%[====================================================>] 996.90K 20.3KB/s in 47s 2025-11-02 15:37:57 (21.2 KB/s) - ‘nginx-1.26.3-1.el9.ngx.x86_64.rpm’ saved [1020824/1020824]
2)安装nginx
[root@client ~]# rpm -ivh nginx-1.26.3-1.el9.ngx.x86_64.rpm
warning: nginx-1.26.3-1.el9.ngx.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY
----------------------------------------------------------------------Thanks for using nginx!Please find the official documentation for nginx here:
* https://nginx.org/en/docs/Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.htmlCommercial subscriptions for nginx are available on:
* https://nginx.com/products/----------------------------------------------------------------------
3)启动服务
[root@client ~]# systemctl start nginx
[root@client ~]# systemctl status nginx
● nginx.service - nginx - high performance web serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)Active: active (running) since Sun 2025-11-02 15:39:33 CST; 17s agoDocs: http://nginx.org/en/docs/Process: 1446 ExecStart=/usr/sbin/nginx -c ${conffile} (code=exited, status=0/SUCCESS)Main PID: 1447 (nginx)Tasks: 3 (limit: 12067)Memory: 3.3MCPU: 10msCGroup: /system.slice/nginx.service├─1447 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"├─1448 "nginx: worker process"└─1449 "nginx: worker process"Nov 02 15:39:33 client systemd[1]: Starting nginx - high performance web server...
Nov 02 15:39:33 client systemd[1]: Started nginx - high performance web server.
4)访问测试
[root@client ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
可以发现在服务器内部是可以访问的,说明服务是没有问题的。
5)访问端口
如果外部需要访问的话,我们就需要将 80 端口开放,因为 nginx 服务器默认监听的是 80 端口。
[root@client ~]# firewall-cmd --permanent --add-port=80/tcp
success
[root@client ~]# firewall-cmd --reload
success
6)卸载nginx
# 1. 停止服务
[root@client ~]# systemctl stop nginx# 2. 卸载nginx
[root@client ~]# rpm -e nginx# 3. 删除以下两个文件
rm -rf /var/cache/nginx
rm -rf /var/log/nginx# 4. 删除用户
[root@client ~]# userdel -r nginx
1.2 dnf 安装
dnf 安装的命令回顾:
dnf -y install 安装remove 卸载search 查询provides 查看软件是由哪个包提供update 更新
1、将 nginx 的仓库配置到本地
[root@client ~]# vim /etc/yum.repos.d/nginx.repo
文件内容如下:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2、安装 nginx
[root@client ~]# dnf install nginx
3、启动服务
[root@client ~]# systemctl start nginx
[root@client ~]# systemctl status nginx
● nginx.service - nginx - high performance web serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)Active: active (running) since Sun 2025-11-02 15:55:59 CST; 6s agoDocs: http://nginx.org/en/docs/Process: 1804 ExecStart=/usr/sbin/nginx -c ${conffile} (code=exited, status=0/SUCCESS)Main PID: 1805 (nginx)Tasks: 3 (limit: 12067)Memory: 3.3MCPU: 11msCGroup: /system.slice/nginx.service├─1805 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"├─1806 "nginx: worker process"└─1807 "nginx: worker process"Nov 02 15:55:59 client systemd[1]: Starting nginx - high performance web server...
Nov 02 15:55:59 client systemd[1]: Started nginx - high performance web server.
4、访问服务
[root@client ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
5、查看服务文件
[root@client ~]# cat /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=/run/nginx.pid
Environment="conffile=/etc/nginx/nginx.conf"
EnvironmentFile=-/etc/sysconfig/nginx
ExecStart=/usr/sbin/nginx -c ${conffile}
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /run/nginx.pid)"[Install]
WantedBy=multi-user.target
6、卸载nginx
# 1. 停止服务
[root@client ~]# systemctl stop nginx# 2. 卸载nginx
[root@client ~]# dnf remove nginx -y# 3. 删除目录
[root@client ~]# rm -rf /var/log/nginx/
[root@client ~]# rm -rf /var/cache/nginx/# 4. 删除用户
[root@client ~]# userdel -r nginx
1.3 源码安装
1、安装编译环境
[root@client ~]# dnf install -y gcc-c++ pcre-devel openssl-devel zlib-devel
2、下载要安装的源码包
[root@client ~]# wget https://nginx.org/download/nginx-1.26.3.tar.gz
--2025-11-02 16:10:52-- https://nginx.org/download/nginx-1.26.3.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:5c0:2601::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1260179 (1.2M) [application/octet-stream]
Saving to: ‘nginx-1.26.3.tar.gz’nginx-1.26.3.tar.gz 100%[====================================================>] 1.20M 21.8KB/s in 55s 2025-11-02 16:11:49 (22.5 KB/s) - ‘nginx-1.26.3.tar.gz’ saved [1260179/1260179][root@client ~]# ls
nginx-1.26.3.tar.gz
3、解压源码包
[root@client ~]# tar -zxf nginx-1.26.3.tar.gz -C /usr/local/src/[root@client ~]# ls /usr/local/src/
nginx-1.26.3
4、进入解压目录
[root@client ~]# cd /usr/local/src/nginx-1.26.3/
[root@client nginx-1.26.3]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
5、对nginx进行配置
# 1. 创建用户
[root@client nginx-1.26.3]# useradd -M -r -s /sbin/nolgin nginx# 2. 配置nginx
[root@client nginx-1.26.3]# ./configure \
--prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module.....
Configuration summary+ using system PCRE library+ using system OpenSSL library+ using system zlib librarynginx path prefix: "/opt/nginx"nginx binary file: "/opt/nginx/sbin/nginx"nginx modules path: "/opt/nginx/modules"nginx configuration prefix: "/opt/nginx/conf"nginx configuration file: "/opt/nginx/conf/nginx.conf"nginx pid file: "/opt/nginx/logs/nginx.pid"nginx error log file: "/opt/nginx/logs/error.log"nginx http access log file: "/opt/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"[root@client nginx-1.26.3]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
6、进行编译和安装
[root@client nginx-1.26.3]# make && make install
7、启动服务
[root@client nginx-1.26.3]# systemctl start nginx
Failed to start nginx.service: Unit nginx.service not found.
由于是源码安装,所以不会自动帮我们创建 /usr/lib/systemd/system/nginx.service 服务文件,因此不能通过 systemctl 来管理 nginx,只能通过 nginx 的二进制可执行文件来启动、停止 nginx 服务。
# 1. 启动服务
[root@client nginx]# /opt/nginx/sbin/nginx# 2. 查看进程
[root@client nginx]# ps -auxf | grep nginx
root 8049 0.0 0.1 6408 2176 pts/1 S+ 16:27 0:00 \_ grep --color=auto nginx
root 8043 0.0 0.1 11112 2068 ? Ss 16:26 0:00 nginx: master process /opt/nginx/sbin/nginx
nginx 8044 0.0 0.2 15388 4884 ? S 16:26 0:00 \_ nginx: worker process
8、访问测试
[root@client nginx]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
2. 平滑升级
2.1 数据准备
为了演示在整个平滑升级过程中用户是无感的,我们在 nginx 服务器的根目录下生成一个 10M 大小的文件,然后通过 wget 的方式去下载这个文件。
1、生成文件
[root@client html]# dd if=/dev/zero of=./test.img bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0076798 s, 1.4 GB/s[root@client html]#
[root@client html]# ll
total 10248
-rw-r--r--. 1 root root 497 Nov 2 16:23 50x.html
-rw-r--r--. 1 root root 615 Nov 2 16:23 index.html
-rw-r--r--. 1 root root 10485760 Nov 2 17:03 test.img
2、使用 wget 命令来下载这个文件,由于我们要演示平滑升级,因此我们在进行下载时,限制下载的带宽。
[root@client html]# wget --limit-rate=1K http://localhost/test.img
--2025-11-02 17:07:53-- http://localhost/test.img
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10485760 (10M) [application/octet-stream]
Saving to: ‘test.img.1’test.img.1 0%[ ] 2.25K 1024 B/s
2.2 平滑升级
1、下载nginx新版本
[root@client ~]# cd
[root@client ~]# wget https://nginx.org/download/nginx-1.28.0.tar.gz
--2025-11-02 17:09:00-- https://nginx.org/download/nginx-1.28.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2601::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1280111 (1.2M) [application/octet-stream]
Saving to: ‘nginx-1.28.0.tar.gz’nginx-1.28.0.tar.gz 100%[====================================================>] 1.22M 23.3KB/s in 37s 2025-11-02 17:09:39 (33.4 KB/s) - ‘nginx-1.28.0.tar.gz’ saved [1280111/1280111][root@client ~]# ls
nginx-1.26.3.tar.gz nginx-1.28.0.tar.gz
2、解压下载的文件
[root@client ~]# tar -zxf nginx-1.28.0.tar.gz -C /usr/local/src/
3、进入到解压目录
[root@client ~]# cd /usr/local/src/nginx-1.28.0/
[root@client nginx-1.28.0]# ls
auto CHANGES.ru conf contrib html man SECURITY.md
CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md src
4、配置新版本的nginx
[root@client nginx-1.28.0]# ./configure \
--prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module.....
Configuration summary+ using system PCRE library+ using system OpenSSL library+ using system zlib librarynginx path prefix: "/opt/nginx"nginx binary file: "/opt/nginx/sbin/nginx"nginx modules path: "/opt/nginx/modules"nginx configuration prefix: "/opt/nginx/conf"nginx configuration file: "/opt/nginx/conf/nginx.conf"nginx pid file: "/opt/nginx/logs/nginx.pid"nginx error log file: "/opt/nginx/logs/error.log"nginx http access log file: "/opt/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"
5、编译新版本的nginx,需要注意:只执行 make 命令,不执行 make install
[root@client nginx-1.28.0]# make
编译完成后,会在当前源码目录下的 objs 目录下生成可执行的二进制文件:
[root@client nginx-1.28.0]# ls
auto CHANGES.ru conf contrib html Makefile objs SECURITY.md
CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE man README.md src
[root@client nginx-1.28.0]# ls objs
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
我们通过这个文件来查看其版本。
[root@client nginx-1.28.0]# objs/nginx -v
nginx version: nginx/1.28.0
我们当前所使用的是旧版本(1.26.3版本):
[root@client nginx-1.28.0]# /opt/nginx/sbin/nginx -v
nginx version: nginx/1.26.3
查看一下当前的进程信息:
[root@client nginx-1.28.0]# ps auxf | grep nginx
root 11222 0.0 0.1 6408 2176 pts/0 S+ 17:17 0:00 | \_ grep --color=auto nginx
root 8043 0.0 0.1 11112 2068 ? Ss 16:26 0:00 nginx: master process /opt/nginx/sbin/nginx
nginx 8044 0.0 0.2 15388 5268 ? S 16:26 0:00 \_ nginx: worker process
6、备份旧版本的可执行二进制文件
[root@client nginx-1.28.0]# mv /opt/nginx/sbin/nginx /root/nginx
7、将编译所得到的新版本的可执行二进制文件复制到 /opt/nginx/sbin/ 目录(将新版本替换旧版本)
[root@client nginx-1.28.0]# cp -f objs/nginx /opt/nginx/sbin/
8、发送信号 USR2 平滑升级可执行程序,将存储有旧版本主进程 PID 的文件重命令为 nginx.pid.oldbin(执行命令后自动生成的),并启动新的 nginx。
# 1. 查看当前日志目录文件
[root@client nginx]# cd /opt/nginx/logs/
[root@client logs]# ls
access.log error.log nginx.pid# 2. 查看当前 nginx 服务的 master 进程号
[root@client logs]# cat /opt/nginx/logs/nginx.pid
8043# 3. 发送 USR2 信号给 8043 进程
[root@client logs]# kill -USR2 `cat /opt/nginx/logs/nginx.pid`# 4. 查看日志目录下的文件变化
[root@client logs]# ls
access.log error.log nginx.pid nginx.pid.oldbin# 5. 查看新的进程信息
[root@client nginx-1.28.0]# ps auxf | grep nginx
root 11297 0.0 0.1 6408 2176 pts/0 S+ 17:25 0:00 | \_ grep --color=auto nginx
root 8043 0.0 0.1 11112 2452 ? Ss 16:26 0:00 nginx: master process /opt/nginx/sbin/nginx
nginx 8044 0.0 0.2 15388 5268 ? S 16:26 0:00 \_ nginx: worker process
root 11292 0.0 0.3 11164 6656 ? S 17:24 0:00 \_ nginx: master process /opt/nginx/sbin/nginx
nginx 11293 0.0 0.2 15440 5012 ? S 17:24 0:00 \_ nginx: worker process
注意:在 nginx.pid.oldbin 文件中保存的就是旧版本的 PID 值,即 8043
9、先关闭旧 Nginx 的 worker 进程,而不关闭旧 nginx 的主进程,方便发生问题后回滚向旧 nginx 主进程发送 WINCH 信号,它会平滑关闭旧的工作进程(主进程不会退出),这时所有新请求都会由新版本 nginx 处理
# 1. 查看旧进程 PID
[root@client logs]# cat /opt/nginx/logs/nginx.pid.oldbin
8043# 2. 还是给 8043 进程发送 WINCH 信号来平滑关闭旧进程
[root@client logs]# kill -WINCH `cat /opt/nginx/logs/nginx.pid.oldbin`# 3. 查看此时的进程信息
[root@client nginx-1.28.0]# ps auxf | grep nginx
root 11302 0.0 0.1 6408 2176 pts/0 S+ 17:30 0:00 | \_ grep --color=auto nginx
root 8043 0.0 0.1 11112 2452 ? Ss 16:26 0:00 nginx: master process /opt/nginx/sbin/nginx
root 11292 0.0 0.3 11164 6656 ? S 17:24 0:00 \_ nginx: master process /opt/nginx/sbin/nginx
nginx 11293 0.0 0.2 15440 5012 ? S 17:24 0:00 \_ nginx: worker process
这个时候依然是新旧进程同时存在,但是是由新的进程来处理请示。
10、经过一段时间测试,新版本服务没有问题,最后发送 QUIT 信号,退出旧的 Nginx 的 master,完成全部升级过程
# 1. 向旧版本的进程 PID 发送 QUIT 信号
[root@client logs]# kill -QUIT `cat /opt/nginx/logs/nginx.pid.oldbin`# 2. 再查看目录的进程信息
[root@client nginx-1.28.0]# ps auxf | grep nginx
root 11306 0.0 0.1 6408 2048 pts/0 S+ 17:32 0:00 | \_ grep --color=auto nginx
root 11292 0.0 0.3 11164 6656 ? S 17:24 0:00 nginx: master process /opt/nginx/sbin/nginx
nginx 11293 0.0 0.2 15440 5012 ? S 17:24 0:00 \_ nginx: worker process
此时就只剩下新版本的进程信息了。
[root@client nginx-1.28.0]# /opt/nginx/sbin/nginx -v
nginx version: nginx/1.28.0
至此,nginx 服务器平滑升级完成。
[root@client html]# wget --limit-rate=1K http://localhost/test.img
--2025-11-02 17:07:53-- http://localhost/test.img
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10485760 (10M) [application/octet-stream]
Saving to: ‘test.img.1’test.img.1 7%[===> ] 796.50K 1024 B/s in 13m 17s 2025-11-02 17:21:09 (1024 B/s) - Connection closed at byte 815616. Retrying.--2025-11-02 17:21:10-- (try: 2) http://localhost/test.img
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 10485760 (10M), 9670144 (9.2M) remaining [application/octet-stream]
Saving to: ‘test.img.1’test.img.1 15%[++++===> ] 1.54M 1.00KB/s eta 2h 24m
