[原创]免费的Https证书Let‘s Encrypt自动申请与续期

目前各大云厂商提供的通配符*证书,基本都需要收费,Let’s Encrypt的通配符证书免费,唯一问题是有效期只有3个月,3个月后又要手动申请,更新nginx证书。

这里基于linux来自动申请证书

1、安装snapd工具

# 安装EPEL存储库 
sudo yum install epel-release -y 
# 安装snapd包 
sudo yum install snapd -y 
# snapd开机启动 
sudo systemctl enable --now snapd.socket
 systemctl start snapd 
# 配置snap软链接
 sudo ln -s /var/lib/snapd/snap /snap
 #更新snapd版本 sudo snap install core

2、使用snap安装certbot工具

# 安装certbot证书机器人
 sudo snap install --classic certbot 
# 配置软链接
 sudo ln -s /snap/bin/certbot /usr/bin/certbot

3、配置自动续期脚本

$ git clone https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au
$ cd certbot-letencrypt-wildcardcertificates-alydns-au 
$ chmod 0777 au.sh 
将安装包解压到/opt目录下

4、certbot 配置文件修改

1)domain.ini

如果domain.ini文件没有你的根域名,请自行添加。

(2)DNS API 密钥:

ALY_KEY=”你的APIKey”

ALY_TOKEN=”你的APIToken”

这个 API 密钥什么意思呢?由于需要通过 API 操作阿里云 DNS, 腾讯云 DNS 的记录,所以需要去域名服务商哪儿获取 API 密钥,然后配置在au.sh文件中:

(3)选择运行环境

目前该工具支持五种运行环境和场景,通过 hook 文件和参数来调用:

  • PHP(>4以上版本均可)
  • au.sh php aly add/clean:PHP操作阿里云DNS,增加/清空DNS。
  • au.sh php txy add/clean:PHP操作腾讯云DNS,增加/清空DNS。
  • au.sh php godaddy add/clean:PHP操作GoDaddy DNS,增加/清空DNS。
  • Python(支持2.7和3.7,无需任何第三方库)
  • au.sh python aly add/clean:Python操作阿里云DNS,增加/清空DNS。
  • au.sh python txy add/clean:Python操作腾讯云DNS,增加/清空DNS。
  • au.sh python hwy add/clean:Python操作华为云DNS,增加/清空DNS。
  • au.sh python godaddy add/clean:Python操作GoDaddy DNS,增加/清空DNS。

根据自己服务器环境和域名服务商选择任意一个 hook shell(包含相应参数),具体使用见下面。

(4) 使用certbot生成通配符证书

/usr/bin/certbot certonly -d *.aiapaas.com -d aiapaas.com --manual --preferred-challenges dns --manual-auth-hook "/opt/certbot/au.sh python aly add" --manual-cleanup-hook "/opt/certbot/au.sh python aly clean"

5、通配符证书续期

/usr/bin/certbot renew --manual --preferred-challenges dns --manual-auth-hook "/opt/certbot/au.sh python aly add" --manual-cleanup-hook "/opt/certbot/au.sh python aly clean"

6、配置crontab

vim /etc/crontab

3 3 */1 * * root /usr/bin/certbot renew --manual --preferred-challenges dns --manual-auth-hook "/data/certbot-letencrypt-wildcardcertificates-alydns-au-master/au.sh python aly add" --manual-cleanup-hook "/data/certbot-letencrypt-wildcardcertificates-alydns-au-master/au.sh python aly clean"

Copyright © 2024 aiapaas.com 粤ICP备 18086566号