跳转到内容

Let's Encrypt SSL 证书自动化管理完全指南 2026

Let's Encrypt SSL 证书

🔒 HTTPS 已不是选项,而是标配。 自 2020 年起,主流浏览器对所有 HTTP 网站标记"不安全",搜索引擎也优先排名 HTTPS 网站。Let's Encrypt 提供免费、自动化的 SSL 证书,是个人网站和小型项目的首选方案。本文带你从零掌握证书申请、自动续期与全场景配置。

本文将带你系统掌握:

  • ✅ SSL/TLS 基础知识与证书类型
  • ✅ Certbot 安装与使用
  • ✅ 单域名证书申请
  • ✅ 通配符证书(Wildcard)申请
  • ✅ Nginx / Apache SSL 配置
  • ✅ 自动续期与到期告警
  • ✅ Docker 环境下的证书管理
  • ✅ Cloudflare 模式下的 SSL 选择
  • ✅ 常见问题排查与最佳实践

一、SSL/TLS 基础

1.1 为什么需要 HTTPS

  • 加密传输:防止数据在传输中被窃听
  • 数据完整性:防止内容被篡改
  • 身份验证:证明网站真实身份
  • SEO 优势:Google 优先排名 HTTPS 网站
  • 浏览器信任:避免"不安全"警告
  • 功能必需:HTTP/2、PWA、Geolocation 等需要 HTTPS

1.2 SSL vs TLS

项目SSLTLS
全称Secure Sockets LayerTransport Layer Security
版本SSL 2.0 / 3.0(已废弃)TLS 1.0 ~ 1.3
现状已不安全,禁用TLS 1.2 / 1.3 为主流

💡 现在说的"SSL 证书"实际上都是 TLS 证书,只是习惯称呼。

1.3 证书类型

类型说明验证方式适用场景
DV(域名验证)仅验证域名所有权DNS / HTTP个人博客、小型网站
OV(组织验证)验证企业信息企业资料审核企业官网
EV(扩展验证)最高级别验证严格审核金融、电商

Let's Encrypt 只提供 DV 证书,但对大多数场景足够。

1.4 Let's Encrypt 的特点

  • 完全免费:不收任何费用
  • 自动化:支持自动申请和续期
  • 开放标准:基于 ACME 协议
  • 有效期 90 天:鼓励自动化续期
  • 支持通配符:可申请 *.example.com 通配符证书
  • 受信任:被所有主流浏览器信任

二、Certbot 安装

2.1 什么是 Certbot

Certbot 是 Let's Encrypt 官方推荐的 ACME 客户端,用于:

  • 自动申请证书
  • 自动验证域名
  • 自动配置 Web 服务器
  • 自动续期

2.2 安装 Certbot

Ubuntu / Debian:

bash
# 更新包管理器
sudo apt update

# 安装 Certbot 和 Nginx 插件
sudo apt install -y certbot python3-certbot-nginx

# 安装 Apache 插件(如使用 Apache)
sudo apt install -y python3-certbot-apache

CentOS / RHEL:

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

# 安装 Certbot
sudo yum install -y certbot python3-certbot-nginx

使用 Snap(官方推荐):

bash
# 安装 snapd(如未安装)
sudo apt install -y snapd

# 安装 Certbot
sudo snap install --classic certbot

# 创建软链接
sudo ln -s /snap/bin/certbot /usr/bin/certbot

使用 Docker:

bash
docker run --rm \
  -v /etc/letsencrypt:/etc/letsencrypt \
  -v /var/lib/letsencrypt:/var/lib/letsencrypt \
  -v /var/log/letsencrypt:/var/log/letsencrypt \
  certbot/certbot certonly

2.3 验证安装

bash
certbot --version
# 输出: certbot 2.x.0

三、申请单域名证书

3.1 Webroot 方式(推荐,不中断服务)

Webroot 方式通过在网站根目录放置验证文件来证明域名所有权。

前提条件:

  • 域名已解析到服务器
  • Web 服务器(Nginx/Apache)已运行
  • 80 端口可访问

申请证书:

bash
sudo certbot certonly \
  --webroot \
  -w /var/www/html \
  -d example.com \
  -d www.example.com \
  --email your@email.com \
  --agree-tos \
  --no-eff-email

参数说明:

  • --webroot:使用 Webroot 验证方式
  • -w:网站根目录
  • -d:域名(可指定多个)
  • --email:用于到期提醒的邮箱
  • --agree-tos:同意服务条款

生成的文件位置:

/etc/letsencrypt/live/example.com/
├── cert.pem         # 证书文件
├── chain.pem        # 链证书
├── fullchain.pem    # 完整证书链(推荐使用)
├── privkey.pem      # 私钥文件
└── README           # 说明文件

3.2 Standalone 方式

Standalone 方式会临时启动一个 Web 服务器进行验证,需要停止现有 Web 服务器。

bash
# 先停止 Nginx
sudo systemctl stop nginx

# 申请证书
sudo certbot certonly \
  --standalone \
  -d example.com \
  --email your@email.com \
  --agree-tos

# 重新启动 Nginx
sudo systemctl start nginx

3.3 Nginx 插件方式(最简单)

Certbot 可以自动修改 Nginx 配置:

bash
sudo certbot --nginx \
  -d example.com \
  -d www.example.com \
  --email your@email.com \
  --agree-tos

Certbot 会自动:

  • 申请证书
  • 修改 Nginx 配置
  • 配置 HTTPS
  • 设置 HTTP 自动跳转 HTTPS

四、申请通配符证书

4.1 为什么需要通配符证书

通配符证书 *.example.com 可以:

  • 保护所有子域名
  • 无需为每个子域名单独申请
  • 添加新子域名时无需重新申请

4.2 DNS 验证方式

通配符证书必须使用 DNS 验证:

bash
sudo certbot certonly \
  --manual \
  --preferred-challenges dns \
  -d *.example.com \
  -d example.com \
  --email your@email.com \
  --agree-tos

Certbot 会提示添加 DNS TXT 记录:

Please deploy a DNS TXT record under the name:
_acme-challenge.example.com.

with the following value:
gfj9Xq...验证码...

添加 DNS 记录:

  1. 登录域名服务商(Cloudflare、阿里云等)
  2. 添加 TXT 记录
  3. 主机记录:_acme-challenge
  4. 记录值:Certbot 提供的验证码
  5. 等待 DNS 生效(通常 1-10 分钟)
bash
# 验证 DNS 是否生效
dig -t txt _acme-challenge.example.com

# 或使用 nslookup
nslookup -type=txt _acme-challenge.example.com

DNS 生效后,按回车继续。

4.3 使用 DNS 插件自动验证

手动添加 DNS 记录比较麻烦,可以使用 DNS 插件自动化:

Cloudflare 插件:

bash
# 安装 Cloudflare 插件
sudo apt install -y python3-certbot-dns-cloudflare

# 创建配置文件
sudo nano /etc/letsencrypt/cloudflare.ini

配置文件内容:

ini
dns_cloudflare_api_token = 你的Cloudflare_API_Token
bash
# 设置权限
sudo chmod 600 /etc/letsencrypt/cloudflare.ini

# 申请通配符证书
sudo certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  -d *.example.com \
  -d example.com \
  --email your@email.com \
  --agree-tos

其他常用 DNS 插件:

DNS 服务商插件名
Cloudflaredns-cloudflare
阿里云dns-aliyun(第三方)
腾讯云dns-tencent(第三方)
DNSPoddns-dnspod(第三方)
AWS Route53dns-route53
Google Domainsdns-google

五、Nginx SSL 配置

5.1 基本配置

nginx
server {
    listen 80;
    server_name example.com www.example.com;
    # HTTP 跳转 HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    # SSL 证书
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # 网站根目录
    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

5.2 安全加固配置

nginx
server {
    listen 443 ssl http2;
    server_name example.com;

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

    # SSL 协议与加密套件
    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;

    # HSTS(HTTP 严格传输安全)
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

    # 其他安全头
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header Referrer-Policy strict-origin-when-cross-origin;

    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    root /var/www/html;
    index index.html;
}

5.3 Apache SSL 配置

apache
<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

    # 安全配置
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256
    SSLHonorCipherOrder off
    Header always set Strict-Transport-Security "max-age=63072000"
</VirtualHost>

# HTTP 跳转 HTTPS
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

六、自动续期

6.1 测试续期

Let's Encrypt 证书有效期 90 天,建议在到期前 30 天续期。

bash
# 测试续期(不会真正续期)
sudo certbot renew --dry-run

6.2 自动续期配置

Certbot 安装时会自动创建定时任务:

bash
# 查看定时任务
systemctl list-timers | grep certbot

如果没有,手动创建:

bash
# 编辑 crontab
sudo crontab -e

# 每天 3 点检查并续期
0 3 * * * certbot renew --quiet --post-hook "systemctl reload nginx"

6.3 续期钩子

bash
# 续期前执行
sudo certbot renew --pre-hook "systemctl stop nginx"

# 续期后执行
sudo certbot renew --post-hook "systemctl start nginx"

# 仅在证书更新后执行
sudo certbot renew --deploy-hook "systemctl reload nginx"

💡 使用 --deploy-hook 更好,只在证书真正更新后才执行 reload。

6.4 续期通知

邮件通知:

Certbot 会在证书即将到期时发送邮件(需配置 email)。

Webhook 通知:

bash
#!/bin/bash
# /opt/scripts/certbot-renew-hook.sh

WEBHOOK_URL="https://your-webhook-url"

# 发送通知
curl -X POST $WEBHOOK_URL \
  -H "Content-Type: application/json" \
  -d "{\"event\":\"ssl_renewed\",\"domain\":\"$RENEWED_DOMAINS\",\"date\":\"$(date)\"}"

配置:

bash
sudo certbot renew --deploy-hook /opt/scripts/certbot-renew-hook.sh

6.5 查看证书信息

bash
# 查看所有证书
sudo certbot certificates

# 查看证书详情
sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -text -noout | head -20

# 查看到期时间
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates

七、Docker 环境下的证书管理

7.1 Certbot 容器化

bash
# 申请证书
docker run -it --rm \
  -v /etc/letsencrypt:/etc/letsencrypt \
  -v /var/lib/letsencrypt:/var/lib/letsencrypt \
  -v /var/www/html:/webroot \
  certbot/certbot certonly \
  --webroot -w /webroot \
  -d example.com \
  --email your@email.com \
  --agree-tos

# 续期
docker run --rm \
  -v /etc/letsencrypt:/etc/letsencrypt \
  -v /var/www/html:/webroot \
  certbot/certbot renew

7.2 Docker Compose 集成

yaml
version: '3.8'

services:
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./html:/var/www/html
      - /etc/letsencrypt:/etc/letsencrypt:ro
    restart: always

  certbot:
    image: certbot/certbot
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/lib/letsencrypt:/var/lib/letsencrypt
      - ./html:/var/www/html
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h; done;'"
    restart: always

7.3 Nginx Proxy Manager

如果你不想手动配置,可以使用 Nginx Proxy Manager(NPM):

yaml
version: '3'
services:
  npm:
    image: jc21/nginx-proxy-manager:latest
    restart: always
    ports:
      - "80:80"
      - "443:443"
      - "81:81"
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
    environment:
      DB_SQLITE_FILE: "/data/database.sqlite"

通过 Web 界面(端口 81)一键申请和管理 SSL 证书。


八、Cloudflare 模式下的 SSL

8.1 Cloudflare SSL 模式

模式说明适用场景
Off不加密不推荐
FlexibleCF 到客户端加密,CF 到源站不加密源站不支持 SSL
Full全程加密,但不验证证书自签名证书
Full (Strict)全程加密且验证证书推荐配置

8.2 使用 Cloudflare 代理

如果使用 Cloudflare 代理(橙色云朵):

  1. Cloudflare 会提供边缘证书(免费)
  2. 源服务器仍需要 SSL 证书
  3. 推荐使用 Full (Strict) 模式
  4. 可以使用 Cloudflare Origin CA 证书(有效期 15 年)

8.3 Cloudflare Origin CA 证书

bash
# 使用 Cloudflare API 申请 Origin 证书
curl -X POST "https://api.cloudflare.com/client/v4/certificates" \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  --data '{
    "hostnames": ["example.com", "*.example.com"],
    "requested_validity": 5475,
    "request_type": "origin-rsa",
    "csr": ""
  }'

💡 Origin CA 证书有效期最长 15 年,无需频繁续期,但仅在 Cloudflare 代理时有效。


九、常见问题排查

9.1 申请失败

问题:域名验证失败

bash
# 检查域名解析
dig example.com

# 检查 80 端口是否可访问
curl -I http://example.com

# 检查防火墙
sudo ufw status
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

问题:DNS 验证未生效

bash
# 检查 TXT 记录
dig -t txt _acme-challenge.example.com

# 等待更长时间(可能 DNS 缓存未刷新)
# 清除本地 DNS 缓存
sudo systemd-resolve --flush-caches

9.2 续期失败

问题:Webroot 路径错误

bash
# 确认网站根目录
ls /var/www/html

# 检查 Nginx 配置
nginx -t

问题:端口被占用

bash
# 查看端口占用
sudo lsof -i :80
sudo lsof -i :443

9.3 查看日志

bash
# Certbot 日志
sudo cat /var/log/letsencrypt/letsencrypt.log

# Nginx 错误日志
sudo tail -f /var/log/nginx/error.log

9.4 撤销证书

bash
# 撤销证书
sudo certbot revoke \
  --cert-path /etc/letsencrypt/live/example.com/cert.pem \
  --reason keycompromise

# 删除证书
sudo certbot delete --cert-name example.com

十、最佳实践

10.1 安全建议

  1. 使用 TLS 1.2+:禁用 TLS 1.0 和 1.1
  2. 启用 HSTS:强制浏览器使用 HTTPS
  3. 启用 OCSP Stapling:提升性能和隐私
  4. 使用强加密套件:优先使用 ECDHE
  5. 定期检查评级:使用 SSL Labs 测试

10.2 性能优化

  1. 启用 HTTP/2:多路复用,提升加载速度
  2. Session 缓存:减少握手开销
  3. Session Tickets:简化会话恢复
  4. ECC 证书:比 RSA 更快,密钥更小

10.3 运维规范

规范说明
监控到期设置告警,提前 30 天提醒
自动续期配置 crontab 或 systemd timer
定期测试定期执行 --dry-run 测试
备份证书备份 /etc/letsencrypt/ 目录
记录文档记录所有域名和证书对应关系

10.4 检查 SSL 评级

使用在线工具检查 SSL 配置:


十一、常用命令速查

bash
# 申请证书
certbot certonly --webroot -w /var/www/html -d example.com --email you@email.com --agree-tos

# 申请通配符证书
certbot certonly --manual --preferred-challenges dns -d *.example.com -d example.com

# Nginx 自动配置
certbot --nginx -d example.com

# 续期
certbot renew                    # 续期所有
certbot renew --dry-run          # 测试续期
certbot renew --cert-name example.com  # 续期指定证书

# 查看
certbot certificates             # 查看所有证书
openssl x509 -in cert.pem -text -noout  # 查看证书详情

# 删除
certbot delete --cert-name example.com  # 删除证书

# 撤销
certbot revoke --cert-path cert.pem

十二、总结

Let's Encrypt 让 HTTPS 普惠化:

  1. 完全免费:零成本获得受信任的 SSL 证书
  2. 自动化:Certbot 让申请和续期全自动
  3. 安全可靠:被所有主流浏览器信任
  4. 通配符支持:一张证书覆盖所有子域名
  5. 生态完善:支持各种 Web 服务器和 DNS 服务商

🎯 配置好 SSL 证书和自动续期,从此告别浏览器"不安全"警告,让网站更专业、更安全。


相关文章推荐: