🌐 阿里云 Linux 服务器 Let's Encrypt 免费 SSL 证书完整部署指南

153 阅读4分钟

🌐 阿里云 Linux 服务器 Let's Encrypt 免费 SSL 证书完整部署指南

适用系统:Alibaba Cloud Linux 3(兼容 CentOS/RHEL)
Web 服务器:Nginx
更新时间:2025 年 11 月
作者:DevOps Guide


✅ 一、前提条件

在开始前,请确保满足以下条件:

要求说明
1. 阿里云 ECS 实例已创建,操作系统为 Alibaba Cloud Linux 3
2. 域名已解析yourdomain.com 的 A 记录指向 ECS 公网 IP
3. 安全组开放端口入方向允许 80 (HTTP)443 (HTTPS)(来源:0.0.0.0/0
4. 域名备案(中国大陆地域)若 ECS 位于中国内地(如杭州、北京),必须完成 ICP 备案
5. 已安装 Nginx且能通过 http://yourdomain.com 访问

🔍 验证域名解析:

dig yourdomain.com +short
# 应返回你的 ECS 公网 IP

🚀 二、完整操作流程

步骤 1:安装 Nginx(如未安装)

# 安装 Nginx
sudo dnf install -y nginx

# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx

步骤 2:配置 Nginx 站点(添加 server_name)

sudo vim /etc/nginx/conf.d/yourdomain.com.conf

写入以下内容:

server {
    listen 80;
    server_name yourdomain.com;  # ← 必须包含你要申请证书的域名
    root /usr/share/nginx/html;
    index index.html;
}

测试并重载:

sudo nginx -t
sudo systemctl reload nginx

✅ 此时应能通过浏览器访问 http://yourdomain.com


步骤 3:安装 Certbot

# 安装 EPEL 仓库
sudo dnf install -y epel-release

# 安装 Certbot 及 Nginx 插件
sudo dnf install -y certbot python3-certbot-nginx

步骤 4:申请并安装 SSL 证书

sudo certbot --nginx -d yourdomain.com

执行时会提示:

  • 输入邮箱(用于过期提醒)
  • 同意服务条款(按 A
  • 是否重定向 HTTP → HTTPS(建议选 Yes

✅ 成功后,Nginx 会自动启用 HTTPS,访问 https://yourdomain.com 应显示安全锁图标。


步骤 5:验证证书信息

sudo certbot certificates

输出示例:

Certificate Name: yourdomain.com
    Expiry Date: 2026-02-20 12:34:56+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/yourdomain.com/privkey.pem

步骤 6:配置自动续期(关键!)

6.1 测试续期流程(安全,不会真续)
sudo certbot renew --dry-run

✅ 应看到:Congratulations, all simulated renewals succeeded.

6.2 设置定时任务
sudo crontab -e

在打开的编辑器中i 进入插入模式,粘贴以下内容:

0 2 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"

保存退出:

  • ESC
  • 输入 :wq 并回车
6.3 验证 cron 是否设置成功
sudo crontab -l

应输出:

0 2 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"

🔍 三、常见问题排查

❌ 问题 1:申请证书时超时(Timeout during connect)

错误示例

Fetching http://yourdomain.com/.well-known/acme-challenge/...: Timeout

原因与解决

原因解决方案
阿里云安全组未开 80 端口控制台 → 安全组 → 添加 80 入站规则
域名未备案(中国内地 ECS)备案域名,或改用 DNS 验证(见附录)
DNS 未解析到公网 IP检查 A 记录:dig yourdomain.com
本地防火墙阻止检查:sudo firewall-cmd --list-ports(Alibaba Cloud Linux 默认关闭)

❌ 问题 2:Certbot 找不到 server_name

错误No matching server blocks located

解决

  • 确保 Nginx 配置中 server_name 精确包含 yourdomain.com
  • 配置文件必须在 /etc/nginx/conf.d/ 或被 nginx.confinclude 包含

❌ 问题 3:自动续期失败

排查步骤

# 查看 cron 执行日志
sudo grep CRON /var/log/cron

# 查看 certbot 日志
sudo tail -n 20 /var/log/letsencrypt/letsencrypt.log

# 手动运行续期(不静默)
sudo certbot renew

📎 附录:替代方案 —— DNS 验证(无需 80 端口)

适用于:未备案域名无法开放 80 端口 的场景

步骤 A:获取阿里云 AccessKey

  1. 进入 RAM 控制台
  2. 创建用户,授权 AliyunDNSFullAccess
  3. 获取 AccessKey IDAccessKey Secret

步骤 B:配置 DNS 插件

# 安装插件
sudo dnf install -y certbot-dns-alidns

# 创建凭证文件
mkdir -p ~/.secrets
cat > ~/.secrets/alidns.ini <<EOF
dns_alidns_access_key = YOUR_ACCESS_KEY_ID
dns_alidns_secret_key = YOUR_ACCESS_KEY_SECRET
EOF

chmod 600 ~/.secrets/alidns.ini

步骤 C:申请证书

sudo certbot certonly \
  --dns-alidns \
  --dns-alidns-credentials ~/.secrets/alidns.ini \
  -d yourdomain.com

步骤 D:手动配置 Nginx HTTPS

编辑 /etc/nginx/conf.d/yourdomain.com.conf,添加:

server {
    listen 443 ssl;
    server_name yourdomain.com;
    root /usr/share/nginx/html;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
}

重载 Nginx:

sudo nginx -t && sudo systemctl reload nginx

⚠️ DNS 验证方式 仍需配置自动续期 cron(同上)


✅ 四、总结

步骤命令/操作状态
1. 安装 Nginxsudo dnf install nginx
2. 配置站点创建 /etc/nginx/conf.d/*.conf
3. 安装 Certbotsudo dnf install certbot...
4. 申请证书sudo certbot --nginx -d yourdomain.com
5. 设置自动续期sudo crontab -e + 添加任务
6. 测试续期sudo certbot renew --dry-run

🎉 完成!你的网站现在拥有免费、自动更新的 HTTPS 加密。


🔗 官方参考

💡 提示:每 3 个月手动运行一次 sudo certbot certificates 检查到期时间,确保万无一失。