Nginx 从零开始部署网站(小白教程)

2025-12-20 10 分钟阅读

Nginx 从零开始部署网站(小白教程)

前提准备

你需要:

  1. 一台云服务器(阿里云/腾讯云/华为云都行)
  2. 能用 SSH 连接到服务器
  3. 服务器系统是 CentOS 或 Ubuntu

第一步:连接服务器

Windows 用户

打开 CMD 或 PowerShell,输入:

ssh root@你的服务器IP # 例如:ssh root@123.45.67.89

输入密码后回车,看到命令提示符说明连接成功。

Mac 用户

打开终端,输入同样的命令。

第二步:安装 Nginx

如果你的服务器是 CentOS

直接复制粘贴这两行命令:

sudo yum update -y sudo yum install nginx -y

解释:

  • 第一行:更新系统软件包
  • 第二行:安装 Nginx(-y 表示自动确认,不用手动输入 yes)

如果你的服务器是 Ubuntu

直接复制粘贴这两行命令:

sudo apt update sudo apt install nginx -y

等待安装完成(大概 30 秒到 1 分钟)。

第三步:启动 Nginx

输入这个命令:

sudo systemctl start nginx

检查是否启动成功:

sudo systemctl status nginx

如果看到绿色的 "active (running)",说明成功了。

验证方法 2:

打开浏览器,输入 http://你的服务器IP(例如 http://123.45.67.89),如果看到 "Welcome to nginx!" 页面,说明安装成功!

第四步:准备你的网站文件

假设你有一个网站,包含这些文件:

我的网站/
  ├── index.html
  ├── style.css
  ├── script.js
  └── images/
      └── logo.png

第五步:上传网站文件到服务器

方法 1:使用 FileZilla Client(推荐小白)

  1. 下载 FileZilla Client:https://filezilla-project.org/download.php?type=client

    • 点击 "Download FileZilla Client"
    • 选择免费版本(Free)下载安装
  2. 打开 FileZilla,连接服务器:

    • 主机:填写你的服务器 IP(例如:123.45.67.89)
    • 用户名:填 root
    • 密码:填你的服务器密码
    • 端口:填 22
    • 点击"快速连接"
  3. 连接成功后界面说明:

    • 左边区域:你的电脑文件
    • 右边区域:服务器文件
  4. 创建网站目录:

    • 在右边(服务器)找到根目录 /
    • 找到 var 文件夹,双击进入
    • 找到 www 文件夹(如果没有就右键新建目录)
    • www 里右键 → 创建目录 → 输入 mywebsite
  5. 上传网站文件:

    • 双击进入 /var/www/mywebsite/ 目录
    • 在左边(你的电脑)找到你的网站文件夹
    • 选中所有文件(index.htmlstyle.css 等)
    • 右键 → 上传,或直接拖到右边
  6. 等待上传完成:

    • 下方会显示上传进度
    • 全部完成后,右边应该能看到你的文件

方法 2:用命令创建目录(需要在服务器上操作)

如果你已经连上服务器,输入:

# 创建网站目录 sudo mkdir -p /var/www/mywebsite # 进入这个目录 cd /var/www/mywebsite

然后用 FileZilla 把文件上传到这个目录。

重要:确保你的网站目录里有 index.html 文件!

第六步:配置 Nginx(核心步骤)

1. 创建配置文件

在服务器上输入:

sudo vim /etc/nginx/conf.d/mywebsite.conf

i 键进入编辑模式(左下角会显示 INSERT)

2. 复制粘贴以下内容

server { listen 80; server_name 123.45.67.89; # 改成你的服务器IP或域名 root /var/www/mywebsite; # 你的网站文件路径 index index.html; location / { try_files $uri $uri/ /index.html; } }

配置说明:

  • listen 80; - 监听 80 端口(HTTP 默认端口)
  • server_name - 你的服务器 IP 或域名
  • root - 网站文件存放位置
  • index - 首页文件名

3. 保存并退出

  1. Esc 键退出编辑模式
  2. 输入 :wq 然后回车(w=保存,q=退出)

如果写错了想放弃:按 Esc,然后输入 :q! 回车(强制退出不保存)

第七步:测试并重启 Nginx

1. 测试配置文件是否正确

sudo nginx -t

如果看到:

nginx: configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

说明配置没问题!

2. 重启 Nginx 使配置生效

sudo systemctl restart nginx

第八步:开放防火墙端口

CentOS 系统

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

Ubuntu 系统

sudo ufw allow 80/tcp sudo ufw reload

第九步:访问你的网站

打开浏览器,输入:http://你的服务器IP

例如:http://123.45.67.89

如果看到你的网站内容,恭喜你成功了!

常见问题解决

问题 1:浏览器显示 403 Forbidden

原因:Nginx 没有权限读取你的文件

解决办法:

sudo chmod -R 755 /var/www/mywebsite

这个命令是给文件设置读取权限。

问题 2:浏览器显示 404 Not Found

原因:文件路径不对或者没有 index.html

检查方法:

# 查看目录里有什么文件 ls -la /var/www/mywebsite

确保有 index.html 文件。

问题 3:网站打不开

检查清单:

  1. Nginx 是否在运行:
sudo systemctl status nginx

如果没运行,启动它:

sudo systemctl start nginx
  1. 防火墙是否开放 80 端口:

参考第八步重新执行一遍。

  1. 云服务器安全组是否开放:
  • 登录阿里云/腾讯云控制台
  • 找到你的服务器实例
  • 进入"安全组配置"
  • 添加规则:允许入站 80 端口

问题 4:修改配置后没效果

解决办法:

# 先测试配置 sudo nginx -t # 重新加载配置 sudo systemctl reload nginx

常用命令速查

# 启动 Nginx sudo systemctl start nginx # 停止 Nginx sudo systemctl stop nginx # 重启 Nginx sudo systemctl restart nginx # 查看 Nginx 状态 sudo systemctl status nginx # 测试配置文件 sudo nginx -t # 查看错误日志(排查问题用) sudo tail -f /var/log/nginx/error.log

进阶:绑定域名

如果你有域名(比如 www.mywebsite.com):

1. 域名解析

登录你的域名服务商(阿里云/腾讯云等):

  1. 找到"域名解析"
  2. 添加一条 A 记录
  3. 主机记录填 @www
  4. 记录值填你的服务器 IP

等待 10 分钟生效。

2. 修改 Nginx 配置

把之前配置文件里的:

server_name 123.45.67.89;

改成:

server_name www.mywebsite.com mywebsite.com;

保存后重启 Nginx。

总结(完整流程)

  1. SSH 连接服务器
  2. 安装 Nginx:sudo yum install nginx -y
  3. 启动 Nginx:sudo systemctl start nginx
  4. 创建网站目录:sudo mkdir -p /var/www/mywebsite
  5. 用 FileZilla 上传网站文件到这个目录
  6. 创建配置文件:sudo vim /etc/nginx/conf.d/mywebsite.conf
  7. 写入配置(复制本文的配置模板)
  8. 测试配置:sudo nginx -t
  9. 重启 Nginx:sudo systemctl restart nginx
  10. 开放防火墙:sudo firewall-cmd --permanent --add-service=http
  11. 开放云服务器安全组(在控制台操作)
  12. 浏览器访问:http://你的IP

完成!