本文详解使用Certbot自动获取和续期Let's Encrypt证书,确保网站HTTPS永不过期。

为什么需要自动续期

Let's Encrypt证书有效期:90天
手动续期 → 容易忘记 → 证书过期 → 网站被浏览器标记"不安全"
自动续期 → 证书到期前30天自动续期 → 永远有效

安装Certbot

Ubuntu/Debian

# 更新软件包
sudo apt update

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

# 验证安装
certbot --version
# 输出:certbot 2.11.0(2026年版本)

CentOS/RHEL

# 启用EPEL仓库
sudo yum install epel-release -y

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

使用Snap(推荐,版本最新)

# 安装Snapd
sudo yum install snapd -y
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

# 安装Certbot via Snap
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

获取证书(首次)

方式一:自动配置Nginx(推荐)

# 自动修改Nginx配置文件
sudo certbot --nginx -d example.com -d www.example.com

# 输出:
# Successfully received certificate.
# Certificate is saved at /etc/letsencrypt/live/example.com/fullchain.pem
# Key is saved at /etc/letsencrypt/live/example.com/privkey.pem
# This certificate expires on 2026-08-18.
# These files will be updated when the certificate renews.

方式二:仅获取证书(手动配置)

# 获取证书但不修改Nginx配置
sudo certbot certonly --nginx -d example.com -d www.example.com

# 然后手动配置Nginx:
server {
    listen 443 ssl;
    server_name example.com;

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

通配符证书(Wildcard)

为什么需要通配符证书

单域名证书:example.com(仅保护example.com)
多域名证书:example.com + api.example.com + admin.example.com
通配符证书:*.example.com(保护所有子域名)

使用DNS-01挑战(通配符必须用这个)

# 获取通配符证书
sudo certbot -d *.example.com -d example.com \
  --manual --preferred-challenges dns-01 \
  --server https://acme-v02.api.letsencrypt.org/directory

# 输出:
# Please deploy a DNS TXT record under the name:
# _acme-challenge.example.com
# with the following value:
# xxxxxxxxxxxxxxxxxxxxxxxx
# 
# Before continuing, verify the record is deployed.
# Press Enter when ready.

# 前往你的DNS管理界面,添加TXT记录:
# 名称:_acme-challenge
# 值:xxxxxxxxxxxxxxxxxxxxxxxx

使用DNS插件(自动添加TXT记录)

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

# 配置Cloudflare API Token
echo "dns_cloudflare_api_token = YOUR_CF_API_TOKEN" \
  | sudo tee /etc/letsencrypt/cloudflare.ini
sudo chmod 600 /etc/letsencrypt/cloudflare.ini

# 自动获取通配符证书(无需手动添加DNS记录)
sudo certbot -d *.example.com -d example.com \
  --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  --server https://acme-v02.api.letsencrypt.org/directory

# 现在自动续期也会自动更新DNS记录!

自动续期

Certbot自动续期原理

Certbot安装时会添加systemd timer:
/etc/systemd/system/certbot.timer

定时任务:
- 每天运行2次(随机时间,避免集中请求Let's Encrypt服务器)
- 证书剩余有效期 < 30天 → 自动续期
- 证书剩余有效期 >= 30天 → 跳过

验证自动续期已启用

# 查看systemd timer状态
sudo systemctl status certbot.timer

# 输出:
# ● certbot.timer - Run certbot twice daily
#      Loaded: loaded (/lib/systemd/system/certbot.timer; enabled;)
#      Active: active (waiting) since ...
#     Trigger: ... (in 8h)

# 手动测试续期(不实际续期,仅测试)
sudo certbot renew --dry-run

# 输出:
# Simulating renewal of an existing certificate for example.com
# The dry run was successful.

手动触发续期

# 强制续期(即使证书未到期)
sudo certbot renew --force-renewal

# 续期特定证书
sudo certbot renew --cert-name example.com

在Nginx中配置SSL

完整SSL配置(推荐)

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

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

    # 证书路径(Certbot自动更新)
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # SSL安全配置(2026推荐)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_prefer_server_ciphers on;

    # HSTS(强制浏览器使用HTTPS)
    add_header Strict-Transport-Security "max-age=63072000" always;

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

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}

在WordPress中强制HTTPS

修改wp-config.php

// 强制HTTPS
define('FORCE_SSL_ADMIN', true);
$_SERVER['HTTPS'] = 'on';

// 修复混合内容(HTTP资源在HTTPS页面中加载)
add_action('template_redirect', function() {
    if (!is_ssl()) {
        wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
        exit;
    }
});

修复混合内容(Mixed Content)

-- 替换数据库中的HTTP链接为HTTPS
UPDATE wp_posts 
SET post_content = REPLACE(post_content, 'http://example.com', 'https://example.com');

UPDATE wp_posts 
SET guid = REPLACE(guid, 'http://example.com', 'https://example.com');

UPDATE wp_options 
SET option_value = REPLACE(option_value, 'http://example.com', 'https://example.com')
WHERE option_name IN ('siteurl', 'home');

多域名证书管理

查看已获取的证书

sudo certbot certificates

# 输出:
# Found the following certs:
#   Certificate Name: example.com
#     Domains: example.com www.example.com
#     Expiry Date: 2026-08-18 12:34:56+00:00 (VALID: 89 days)
#     Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
#     Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem

更新证书(添加新域名)

# 更新已有证书,添加新域名
sudo certbot --nginx -d example.com -d www.example.com -d api.example.com

# 或者使用renew(如果证书即将到期)
sudo certbot renew --cert-name example.com --expand -d api.example.com

删除证书

# 删除证书(不再需要)
sudo certbot delete --cert-name example.com

# 仅撤销证书(保留文件)
sudo certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem

2026年Certbot趋势

趋势一:ACME v2通配符证书普及

Let's Encrypt ACME v2 API(2018年推出):
- 支持通配符证书(*.example.com)
- 必须使用DNS-01挑战
- 推荐使用DNS插件自动验证

趋势二:TLS 1.3成为标准

# 2026年推荐SSL配置
ssl_protocols TLSv1.2 TLSv1.3;  # TLS 1.3性能提升30%
ssl_ciphers 'TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256';  # TLS 1.3密码套件

趋势三:证书透明度(CT)日志

浏览器要求:
HTTPS证书必须出现在CT日志中(否则标记为不安全)

Certbot自动提交证书到CT日志(无需手动操作)

故障排除

问题一:续期失败(端口80被占用)

# 错误:Port 80 is already in use
# 解决:停止占用端口的服务
sudo systemctl stop nginx
sudo certbot renew
sudo systemctl start nginx

问题二:DNS传播延迟

# 错误:DNS record not propagated
# 解决:等待DNS传播(最长48小时)
# 使用在线工具检查:https://www.whatsmydns.net/

问题三:Rate Limit(请求限制)

Let's Encrypt速率限制:
- 每个域名每周最多5次重复证书
- 每个IP地址每秒最多10个请求

解决:使用--staging参数测试(不计入限制)
sudo certbot --nginx -d example.com --staging

决策建议

  • 单域名/少量子域名 → Certbot + Nginx插件(自动配置)
  • 大量子域名 → 通配符证书 + DNS插件
  • 多服务器负载均衡 → DNS-01挑战(证书可共享)
  • 企业内部CA → 不使用Let's Encrypt,改用Smallstep或Vault

总结

Certbot让HTTPS证书获取和续期变得完全自动化。2026年,每个网站都应该使用HTTPS,而Certbot是最简单的实现方式。

立即行动:在你的服务器上运行 sudo certbot --nginx,为你的网站启用HTTPS!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。