跳转至

Certbot Let's Encrypt SSL 证书管理

一句话概述:Certbot 是 Let's Encrypt 的官方客户端,自动申请和续期免费的 HTTPS 证书,一个命令就能给你的网站加上小锁头,证书 90 天到期前自动续期。

核心知识点

概念白话解释
Let's Encrypt免费的 SSL 证书颁发机构
Certbot自动化证书申请/续期的工具
SSL/TLS加密协议,让 HTTP 变成 HTTPS
ACME 协议自动化证书管理协议,Certbot 用它和 Let's Encrypt 通信
HTTP 验证通过在网站放一个文件来证明你拥有这个域名
DNS 验证通过添加 DNS TXT 记录来证明域名所有权
通配符证书*.example.com,一个证书覆盖所有子域名

安装配置

# Ubuntu/Debian
sudo apt update
sudo apt install certbot  # 安装 Certbot

# 如果用 Nginx
sudo apt install python3-certbot-nginx  # Nginx 插件

# 如果用 Apache
sudo apt install python3-certbot-apache  # Apache 插件

# macOS
brew install certbot

# Docker
docker run --rm -v /etc/letsencrypt:/etc/letsencrypt certbot/certbot certonly ...

基本使用

Nginx 自动配置(最简单)

# 一个命令搞定:申请证书 + 自动配置 Nginx
sudo certbot --nginx -d example.com -d www.example.com
# -d: 指定域名(可以多个)
# 会自动修改 Nginx 配置文件,加入 SSL 设置

# 只申请证书不改 Nginx 配置
sudo certbot certonly --nginx -d example.com

Standalone 模式(没有 Web 服务器)

# Certbot 自己启动临时 Web 服务器验证
sudo certbot certonly --standalone -d example.com
# 注意:需要 80 端口空闲

Webroot 模式(Web 服务器已在运行)

# 在已有 Web 服务器的根目录放验证文件
sudo certbot certonly --webroot -w /var/www/html -d example.com
# -w: Web 根目录路径

DNS 验证(通配符证书必须用这个)

# 手动 DNS 验证
sudo certbot certonly --manual --preferred-challenges dns -d "*.example.com" -d example.com
# 会让你添加一条 DNS TXT 记录
# 添加后等 DNS 生效再按回车

# 自动 DNS 验证(需要 DNS 插件)
sudo apt install python3-certbot-dns-cloudflare  # Cloudflare 插件
sudo certbot certonly --dns-cloudflare \
  --dns-cloudflare-credentials /etc/cloudflare.ini \
  -d "*.example.com" -d example.com

证书文件位置

# 证书文件存储在 /etc/letsencrypt/live/example.com/
fullchain.pem  # 完整证书链(Nginx 用这个)
privkey.pem    # 私钥
cert.pem       # 证书
chain.pem      # 中间证书

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;  # 私钥

    location / {
        proxy_pass http://localhost:3000;
    }
}

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

高级用法

自动续期

# 测试续期(不实际执行)
sudo certbot renew --dry-run  # 模拟续期过程

# 手动续期
sudo certbot renew  # 续期所有快到期的证书

# 自动续期(Certbot 安装后默认已配置)
# systemd timer 或 cron 每天检查两次
sudo systemctl status certbot.timer  # 查看定时器状态

# 如果需要手动配置 cron
# crontab -e
# 0 0,12 * * * certbot renew --quiet  # 每天 0 点和 12 点检查续期

续期后自动重载 Nginx

sudo certbot renew --deploy-hook "systemctl reload nginx"
# --deploy-hook: 续期成功后执行的命令

列出和管理证书

sudo certbot certificates  # 列出所有证书和到期时间
sudo certbot delete --cert-name example.com  # 删除证书
sudo certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem  # 吊销证书

常见报错

报错信息原因解决方案
Challenge failed域名验证失败检查 DNS 指向和 80 端口是否可访问
Too many requests申请太频繁Let's Encrypt 有速率限制,等一小时
port 80 already in use80 端口被占--webroot--nginx 模式
续期失败验证方式变了检查 Web 服务器配置
证书过期自动续期没生效certbot renew --dry-run 排查
权限问题没用 sudoCertbot 需要 root 权限

速查表

# 申请证书
certbot --nginx -d example.com            # Nginx 自动配置
certbot certonly --nginx -d example.com     # 只申请不改配置
certbot certonly --standalone -d example.com  # 独立模式
certbot certonly --webroot -w /path -d example.com  # Webroot 模式
certbot certonly --manual --preferred-challenges dns -d "*.example.com"  # DNS 通配符

# 管理
certbot certificates          # 列出证书
certbot renew                 # 续期
certbot renew --dry-run       # 模拟续期
certbot delete                # 删除证书
certbot revoke                # 吊销证书

# 证书路径
/etc/letsencrypt/live/<域名>/fullchain.pem  # 完整证书链
/etc/letsencrypt/live/<域名>/privkey.pem    # 私钥

# 限制
# 每个域名每周最多 50 个证书
# 每个 IP 每 3 小时最多 10 次验证失败
# 证书有效期 90 天
# 建议每 60 天续期一次

参考:Certbot 官网 | Let's Encrypt | Certbot 文档