使用certbot申请https通配符证书【阿里云篇】

certbot-dns-aliyun

解决阿里云 DNS 不能自动为通配符证书续期的问题

原理

当我们使用 certbot 申请通配符证书时,需要手动添加 TXT 记录。每个 certbot 申请的证书有效期为 3 个月,虽然 certbot 提供了自动续期命令,但是当我们把自动续期命令配置为定时任务时,我们无法手动添加新的 TXT 记录用于 certbot 验证。

好在 certbot 提供了一个 hook,可以编写一个 Shell 脚本。在续期的时候让脚本调用 DNS 服务商的 API 接口动态添加 TXT 记录,验证完成后再删除此记录。

安装

  1. 安装 aliyun cli 工具

    shell 复制代码
    wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
    tar xzvf aliyun-cli-linux-latest-amd64.tgz
    sudo cp aliyun /usr/local/bin
    rm aliyun

    安装完成后需要配置凭证信息

  2. 安装 certbot-dns-aliyun 插件

    shell 复制代码
    wget https://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh
    sudo cp alidns.sh /usr/local/bin
    sudo chmod +x /usr/local/bin/alidns.sh
    sudo ln -s /usr/local/bin/alidns.sh /usr/local/bin/alidns
    rm alidns.sh
  3. 申请证书

    测试是否能正确申请:

    sh 复制代码
    certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

    正式申请时去掉 --dry-run 参数:

    • 单域名申请
    sh 复制代码
    certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
    • 多域名申请
sh 复制代码
certbot certonly -d '*.example.com' -d '*.example.site' -d '*.example.fun' --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
  1. 证书续期

    sh 复制代码
    certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

    如果以上命令没有错误,把 --dry-run 参数去掉。

  2. 自动续期

    添加定时任务 crontab。

    sh 复制代码
    crontab -e

    输入

    txt 复制代码
    1 1 */1 * * root certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload"

    上面脚本中的 --deploy-hook "nginx -s reload" 表示在续期成功后自动重启 nginx。

相关推荐
一颗宁檬不酸3 小时前
文件管理知识点
数据库
3 小时前
达梦数据库-事务
数据库·达梦数据库·dm
网硕互联的小客服4 小时前
MYSQL数据库和MSSQL数据库有什么区别?分别适用于什么脚本程序?
数据库·mysql·sqlserver
weixin_462446234 小时前
【原创实践】python 获取节假日列表 并保存为excel
数据库·python·excel
RPA 机器人就找八爪鱼5 小时前
RPA 赋能银行数字化转型:四大核心应用场景深度解析
数据库·人工智能·rpa
张人玉5 小时前
技术开发文档:MES 系统与西门子 PLC 通信软件集成方案
https·c#·plc·mes系统·西门子s7
掂掂三生有幸5 小时前
使用openGauss搭建一个监狱管理系统
数据库
VX:Fegn08955 小时前
计算机毕业设计|基于Java人力资源管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·课程设计
siriuuus5 小时前
Redis 安装、多实例部署、主从复制及 Cluster 实践
数据库·redis·centos
2301_810746315 小时前
CKA冲刺40天笔记 - day20-day21 SSL/TLS详解
运维·笔记·网络协议·kubernetes·ssl