Skip to content

配置与文件布局

目标:看懂 /etc/letsencrypt 里每个目录/文件是干什么的,能安全地定制与排错。

1. 目录总览

/etc/letsencrypt/
├── accounts/            # ACME 账号(账号密钥、注册信息)
├── live/                # 当前生效证书(符号链接,Web 服务器引用这里)
│   └── example.com/
│       ├── cert.pem     → ../../archive/example.com/certN.pem
│       ├── chain.pem
│       ├── fullchain.pem
│       └── privkey.pem
├── archive/             # 历史版本真实文件(cert1.pem, cert2.pem, ...)
│   └── example.com/
├── renewal/             # 每张证书的续期配置(example.com.conf)
├── renewal-hooks/       # 全局续期钩子脚本目录(pre/ deploy/ post/)
│   ├── pre/
│   ├── deploy/
│   └── post/
├── cli.ini              # 全局默认配置(需自建)
└── (keys/、csr/ 等内部目录)

其他相关路径:

路径作用
/var/log/letsencrypt/letsencrypt.log主日志(排错看这里)
/var/lib/letsencrypt/工作目录(临时文件、状态)

2. live/ 与 archive/ 的关系(关键)

  • live/example.com/ 里的文件是符号链接,指向 archive/example.com/ 下的最新版本(cert1.pemcert2.pem…)。
  • 每次续期生成 certN.pemlive/ 链接自动切到最新。
  • Web 服务器始终引用 live/ 路径,这样续期后无需改配置,只需 reload。
bash
ls -l /etc/letsencrypt/live/example.com/
# fullchain.pem -> ../../archive/example.com/fullchain2.pem

3. renewal/<name>.conf 续期配置

每张证书一份,记录首次签发用的参数。示例:

ini
# renew_before_expiry = 30 days
version = 2.11.0
archive_dir = /etc/letsencrypt/archive/example.com
cert = /etc/letsencrypt/live/example.com/cert.pem
privkey = /etc/letsencrypt/live/example.com/privkey.pem
chain = /etc/letsencrypt/live/example.com/chain.pem
fullchain = /etc/letsencrypt/live/example.com/fullchain.pem

[renewalparams]
account = 0123...
authenticator = webroot
server = https://acme-v02.api.letsencrypt.org/directory
webroot_path = /var/www/html,
[[webroot_map]]
example.com = /var/www/html
  • authenticator:续期时复用的验证插件。
  • webroot_path / webroot_map:webroot 目录(多域名时按域名映射)。
  • 可直接编辑此文件改续期行为(如加 deploy_hook),改完 renew --dry-run 验证。

4. cli.ini 全局默认配置

创建 /etc/letsencrypt/cli.ini,所有 certbot 命令自动读取(命令行参数优先级更高):

ini
# /etc/letsencrypt/cli.ini
email = ops@example.com
agree-tos = true
no-eff-email = true
non-interactive = true
# 默认续期后重载 nginx
deploy-hook = systemctl reload nginx
# 默认密钥类型
# key-type = ecdsa

注意:cli.ini 里写 renew_before_expirypreferred-challenges 等也生效,但注意不要和 renewal/*.conf 冲突。

5. renewal-hooks 全局钩子目录

放脚本即可对所有证书生效(比在 cli.ini 里写更灵活、可执行多步):

bash
sudo mkdir -p /etc/letsencrypt/renewal-hooks/{pre,deploy,post}

# deploy 钩子:续期成功后重载 nginx
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh > /dev/null <<'EOF'
#!/bin/bash
systemctl reload nginx
EOF
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

certbot renew 会自动执行这些目录里的可执行脚本。

6. 常用定制

需求做法
换默认邮箱certbot update_account -m new@x.com 或改 cli.ini
换证书名签发时 --cert-name;已存在则用 certbot certificates 找到后重新签发
换密钥类型/位数重新签发 --key-type ecdsa--rsa-key-size 4096
改 webroot 目录编辑 renewal/*.conf 的 webroot_path,再 renew --dry-run
换 CA(staging→生产)renewal/*.conf 的 server 字段,或重新签发

7. 备份与迁移

需要备份的最小集合:

bash
# 建议整体备份(含账号密钥与私钥)
sudo tar czf letsencrypt-backup-$(date +%F).tar.gz /etc/letsencrypt

# 迁移到新服务器:解压到相同路径即可(路径一致则 certbot 直接可用)

私钥(privkey.pem)与账号密钥都在其中,务必加密保存、限制访问。迁移后记得把 DNS 解析与 Web 服务一并切换,并在新机器上 certbot renew --dry-run 验证。