直接用系统包管理器安装Nginx最省心,Ubuntu/Debian执行sudo apt update && sudo apt install nginx -y,CentOS/RHEL 8+用sudo dnf install nginx -y,7需先装epel-release;随后启动服务、设开机自启、检查端口监听、配置防火墙、验证响应及确认配置路径。
直接用系统包管理器安装是最省心的方式,适合绝大多数场景,尤其新手或快速上线需求。更新软件源并安装 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 欢迎页。
Nginx 已在默认仓库中,无需额外添加源:
sudo dnf install nginx -y
启动并启用开机自启:
sudo systemctl start nginx && sudo systemctl enable nginx
需先启用 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 端口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系)