使用certbot申请https通配符证书【阿里云篇】,自动续约haproxy或nginx

原理

当我们使用 certbot 申请通配符证书时,需要手动添加 TXT 记录。每个 certbot 申请的证书有效期为 3 个月,虽然 certbot 提供了自动续期命令,但是当我们把自动续期命令配置为定时任务时,我们无法手动添加新的 TXT 记录用于 certbot 验证。
好在 certbot 提供了一个 hook,可以编写一个 Shell 脚本。在续期的时候让脚本调用 DNS 服务商的 API 接口动态添加 TXT 记录,验证完成后再删除此记录。

安装Liunx系统上安装 Certbot

yum install certbot python3-certbot-nginx

1、安装 aliyun cli 工具

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

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

aliyun configure --profile akProfile

2、安装 certbot-dns-aliyun 插件

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.申请证书

测试是否能正确申请:
certbot certonly -d *.oulu56.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

单域名申请

certbot certonly -d *.oulu56.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"

多域名申请

certbot certonly -d '*.oulu56.com' -d '*.oulu56.site' -d '*.example.fun' --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"

4.证书续期

certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"

5.自动续期ngixn篇

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

6.自动续约脚本haproxy篇

#!/bin/bash

DOMAIN="$1"

if [ -z "$1" ]; then
  echo "错误:未传入参数域名"
  echo "用法:$1 参数"
  exit 1
fi
# 证书地址
SSL_PATH="/etc/letsencrypt/live/$DOMAIN"
# haproxy的配置SSL的地址
HAPROXY_CONFIG="/app/haproxy/sock/$DOMAIN"

# 检查证书是否存在
if [ ! -d "$SSL_PATH" ]; then
  echo "证书不存在,开始申请..."
  certbot certonly -d *.$DOMAIN --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
else
  echo "证书已存在,查询有效期..."
  # 获取证书有效期
  NOT_AFTER=$(openssl x509 -in ${SSL_PATH}/fullchain.pem -noout -enddate | cut -d= -f2)

  # 计算有效期剩余天数
  NOT_AFTER_TIMESTAMP=$(date -d "$NOT_AFTER" +%s)
  CURRENT_TIMESTAMP=$(date +%s)
  DAYS_LEFT=$(( ( -(CURRENT_TIMESTAMP - NOT_AFTER_TIMESTAMP) / 86400 ) ))
  echo "证书有效期剩余:$DAYS_LEFT 天"

   # 判断有效期是否大于 10 天
  if [ $DAYS_LEFT -gt 10 ]; then
    echo "证书有效期大于 10 天,退出执行!"
    exit 0
  else
    echo "证书有效期不足 10 天,继续执行..."
    certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
  fi
fi

# 等待证书更新完成
echo "等待证书更新完成..."
sleep 10

rm -rf $HAPROXY_CONFIG/$DOMAIN.pem
cat $SSL_PATH/fullchain.pem $SSL_PATH/privkey.pem | tee $HAPROXY_CONFIG/$DOMAIN.pem

# 等待证书复制完成
echo "等待证书复制完成..."
sleep 10

docker restart haproxy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值