Ubuntu 22.04 搭建 Nginx 踩坑记录
在阿里云 ECS(Ubuntu 22.04)上搭建 Nginx 静态博客的完整记录。
1. 安装 Nginx
sudo apt update
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
2. 防火墙放行 80 端口
sudo ufw allow 'Nginx HTTP'
sudo ufw enable
sudo ufw status
3. 阿里云安全组
控制台 → ECS → 安全组 → 入方向,添加规则:
- 协议:TCP
- 端口:80/80
- 授权对象:0.0.0.0/0
4. 放置网页
sudo nano /var/www/html/index.html
5. 验证
浏览器访问 http://公网IP/,看到页面即成功。
6. 域名解析
在阿里云域名管理控制台添加 A 记录:
- 记录类型:A
- 主机记录:@
- 记录值:公网 IP
- TTL:600(便于备案验证时快速变更)
备案通过后 TTL 可改回默认 604800(7 天)。
7. ICP 备案
在阿里云 ICP 备案系统提交申请,流程:
- 域名实名通过满 7 天后提交
- 准备材料:身份证正反面 + 手持照 + 手机号 + 站点名称
- 云厂商初审 → 工信部自动关联 → 管局短信核验(24h 内必须确认)→ 管局审核(7-20 工作日)
- 备案号:沪ICP备2026050558号-1
备案通过后,页脚添加备案号链接到 工信部查询页。
8. 配置 HTTPS
8.1 安全组 + 防火墙放行 443
阿里云安全组入方向添加 TCP 443/443,服务器防火墙:
sudo ufw allow 'Nginx Full'
sudo ufw reload
8.2 安装 certbot
sudo apt install -y certbot python3-certbot-nginx
8.3 申请证书(自动配置 Nginx)
sudo certbot --nginx -d pandaai.top
certbot 自动完成:下载证书、修改 Nginx 配置添加 443 监听、配置 80→443 跳转、reload Nginx。
8.4 验证
curl -I http://pandaai.top # 预期 301 跳转
curl -I https://pandaai.top # 预期 200 OK
8.5 Nginx 配置(certbot 自动生成的关键部分)
server {
listen 80;
listen [::]:80;
server_name pandaai.top;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
server_name pandaai.top;
root /var/www/html;
index index.html index.htm;
ssl_certificate /etc/letsencrypt/live/pandaai.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/pandaai.top/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
try_files $uri $uri/ =404;
}
}
8.6 证书自动续期
sudo certbot renew --dry-run # 测试续期流程
# certbot 默认 60 天内自动续期,一般不用管
8.7 可选:启用 HTTP/2
把 listen 443 ssl; 改为 listen 443 ssl http2;,然后 nginx -t && nginx -s reload。验证:curl -I --http2 https://pandaai.top 看到 HTTP/2 200 即生效。
9. 公安联网备案
ICP 备案通过后 30 天内完成。流程:
- 登录 全国互联网安全管理服务平台,个人注册 + 实人认证
- 填写开办主体信息(姓名、身份证号、联系方式)
- 填写网站信息:域名、IP、接入商(阿里云计算有限公司)、网站类型(个人博客)、交互服务(否)、前置许可(否)
- 提交审核,3-5 工作日,短信通知结果
- 审核通过后复制公安机关备案号,嵌入博客页脚
纯静态博客(无注册、无评论、无 UGC)选"非交互",线上审核即完成,无需面审。
后续待办
- 迁移到 Hugo 静态博客生成器
- 配置博客文件定时备份
- 启用 HTTP/2(可选)