CentOS 7安装最新nginx
- 清理旧包 / 更新系统
sudo yum remove -y nginx epel-release # 如曾装过旧版或 EPEL 先卸掉
sudo yum update -y
- 导入官方仓库(永远拿到最新稳定版)
sudo tee /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
- 安装并启动
sudo yum install -y nginx
sudo systemctl enable --now nginx
- 防火墙放通 80/443(若 firewalld 开启)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
- 验证版本与状态
nginx -v # 例:nginx/1.26.2
systemctl status nginx # 应为 active (running)
浏览器访问 http://<服务器IP>
出现 “Welcome to nginx!” 即完成。
- 后续常用指令
sudo nginx -t # 修改配置后语法检查
sudo systemctl reload nginx
sudo yum upgrade nginx # 以后直接升级即可保持最新
附:如果想自己编译最新主线版,可参考 的编译参数;但生产环境建议直接用官方仓库方式,升级最省心。