Certbot 通配符 SSL 证书申请和续期指南

Certbot 通配符 SSL 证书申请和续期指南

1. 安装 Certbot

bash 复制代码
yum install epel-release -y
yum install certbot -y

2. 安装证书

2.1 下载 DNS API 脚本

bash 复制代码
git clone https://github.com/yangrongzhou/certbot-letencrypt-wildcardcertificates-alydns-au.git
cd certbot-letencrypt-wildcardcertificates-alydns-au
chmod 0777 au.sh

2.2 配置

2.2.1 domain.ini 配置
  • 如果 domain.ini 文件中没有你的根域名,请自行添加
2.2.2 DNS API 密钥配置

需要从域名服务商获取 API 密钥并配置在 au.sh 文件中:

服务商 密钥变量 申请文档
阿里云 ALY_KEY 和 ALY_TOKEN 官方文档
腾讯云 TXY_KEY 和 TXY_TOKEN 官方文档
华为云 HWY_KEY 和 HWY_TOKEN 官方文档
GoDaddy GODADDY_KEY 和 GODADDY_TOKEN 官方文档
2.2.3 选择运行环境

支持以下运行环境和场景:

PHP 环境(>4 以上版本均可)

  • au.sh php aly add/clean:PHP 操作阿里云 DNS
  • au.sh php txy add/clean:PHP 操作腾讯云 DNS
  • au.sh php godaddy add/clean:PHP 操作 GoDaddy DNS

Python 环境(支持 2.7 和 3.7)

  • au.sh python aly add/clean:Python 操作阿里云 DNS
  • au.sh python txy add/clean:Python 操作腾讯云 DNS
  • au.sh python hwy add/clean:Python 操作华为云 DNS
  • au.sh python godaddy add/clean:Python 操作 GoDaddy DNS

3. 申请证书

3.1 测试申请(干跑模式)

bash 复制代码
certbot certonly -d *.lantanhb.com --manual --preferred-challenges dns --dry-run \
--manual-auth-hook "/work/projects/shell/certbot-au/au.sh python txy add" \
--manual-cleanup-hook "/work/projects/shell/certbot-au/au.sh python txy clean"

参数说明:

  • certonly:验证模式,只获取证书,不配置 web 服务器
  • --manual:使用插件
  • --preferred-challenges dns:采用 DNS 验证申请者合法性
  • --dry-run:测试模式,不实际申请证书
  • -d:指定要申请证书的域名
  • --manual-auth-hook:执行命令时调用 hook 文件
  • --manual-cleanup-hook:清除 DNS 添加的记录

重要提示: --manual-auth-hook--manual-cleanup-hook 的三个参数:

  • 第一个参数:选择语言(php/python)
  • 第二个参数:DNS 服务商(aly/txy/hwy/godaddy)
  • 第三个参数:固定值(--manual-auth-hook 用 add,--manual-cleanup-hook 用 clean)

3.2 实际申请证书

bash 复制代码
certbot certonly -d *.lantanhb.com --manual --preferred-challenges dns \
--manual-auth-hook "/work/projects/shell/certbot-au/au.sh python txy add" \
--manual-cleanup-hook "/work/projects/shell/certbot-au/au.sh python txy clean"

3.3 申请多域名通配符证书(SAN 证书)

bash 复制代码
certbot certonly -d *.example.com -d *.example.org -d www.example.cn \
--manual --preferred-challenges dns \
--manual-auth-hook "/脚本目录/au.sh php aly add" \
--manual-cleanup-hook "/脚本目录/au.sh php aly clean"

4. 续期证书

4.1 续期所有证书

bash 复制代码
certbot renew --manual --preferred-challenges dns \
--manual-auth-hook "/脚本目录/au.sh php aly add" \
--manual-cleanup-hook "/脚本目录/au.sh php aly clean"

4.2 续期特定证书

查看现有证书:

bash 复制代码
certbot certificates

续期指定证书(例如 simplehttps.com):

bash 复制代码
certbot renew --cert-name simplehttps.com \
--manual-auth-hook "/脚本目录/au.sh php aly add" \
--manual-cleanup-hook "/脚本目录/au.sh php aly clean"

5. 设置定时任务

将证书续期加入 crontab:

bash 复制代码
# 证书有效期<30天才会renew,所以crontab可以配置为1天或1周
# 注意:只有成功renew证书,才会重新启动nginx
1 1 */1 * * root certbot-auto renew --manual --preferred-challenges dns \
--deploy-hook "systemctl restart nginx" \
--manual-auth-hook "/脚本目录/au.sh php aly add" \
--manual-cleanup-hook "/脚本目录/au.sh php aly clean"

6.故障排除

  • Debug 日志 :操作 DNS API 时遇到问题可查看 /var/log/certd.log
  • 权限问题:确保 API token 具有足够的 DNS 操作权限

7.参考链接

相关推荐
せいしゅん青春之我10 小时前
[JavaEE初阶]HTTPS-SSL传输过程中的加密
https·java-ee·ssl
任性不起来了10 小时前
宝塔面板点击ssl证书报错:出错了,面板运行时发生错误!ModuleNotFoundError: No module named ‘OpenSSL‘
网络·网络协议·ssl
Andya_net10 小时前
网络安全 | SSL/TLS 证书文件格式详解:PEM、CRT、CER、DER、PKI、PKCS12
安全·web安全·ssl
L47542 天前
SSL/TLS证书:保障网站安全的关键
网络协议·安全·ssl·tls
卓码软件测评4 天前
第三方媒体流压力测试:k6插件xk6-webrtc的使用来测试媒体流的性能
网络协议·测试工具·http·https·webrtc·ssl·媒体
AirDroid_cn4 天前
Win11 远程桌面:连接公司电脑时,提示 “证书错误” 如何解决?
windows·网络协议·https·ssl·电脑技巧
KnowSafe6 天前
iTrustSSL和RapidSSL哪个性价比更高?
ssl·rapidssl·itrustssl
BenChuat7 天前
使用 httpsok 给 QNAP NAS 添加阿里云域名的永久免费 HTTPS(SSL)证书
阿里云·https·ssl
福大大架构师每日一题7 天前
nginx 1.29.2 发布:AWS-LC 支持、QUIC及SSL优化等重要更新
nginx·ssl·aws
小白电脑技术17 天前
Nginx-Proxy-Manager配置SSL泛域名证书教程
运维·ssl