使用 Certbot DNS 插件申请 SSL 证书
家庭宽带基本是封了 80 和 443 端口,因此导致 certbot 的常用方式无法申请 SSL 证书。如果域名托管在以下服务商,那么可以使用 DNS 插件来申请 SSL 证书。
- certbot-dns-cloudflare
- certbot-dns-digitalocean
- certbot-dns-dnsimple
- certbot-dns-dnsmadeeasy
- certbot-dns-gehirn
- certbot-dns-google
- certbot-dns-linode
- certbot-dns-luadns
- certbot-dns-nsone
- certbot-dns-ovh
- certbot-dns-rfc2136
- certbot-dns-route53
- certbot-dns-sakuracloud
本文以 cloudflare 为例,其他 DNS 服务商可以看相关文档,大体流程基本一致。
部分读者可能存在的疑惑
问题:cloudflare 可以自动申请并安装长达 15 年的 SSL 证书,为什么还要通过 certbot 手动申请?
解答:因为 cloudflare 上申请的 SSL 证书只对开启代理的域名生效。而开启代理会显著影响在国内的速度,因此 cloudflare 中的域名只做 DNS 的情况下,无法使用 cloudflare 提供的 SSL 证书,就需要手动申请。
(关于速度不用杠,有可能 A 城市 A 运营商配合上 IP 优选体验很好,但 B 城市 B 运营商就不一定了)
创建 Cloudflare API Token
打开 https://dash.cloudflare.com/profile
- 点击左侧“API 令牌”
- 点击"创建令牌"
- 点击“编辑区域 DNS”右侧的“使用模板”
- (可选)修改令牌名称
- 在区域资源中选择自己的域名
- 点击“继续以显示摘要”
- 点击“创建令牌”。记得复制令牌。
申请 SSL 证书
下面命令中有两处 xxxxx 需要进行替换:
-
Cloudflare API Token
替换成刚刚复制的令牌
-
申请 SSL 证书的域名
替换成想要申请证书的域名,比如 bbb.aaa.com 或者 *.aaa.com(泛域名证书)
# 为了方便,这里使用 root 用户进行操作
sudo su
# 如果之前有相关安装,先删除,避免后续影响
apt remove certbot
rm -rf /etc/letsencrypt
# 安装 certbot 和 dns 插件
apt update
apt install python3 python3-venv libaugeas0
apt remove certbot
python3 -m venv /opt/certbot/
/opt/certbot/bin/pip install --upgrade pip
/opt/certbot/bin/pip install --upgrade certbot certbot-dns-cloudflare
ln -s /opt/certbot/bin/certbot /usr/bin/certbot
# 创建 cloudflare API Token 文件
mkdir -p ~/.secrets/certbot
echo "dns_cloudflare_api_token=xxxxxxxxxxxxxxxxx" > ~/.secrets/certbot/cloudflare.ini
chmod 600 ~/.secrets/certbot/cloudflare.ini
# 申请证书
certbot certonly --dns-cloudflare --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini -d xxx.xxx.xxx
如果一切正常,应该会包含以下输出
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for xxx.xxx.xxx
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/xxx.xxx.xxx/fullchain.pem
Key is saved at: /etc/letsencrypt/live/xxx.xxx.xxx/privkey.pem
This certificate expires on 2025-04-28.
These files will be updated when the certificate renews.
NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
certbot 软件安装在 /opt/certbot/,其工作目录为 /etc/letsencrypt/
Nginx 配置证书
在站点配置文件中添加:
- listen 后添加 ssl
- 添加 ssl_certificate 和 ssl_certificate_key 字段
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name xxx.xxx.xxx;
ssl_certificate /etc/letsencrypt/live/xxx.xxx.xxx/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.xxx.xxx/privkey.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
自动更新证书
采用官方推荐的做法(相比官方删去了一个 sudo,因为已经以 root 身份运行)
–post-hook 是更新后的一个钩子,一般来说让 nginx 重新加载配置即可刷新 SSL 证书
echo "0 0,12 * * * root /opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q --post-hook 'nginx -s reload'" | sudo tee -a /etc/crontab > /dev/null
到此,使用 certbot 申请 SSL 证书并且启用自动更新的内容就结束了。
Certbot 其他命令
certbot 命令需要管理员权限,要么使用 root 来运行,要么使用 sudo
# 列出现有的证书
sudo certbot certificates
# 撤销证书
sudo certbot revoke --cert-name xxx.xxx.xxx
参考:
本站不记录浏览量,但如果您觉得本内容有帮助,请点个小红心,让我知道您的喜欢。