插件窝 干货文章 CentOS7系统下如何用YUM安装Nginx

CentOS7系统下如何用YUM安装Nginx

Nginx sudo systemctl 安装 858    来源:    2025-04-23

在CentOS 7系统下使用YUM安装Nginx

标准安装方法

  1. 添加EPEL仓库(如果尚未添加):

    sudo yum install epel-release
    
  2. 安装Nginx

    sudo yum install nginx
    
  3. 启动Nginx服务

    sudo systemctl start nginx
    
  4. 设置开机自启

    sudo systemctl enable nginx
    

安装最新稳定版Nginx

如果需要最新稳定版而非EPEL仓库中的版本:

  1. 添加Nginx官方仓库

    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    
  2. 安装Nginx

    sudo yum install nginx
    
  3. 启动并启用服务(同上)

验证安装

  1. 检查Nginx版本

    nginx -v
    
  2. 检查服务状态

    systemctl status nginx
    
  3. 测试访问: 在浏览器中访问服务器IP地址,应该能看到Nginx欢迎页面。

防火墙配置

如果启用了防火墙,需要开放HTTP(80)和HTTPS(443)端口:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

配置文件位置

  • 主配置文件:/etc/nginx/nginx.conf
  • 默认服务器配置:/etc/nginx/conf.d/default.conf
  • 网站根目录:/usr/share/nginx/html

常用管理命令

  • 启动:sudo systemctl start nginx
  • 停止:sudo systemctl stop nginx
  • 重启:sudo systemctl restart nginx
  • 重载配置:sudo systemctl reload nginx
  • 检查配置语法:sudo nginx -t

通过以上步骤,您应该能够在CentOS 7系统上成功安装并运行Nginx服务。