SSL证书管理完全指南:Let’s Encrypt自动续签、通配符证书与多域名配置实战

HTTPS已是网站的标配,SSL证书的管理却常常被忽视——证书到期后网站显示"不安全"警告,访客大量流失。本文系统梳理从证书申请到自动续签的完整方案,让SSL永不过期。

一、SSL证书类型选择

类型验证方式覆盖范围费用推荐场景
DV证书(域名验证)验证域名所有权单域名或通配符免费(Let's Encrypt)绝大多数网站
OV证书(组织验证)验证公司真实性单域名或多域名每年数百~数千元企业官网,增强信任
EV证书(扩展验证)严格审查组织身份单域名每年数千元金融、电商提升可信度
通配符证书域名验证*.domain.com所有子域Let's Encrypt免费多子域名站点

二、安装Certbot

# Ubuntu 22.04安装Certbot
apt update
apt install certbot python3-certbot-nginx -y

# 验证安装
certbot --version

三、单域名证书申请(HTTP验证)

<code"># Nginx自动配置模式(最简单,推荐)
certbot --nginx -d yourdomain.com -d www.yourdomain.com

# 纯证书申请模式(手动配置Nginx)
certbot certonly --nginx -d yourdomain.com -d www.yourdomain.com

# 证书文件位置:
# /etc/letsencrypt/live/yourdomain.com/fullchain.pem  (证书链)
# /etc/letsencrypt/live/yourdomain.com/privkey.pem    (私钥)

四、通配符证书申请(DNS验证)

通配符证书(*.yourdomain.com)覆盖所有子域名,必须通过DNS TXT记录验证。

<code"># 申请通配符证书
certbot certonly --manual --preferred-challenges dns \
  -d yourdomain.com \
  -d *.yourdomain.com \
  --agree-tos \
  --email your@email.com

执行后会提示你在DNS中添加一条TXT记录,例如:

<code"># 在DNS管理后台添加:
名称:_acme-challenge.yourdomain.com
类型:TXT
值:系统生成的验证字符串

# 添加后等待1~2分钟DNS生效,再按回车继续

自动化DNS验证(以Cloudflare为例)

<code"># 安装Cloudflare DNS插件
pip3 install certbot-dns-cloudflare --break-system-packages

# 创建Cloudflare凭证文件
mkdir -p /etc/letsencrypt
cat > /etc/letsencrypt/cloudflare.ini << EOF
dns_cloudflare_api_token = 你的Cloudflare API Token
EOF
chmod 600 /etc/letsencrypt/cloudflare.ini

# 全自动申请通配符证书
certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  -d yourdomain.com \
  -d *.yourdomain.com

五、多域名SAN证书

<code"># 一张证书覆盖多个不同域名
certbot certonly --nginx \
  -d site1.com \
  -d www.site1.com \
  -d site2.com \
  -d api.site2.com \
  -d blog.site3.com

# 所有域名共用一张证书,证书文件位于第一个域名目录下:
# /etc/letsencrypt/live/site1.com/

六、自动续签配置

Let's Encrypt证书有效期90天,Certbot安装时会自动创建systemd定时器:

<code"># 查看自动续签定时器状态
systemctl status certbot.timer

# 手动测试续签(不会真正续签)
certbot renew --dry-run

# 强制立即续签
certbot renew --force-renewal

续签后自动重载Nginx

<code"># /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
#!/bin/bash
systemctl reload nginx
echo "$(date): Nginx reloaded after certificate renewal" >> /var/log/certbot-reload.log
<code"># 赋予执行权限
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

添加crontab备用续签(双保险)

<code"># 每天凌晨3点尝试续签(Certbot只在证书剩余30天内才真正续签)
echo "0 3 * * * root certbot renew --quiet && systemctl reload nginx" >> /etc/crontab

七、证书到期监控

<code"># 查看所有证书的到期时间
certbot certificates

# 输出示例:
# Certificate Name: yourdomain.com
#   Domains: yourdomain.com www.yourdomain.com
#   Expiry Date: 2026-04-15 (VALID: 87 days)
#   Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
<code"># 脚本:检查证书到期天数并发送告警
#!/bin/bash
DOMAIN="yourdomain.com"
THRESHOLD=30  # 剩余天数少于30天发告警

EXPIRY=$(echo | openssl s_client -connect "$DOMAIN:443" -servername "$DOMAIN" 2>/dev/null \
    | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
EXPIRY_EPOCH=$(date -d "$EXPIRY" +%s)
NOW_EPOCH=$(date +%s)
DAYS_LEFT=$(( (EXPIRY_EPOCH - NOW_EPOCH) / 86400 ))

echo "$DOMAIN SSL证书还有 $DAYS_LEFT 天到期"

if [ "$DAYS_LEFT" -lt "$THRESHOLD" ]; then
    echo "⚠️ 警告:证书即将到期,请检查自动续签是否正常!"
fi

八、Nginx SSL最佳安全配置

<code">server {
    listen 443 ssl http2;
    server_name yourdomain.com;

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

    # 只允许TLS 1.2和1.3(禁用旧版本)
    ssl_protocols TLSv1.2 TLSv1.3;

    # 推荐的加密套件
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # SSL会话缓存(提升性能)
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # OCSP Stapling(减少SSL握手时间)
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;
    resolver 8.8.8.8 1.1.1.1 valid=300s;
    resolver_timeout 5s;

    # HSTS(强制HTTPS,max-age=1年)
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
}

九、总结

Let's Encrypt + Certbot的自动续签方案完全免费,配置一次后永久有效。通配符证书配合Cloudflare DNS插件实现全自动续签,无需任何人工操作。IDC.Net的香港VPS预装Ubuntu 22.04,Certbot安装后即可申请证书,完整配置约30分钟完成。

THE END