网页部署到域名完整指南

从服务器配置到域名绑定的完整部署流程,不放过任何一个细节

📋 前提条件检查

在开始之前,请确保您已经准备好:

  1. 域名:已购买并完成备案的域名(如:yourdomain.com
  2. 服务器:云服务器(阿里云、腾讯云、华为云等)
  3. 服务器信息
    • 公网IP地址
    • SSH登录账号和密码/密钥
    • 操作系统(通常是 Ubuntu 或 CentOS)

第一步:连接到服务器

1 使用SSH连接服务器

Windows用户

ssh root@你的服务器IP地址

Mac/Linux用户

ssh root@你的服务器IP地址

或者使用SSH工具

  • Windows:PuTTY、Xshell、MobaXterm
  • Mac:Terminal、iTerm2
  • 跨平台:VS Code Remote SSH
2 首次连接设置

连接成功后,建议先更新系统:

Ubuntu/Debian 系统

sudo apt update && sudo apt upgrade -y

CentOS/RHEL 系统

sudo yum update -y

第二步:安装和配置 Nginx

1 安装 Nginx

Ubuntu/Debian 系统

sudo apt install nginx -y

CentOS/RHEL 系统

sudo yum install nginx -y
sudo dnf install nginx -y
2 启动并设置 Nginx 开机自启

启动 Nginx

sudo systemctl start nginx

设置开机自启

sudo systemctl enable nginx

检查运行状态

sudo systemctl status nginx
3 配置防火墙

Ubuntu 系统(使用 ufw)

sudo ufw allow 'Nginx Full'
sudo ufw allow ssh
sudo ufw enable

CentOS 系统(使用 firewalld)

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

第三步:上传网页文件

1 创建网站目录

创建网站根目录

sudo mkdir -p /var/www/yourdomain.com

设置目录权限

sudo chown -R $USER:$USER /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
2 上传文件的几种方法

方法一:使用 SCP 命令(推荐)

在您的本地电脑上执行:

scp index.html root@你的服务器IP:/var/www/yourdomain.com/
scp *.html root@你的服务器IP:/var/www/yourdomain.com/

方法二:使用 SFTP 工具

  • Windows:WinSCP、FileZilla
  • Mac:Cyberduck、Transmit
  • 跨平台:FileZilla

连接信息:

  • 协议:SFTP
  • 主机:您的服务器IP
  • 端口:22
  • 用户名:root(或您的用户名)
  • 密码:您的服务器密码
3 确认文件上传成功
ls -la /var/www/yourdomain.com/

应该看到类似这样的文件:

  • index.html
  • tutorial-center.html
  • tutorials.html
  • edgeone-tutorial.html
  • subpage-tutorial.html

第四步:配置 Nginx 虚拟主机

1 创建网站配置文件
sudo nano /etc/nginx/sites-available/yourdomain.com
2 添加配置内容

将以下内容复制到配置文件中(请替换 yourdomain.com 为您的实际域名):

Nginx 配置文件
server {
    listen 80;
    listen [::]:80;

    # 替换为您的实际域名
    server_name yourdomain.com www.yourdomain.com;

    # 网站文件根目录
    root /var/www/yourdomain.com;
    index index.html index.htm;

    # 主要的location块
    location / {
        try_files $uri $uri/ =404;
    }

    # 静态资源缓存优化
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # 防止访问隐藏文件
    location ~ /\. {
        deny all;
    }

    # 防止访问备份文件
    location ~ ~$ {
        deny all;
    }

    # 错误页面
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
}
3 启用网站配置

创建软链接启用网站

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

测试 Nginx 配置

sudo nginx -t

如果测试通过,重载 Nginx

sudo systemctl reload nginx
4 删除默认网站(可选)
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl reload nginx

第五步:配置域名解析

1 登录域名管理后台

登录您购买域名的服务商后台(阿里云、腾讯云、GoDaddy等)

2 添加 DNS 解析记录

添加以下两条 A 记录:

第一条记录
主机记录: @
记录类型: A
记录值: 您的服务器公网IP地址
TTL: 600(或默认值)
第二条记录
主机记录: www
记录类型: A
记录值: 您的服务器公网IP地址
TTL: 600(或默认值)
3 等待 DNS 生效

DNS 解析通常需要 10 分钟到 24 小时生效,国内一般 10-30 分钟即可。

4 测试域名解析
nslookup yourdomain.com
ping yourdomain.com

第六步:测试网站访问

1 浏览器测试

在浏览器中访问:

  • http://yourdomain.com
  • http://www.yourdomain.com
2 测试所有页面

确保以下页面都能正常访问:

  • http://yourdomain.com/ (主页)
  • http://yourdomain.com/tutorial-center.html (教程中心)
  • http://yourdomain.com/tutorials.html (网页搭建教程列表)
  • http://yourdomain.com/edgeone-tutorial.html (EdgeOne教程)
  • http://yourdomain.com/subpage-tutorial.html (二级页面教程)

第七步:配置 HTTPS(SSL证书)

1 安装 Certbot

Ubuntu/Debian

sudo apt install certbot python3-certbot-nginx -y

CentOS/RHEL

sudo yum install certbot python3-certbot-nginx -y
2 申请 SSL 证书
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

按照提示操作:

  1. 输入邮箱地址
  2. 同意服务条款(输入 Y)
  3. 选择是否接收邮件(输入 Y 或 N)
  4. 选择重定向选项(推荐选择 2,强制 HTTPS)
3 设置证书自动续期

测试自动续期

sudo certbot renew --dry-run

添加定时任务

sudo crontab -e

添加以下行(每天检查证书续期):

定时任务
0 12 * * * /usr/bin/certbot renew --quiet

第八步:性能优化(可选)

1 启用 Gzip 压缩

编辑 Nginx 主配置:

sudo nano /etc/nginx/nginx.conf

http 块中添加:

Gzip 配置
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
2 重启 Nginx
sudo systemctl restart nginx

第九步:监控和维护

1 查看 Nginx 日志

查看访问日志

sudo tail -f /var/log/nginx/access.log

查看错误日志

sudo tail -f /var/log/nginx/error.log
2 定期备份

创建备份脚本:

sudo nano /root/backup-website.sh

添加内容:

备份脚本
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
tar -czf /root/website_backup_$DATE.tar.gz /var/www/yourdomain.com/
find /root/website_backup_*.tar.gz -mtime +7 -delete

设置执行权限并添加定时任务:

sudo chmod +x /root/backup-website.sh
sudo crontab -e

添加:每天凌晨2点备份

定时任务
0 2 * * * /root/backup-website.sh

🔍 常见问题排查

问题1:域名无法访问

检查步骤

sudo systemctl status nginx
sudo netstat -tlnp | grep :80
sudo ufw status
问题2:页面显示 403 Forbidden

解决方法

ls -la /var/www/yourdomain.com/
sudo chown -R www-data:www-data /var/www/yourdomain.com/
sudo chmod -R 755 /var/www/yourdomain.com/
问题3:SSL证书申请失败

检查步骤

  1. 确保域名已正确解析到服务器IP
  2. 确保80端口可以访问
  3. 检查防火墙设置

✅ 完成检查清单

部署完成后,请逐一检查:

部署检查清单
  • 服务器可以SSH连接
  • Nginx已安装并运行
  • 网页文件已上传到正确目录
  • Nginx配置文件已创建并启用
  • 域名DNS解析已配置
  • HTTP访问正常(所有页面)
  • SSL证书已配置
  • HTTPS访问正常
  • 页面间链接跳转正常
  • 备份脚本已设置
恭喜您!

完成以上所有步骤后,您的网站就成功绑定到域名了!现在您拥有:

  • ✅ 一个完全可用的网站,绑定到您的域名
  • ✅ HTTPS安全连接
  • ✅ 所有页面都能正常访问和跳转
  • ✅ 自动化的证书续期和网站备份
  • ✅ 优化的性能配置

如果在任何步骤遇到问题,请检查错误日志并参考常见问题排查部分。