centos

CentOS下如何安装Nginx

小樊
66
2025-04-14 20:30:44
栏目: 智能运维

在CentOS系统上安装Nginx可以通过以下步骤完成。以下指南适用于CentOS 7和CentOS 8。

CentOS 7

  1. 更新系统包

    sudo yum update -y
    
  2. 安装EPEL仓库 EPEL(Extra Packages for Enterprise Linux)仓库提供了许多额外的软件包,包括Nginx。

    sudo yum install epel-release -y
    
  3. 安装Nginx

    sudo yum install nginx -y
    
  4. 启动Nginx服务

    sudo systemctl start nginx
    
  5. 设置Nginx开机自启动

    sudo systemctl enable nginx
    
  6. 检查Nginx状态

    sudo systemctl status nginx
    
  7. 配置防火墙 如果你使用的是firewalld,需要允许HTTP和HTTPS流量。

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

CentOS 8

  1. 更新系统包

    sudo dnf update -y
    
  2. 安装Nginx

    sudo dnf install nginx -y
    
  3. 启动Nginx服务

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

    sudo systemctl enable nginx
    
  5. 检查Nginx状态

    sudo systemctl status nginx
    
  6. 配置防火墙 如果你使用的是firewalld,需要允许HTTP和HTTPS流量。

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

验证安装

打开浏览器,访问服务器的IP地址或域名,你应该能看到Nginx的默认欢迎页面。

配置文件

Nginx的主要配置文件位于 /etc/nginx/nginx.conf。你可以根据需要编辑这个文件或创建新的配置文件在 /etc/nginx/conf.d/ 目录下。

例如,创建一个新的配置文件 /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

然后重新加载Nginx配置:

sudo systemctl reload nginx

这样,你就成功地在CentOS上安装并配置了Nginx。

0
看了该问题的人还看了