Nginx 安装与部署
本指南介绍了在主流 Linux 发行版上安装 Nginx 的标准方法。
📥 安装方法
1. CentOS / RHEL (使用 Yum)
在 CentOS 上,Nginx 位于 EPEL 仓库中。
bash
# 1. 安装 EPEL 仓库
yum install epel-release -y
# 2. 安装 Nginx
yum install nginx -y
# 3. 启动并设置开机自启
systemctl start nginx
systemctl enable nginx2. Ubuntu / Debian (使用 APT)
bash
# 1. 更新包索引
apt update
# 2. 安装 Nginx
apt install nginx -y
# 3. 检查状态
systemctl status nginx3. 使用 Docker 安装
如果你更倾向于容器化部署:
bash
docker run --name my-nginx -p 80:80 -d nginx✅ 验证安装
安装完成后,可以通过以下几种方式验证:
1. 查看版本
bash
nginx -v2. 检查默认页
在浏览器访问服务器的 IP 地址(如 http://192.168.1.100),如果看到 "Welcome to nginx!" 页面,说明安装成功。
3. 检查进程
bash
ps aux | grep nginx🛠️ 常用运维操作
| 动作 | 命令 |
|---|---|
| 启动 | systemctl start nginx |
| 停止 | systemctl stop nginx |
| 重启 | systemctl restart nginx |
| 重载 (不中断服务) | systemctl reload nginx |
| 检查配置语法 | nginx -t |
🧱 防火墙配置 (重要)
如果无法访问,请检查防火墙是否开放了 80 (HTTP) 或 443 (HTTPS) 端口。
CentOS (Firewalld)
bash
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reloadUbuntu (UFW)
bash
ufw allow 'Nginx Full'