直接用系统包管理器安装Nginx最省心,Ubuntu/Debian执行sudo apt update && sudo apt install nginx -y,CentOS/RHEL 8+用sudo dnf install nginx -y,7需先装epel-release;启动并设开机自启后,检查80端口监听、防火墙放行HTTP、curl验证200响应,并注意配置路径差异。

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) 即说明就绪。
默认监听 80 端口,浏览器访问服务器 IP 或 localhost 就能看到欢迎页。
CentOS/RHEL 8+(含 Rocky、AlmaLinux)
Nginx 已在默认仓库中,无需额外添加源:
sudo dnf install nginx -y
启动并启用开机自启:
sudo systemctl start nginx && sudo systemctl enable nginx
CentOS/RHEL 7
需先启用 EPEL 扩展源(提供较新版本):
- sudo yum install epel-release -y
- sudo yum install nginx -y
同样执行:
- sudo systemctl start nginx
- sudo systemctl enable nginx
安装后必须确认的几件事
检查是否监听 80 端口:
ss -tlnp | grep :80
防火墙放行 HTTP 服务:
- firewalld:sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload
- 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系)


















