# 如何在云服务器上安装Nginx

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/38197.html | 发布：2026-05-09 | 更新：2026-05-09 | 分类：投稿分享

## 一、先确认你的系统

  

登录服务器后输入下面命令看系统：

  

bash

 

运行

 

```
cat /etc/os-release
```

  

会显示 **Ubuntu** 或 **CentOS**。

    

## 二、Ubuntu 系统安装 Nginx

  

## 1. 更新软件源

  

bash

 

运行

 

```
sudo apt update -y
```

  

## 2. 安装 Nginx

  

bash

 

运行

 

```
sudo apt install nginx -y
```

  

## 3. 启动并设置开机自启

  

bash

 

运行

 

```
sudo systemctl start nginx
sudo systemctl enable nginx
```

  

## 4. 查看是否运行成功

  

bash

 

运行

 

```
sudo systemctl status nginx
```

  

看到 **active (running)** 就是成功。

    

## 三、CentOS 7 / 8 安装 Nginx

  

## 1. 安装 EPEL 源

  

bash

 

运行

 

```
sudo yum install epel-release -y
```

  

## 2. 安装 Nginx

  

bash

 

运行

 

```
sudo yum install nginx -y
```

  

## 3. 启动 + 开机自启

  

bash

 

运行

 

```
sudo systemctl start nginx
sudo systemctl enable nginx
```

  

## 4. 查看状态

  

bash

 

运行

 

```
sudo systemctl status nginx
```

    

## 四、开放防火墙端口

  

## Ubuntu：

  

bash

 

运行

 

```
sudo ufw allow 80
sudo ufw allow 443
```

  

## CentOS：

  

bash

 

运行

 

```
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
```

    

## 五、云服务器安全组放行 80/443 端口

  

无论你用阿里云、腾讯云、华为云，**必须在控制台安全组里开放 80 和 443 端口**，否则外网无法访问。

  

步骤：

  
 
1. 进入云服务器控制台 
1. 找到 **安全组 / 防火墙** 
1. 添加规则： 
 
  - 协议：TCP 
  - 端口：80、443 
  - 对象：0.0.0.0/0 

    

## 六、测试是否安装成功

  

浏览器直接输入你的 **服务器公网 IP**，例如：

  

plaintext

 

```
http://123.123.123.123
```

  

出现 **Nginx 默认欢迎页** = 安装成功 ✅

    

## 七、常用 Nginx 命令

  

bash

 

运行

 

```
启动：sudo systemctl start nginx
停止：sudo systemctl stop nginx
重启：sudo systemctl restart nginx
重载配置：sudo systemctl reload nginx
查看状态：sudo systemctl status nginx
```

    

## 八、网站文件放哪里

  

默认网站目录：

  
 
- Ubuntu：`/var/www/html/` 
- CentOS：`/usr/share/nginx/html/` 

  

把 `index.html` 放进去就能访问。

    

### 总结

  
 
1. 先看系统 → Ubuntu/CentOS 
1. 复制安装命令 
1. 启动 + 开机自启 
1. 防火墙放行 80/443 
1. 云控制台安全组放行 80/443 
1. 浏览器访问 IP 测试

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
