最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Nginx 如何使用系统包管理器快速安装 Nginx 服务
时间:2026-08-20 11:51:49 编辑:袖梨 来源:一聚教程网
直接用系统包管理器安装Nginx最省心,Ubuntu/Debian执行sudo apt update && sudo apt install nginx -y,CentOS/RHEL 8+用sudo dnf install nginx -y,7需先装epel-release;随后启动服务、设开机自启、检查端口监听、配置防火墙、验证响应及确认配置路径。
直接用系统包管理器安装是最省心的方式,适合绝大多数场景,尤其新手或快速上线需求。Ubuntu/Debian 系统:一行命令搞定
更新软件源并安装 Nginx:
sudo apt update && sudo apt install nginx -y
装完后启动服务并设为开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
验证是否运行正常:
sudo systemctl status nginx
看到 active (running) 就说明服务已就绪。默认配置下,访问服务器 IP 或 localhost 就能看到 Nginx 欢迎页。
CentOS/RHEL 8+(含 Rocky、AlmaLinux)
Nginx 已在默认仓库中,无需额外添加源:
sudo dnf install nginx -y
启动并启用开机自启:
sudo systemctl start nginx && sudo systemctl enable nginx
CentOS/RHEL 7
需先启用 EPEL 扩展源(提供更新版 Nginx):
sudo yum install epel-release -y
sudo yum install nginx -y
同样执行:
sudo systemctl start nginx
sudo systemctl enable nginx
安装后必须确认的几件事
-
检查端口监听:运行
ss -tlnp | grep :80,确认 nginx 进程正在监听 80 端口 -
防火墙放行:若启用 firewalld,执行
sudo firewall-cmd --permanent --add-service=http并重载规则;若用 ufw(Ubuntu),运行sudo ufw allow 'Nginx HTTP' -
验证响应:终端执行
curl -I http://localhost,返回HTTP/1.1 200 OK表示服务正常 -
配置路径固定:主配置文件位于
/etc/nginx/nginx.conf,站点配置通常放在/etc/nginx/sites-enabled/(Debian系)或/etc/nginx/conf.d/(RHEL系)