使用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。

相关推荐
小吴编程之路14 小时前
MySQL 索引核心特性深度解析:从底层原理到实操应用
数据库·mysql
~莫子14 小时前
MySQL集群技术
数据库·mysql
凤山老林15 小时前
SpringBoot 使用 H2 文本数据库构建轻量级应用
java·数据库·spring boot·后端
就不掉头发15 小时前
Linux与数据库进阶
数据库
与衫15 小时前
Gudu SQL Omni 技术深度解析
数据库·sql
咖啡の猫15 小时前
Redis桌面客户端
数据库·redis·缓存
oradh15 小时前
Oracle 11g数据库软件和数据库静默安装
数据库·oracle
what丶k16 小时前
如何保证 Redis 与 MySQL 数据一致性?后端必备实践指南
数据库·redis·mysql
_半夏曲16 小时前
PostgreSQL 13、14、15 区别
数据库·postgresql
把你毕设抢过来16 小时前
基于Spring Boot的社区智慧养老监护管理平台(源码+文档)
数据库·spring boot·后端