使用certbot-dns-cloudflare生成通配符证书与续期

最近将域名解析服务商由腾讯云的DNSpod迁移到了cloudflare,原有的自动续订证书脚本就无法使用了,所以需要重新配置证书续期服务,以下为流程记录

如果你需要国内dns服务商的续期脚本,可以参考GitHub上的这个项目:yangrongzhou/certbot-letencrypt-wildcardcertificates-alydns-au: certbot'renewing letencrypt certificate plugin - automatic verification aliyun/tencentyun/godaddy dns (github.com)

准备

移除原有 certbot

移除之前的certbot,避免对后期配置snap版本的certbot造成干扰

sudo apt-get remove certbot

安装 Certbot 与 certbot-dns-cloudflare

安装certbot

sudo snap install certbot --classic

确保certbot可以执行,创建系统动态链接

sudo ln -s /snap/bin/certbot /usr/bin/certbot

确保已安装的插件与 Certbot snap 具有相同容器

sudo snap set certbot trust-plugin-with-root=ok

安装 DNS 插件,sudo snap install certbot-dns-<PLUGIN>,如果你使用 CloudFlare,直接执行:

sudo snap install certbot-dns-cloudflare

关于其它certbot提供的插件列表,可以访问此链接查看:用户指南 — Certbot 2.11.0 文档 (eff-certbot.readthedocs.io)

生成 Cloudflare API Key

  1. 登录 CloudFlare,在主页面上找到 API keys;如果你的 cloudflare python module 版本大于等于 2.3.1,建议复制 restricted API Token,否则复制 Global API Key

  2. 设置 DNS 凭据

    这里我将凭据保存在了之前的证书配置文件下,你可以根据自己的配置进行修改

    sudo vim /etc/letsencrypt/cloudflare.int
    

如果您使用 restricted API Token,按照下方格式编辑并变更 token 数值:

# Cloudflare API token used by Certbot
dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567

如果您使用 Global API Key,按照下方格式编辑并变更 key 数值与 cloudflare 邮箱:

# Cloudflare API credentials used by Certbot
dns_cloudflare_email = cloudflare@example.com
dns_cloudflare_api_key = 0123456789abcdef0123456789abcdef01234
  1. 修改cloudflare.int文件权限
sudo chmod 0400 /root/.secrets/cloudflare.ini

注意:此步骤一定要执行,不然回出现 unsafe permissions on credentials configuration file 报错

申请证书

sudo certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.int \
  --dns-cloudflare-propagation-seconds 60 \
  --key-type ecdsa \
  -d *.example.com

记得将 example.com 替换为您的域名,之后若您第一次申请,可能会要求您进行 TXT 验证,按照指示在 cloudflare 上操作即可;除此之外,您还应完成主域名的 A 记录验证

如果系统提示 Another instance of Certbot is already running,使用 ps -ef | grep certb 指令查看 ID,并杀死进程 kill ID_number

管理证书

子命令certificates显示所有证书的信息;revoke吊销证书;delete删除证书。

这些管理证书的子命令都不再需要额外的验证,因为账号信息已经保存在了本地。

例如使用

sudo certbot certificates

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: buguagaoshu.com
    Serial Number: ****
    Key Type: RSA
    Domains: *.buguagaoshu.com
    Expiry Date: 2024-08-30 20:01:40+00:00 (VALID: 75 days)
    Certificate Path: ***
    Private Key Path: ***
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ubuntu@VM-8-16-ubuntu:/etc/letsencrypt$

即可查看当前所有证书状态信息

一般来说,Certbot的所有信息都存储在/etc/letsencrypt。

archive目录包含所有已经申请的证书及私钥,包括已经过期的证书。而live目录只包含最新的证书及私钥,通过符号链接指向archive目录对应的文件。

fullchain.pem是大多数软件需要的证书文件,实际上它是cert.pem和chain.pem的拼接。

privkey.pem如其名,是私钥文件。

更新证书

所谓更新证书,实际上是用相同账号申请一个效果相同的新证书。没有到期的旧证书依然可以使用。

Let's Encrypt颁发的证书有效期固定为90天,需要经常更新证书。

子命令renew用来更新证书。虽然更新证书是一个完整的证书申请过程,但是在之前申请证书时,Certbot把相关信息都保存在了renewal目录,我们直接运行certbot renew即可。

$ cat /etc/letsencrypt/renewal/example.com.conf
# renew_before_expiry = 30 days
version = 1.23.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

# Options used in the renewal process
[renewalparams]
account = 1234567890abcde
key_type = ecdsa
authenticator = dns-cloudflare
dns_cloudflare_credentials = /etc/letsencrypt/cloudflare.ini
server = https://acme-v02.api.letsencrypt.org/directory

配置完成后你可以执行

sudo certbot renew --dry-run

来试运行证书更新流程

renew子命令会检查所有证书的到期时间,只有在30天内到期的证书才会被真正更新。这个特性非常有利于自动化,我们不需要关心每个证书的到期时间,只要经常运行certbot renew,就可以自动更新即将到期的证书。

如果想立刻看到更新证书的效果,也可以跳过到期检查,强制更新证书,使用--force-renewal选项。需要注意,证书颁发机构对申请证书的频率有限制,如果频繁强制更新证书,会导致账号被暂时冻结。

renew成功更新证书后,会把新的证书和私钥文件放在archive对应的目录下,并且修改live目录下的符号链接,使其始终指向最新的证书和私钥。

钩子

Certbot支持三种钩子,可以通过选项附加在certbot renew的末尾。

  • --pre-hook:更新证书之前调用

  • --post-hook:更新证书之后调用

  • --deploy-hook:成功更新证书之后调用

当有即将到期的证书时,--pre-hook--post-hook总会调用。--deploy-hook只有新证书已经申请下来才会调用,比较常用。

比如在证书更新后自动重载Caddy服务器:certbot renew --deploy-hook "systemctl reload caddy.service"

命令里塞命令不太方便,更优雅的钩子用法是把可执行文件放在对应的钩子目录。/etc/letsencrypt/renewal-hooks目录下有三个子目录pre、post、deploy,分别对应上面的三种钩子。

自动更新

最后你可以设置一个定时任务,来自动更新证书

编辑文件 /etc/crontab

1 1 */1 * * root certbot renew --deploy-hook  "sudo systemctl restart nginx"

来定期执行更新

版权

本文首发于:https://www.buguagaoshu.com/archives/shi-yong-certbot-dns-cloudflaresheng-cheng-tong-pei-fu-zheng-shu-yu-xu-qi

转载请注明出处。

posted @ 2024-06-16 18:20  陕西颜值扛把子  阅读(54)  评论(0编辑  收藏  举报