Certbot 运维规范文档(Let’s Encrypt SSL/TLS 管理)

一、概述

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

  1. 申请免费 SSL/TLS 证书
  2. 验证域名所有权
  3. 自动生成并更新证书文件
  4. 支持自动续期

本规范覆盖以下内容:

  • 证书申请模式选择
  • Nginx 配置要求
  • Webroot 模式操作流程(不修改 Nginx 配置)
  • 自动续期管理
  • 证书撤销与删除流程

二、前置条件

1. 域名解析

  • 域名需解析到当前服务器公网 IP
  • 必须可通过 HTTP(80 端口)访问

2. Nginx 配置要求

模式要求
--nginx已存在 server_name 配置,80 端口可访问
--webroot任意 Nginx 配置,只需能访问 Webroot 目录
--standaloneNginx 停止或占用其他端口
DNS-01 验证无需 Nginx 配置,需修改 DNS 记录

注意:Certbot 并不依赖 /etc/nginx/conf.d,只要 Nginx 加载配置文件并通过 nginx -t--nginx 模式即可找到 server_name。


三、证书申请流程

3.1 Nginx 插件模式(自动部署)

sudo certbot --nginx -d example.com -d www.example.com

操作步骤:

  1. 输入管理邮箱
  2. 同意 Let’s Encrypt 服务条款
  3. Certbot 自动识别 nginx 配置中的 server_name
  4. 自动生成证书、修改 Nginx 配置并 reload
  5. 可选择启用 HTTP → HTTPS 重定向

适用场景:单机小型服务、快速部署。


3.2 Webroot 模式(生产推荐,不修改 Nginx 配置)

步骤 1:创建验证目录

sudo mkdir -p /var/www/html/.well-known/acme-challenge
sudo chown -R www-data:www-data /var/www/html

步骤 2:Nginx 配置允许访问验证路径

location /.well-known/acme-challenge/ {
    root /var/www/html;
}

步骤 3:申请证书

sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com

证书文件位置:

  • /etc/letsencrypt/live/example.com/fullchain.pem
  • /etc/letsencrypt/live/example.com/privkey.pem

步骤 4:手动配置 Nginx HTTPS

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

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

步骤 5:测试配置并重载

sudo nginx -t
sudo systemctl reload nginx

四、自动续期规范

  • Certbot 自动安装 systemd 定时器:
sudo systemctl status certbot.timer
  • 手动测试续期:
sudo certbot renew --dry-run
  • 续期行为:
    1. 自动检查证书到期时间
    2. 更新 /etc/letsencrypt/live/ 下证书文件
    3. 不重启 Nginx(需要手动 reload 或使用 --deploy-hook 自动 reload)

五、证书撤销与删除流程

5.1 查看证书列表

sudo certbot certificates

5.2 删除证书

sudo certbot delete --cert-name example.com
  • 删除内容:
    • /etc/letsencrypt/live/example.com/
    • /etc/letsencrypt/archive/example.com/
    • /etc/letsencrypt/renewal/example.com.conf

5.3 清理 Nginx 配置

  • 删除或注释 HTTPS server 块中的 ssl_certificate 配置
  • 测试并重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx

⚠️ 不推荐直接 rm -rf /etc/letsencrypt/live/example.com,会导致 Certbot 自动续期报错。


六、运维最佳实践

  1. 生产环境推荐:Webroot 模式 + 手动 Nginx 配置
  2. 测试环境 / 单机:可使用 --nginx 模式快速部署
  3. 泛域名 / 多域名:推荐 DNS-01 验证
  4. 定期检查certbot certificates + systemctl status certbot.timer
  5. 配置变更管理:证书和 Nginx 配置分开管理,避免自动覆盖生产配置

七、规范总结

  • Certbot 可以自动申请、部署、续期 SSL 证书
  • --nginx 模式依赖已有 server_name
  • Webroot / Standalone / DNS 模式不依赖 Nginx 配置
  • 删除证书必须同步清理 Nginx 配置
  • 生产环境推荐证书与服务解耦,确保运维安全
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇