Linux · 2020年9月9日 0

certbot通用证书申请及自动更新

看的certbot原文档中说可以自动更新,但是按文档做了以后自动更新一直没有成功过,通过查找资料发现每次更新需要操作需要对域名解析做一些操作才行,所以有一个操作权限的问题,整理如下,成功更新证书。

0、下载certbot-auto
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

1、下载脚本工具
git clone https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au
cd certbot-letencrypt-wildcardcertificates-alydns-au
chmod 0777 au.sh autxy.sh augodaddy.sh python-version/au.sh

2、配置
根据要更新的域名的提供商,选择相应的脚本:

阿里云(根据环境二选一即可):
au.sh (PHP 环境)
python-version/au.py:(Python环境,兼容Python 2/3)
腾讯云:autxy.sh(PHP 环境)
GoDaddy:augodaddy.sh(PHP 环境)

备注:环境-代表要执行脚本的服务器支持的环境

修改DNS API 密钥:
alydns.php,修改 accessKeyId、accessSecrec 变量, API key 和 Secrec 官方申请文档。
txydns.php,修改 txyaccessKeyId、txyaccessSecrec 变量, API 密钥官方申请文档。
python-version/alydns.py,修改 ACCESS_KEY_ID、ACCESS_KEY_SECRET, API key 和 Secrec 官方申请文档。
godaddydns.php,修改 accessKeyId、accessSecrec 变量,GoDaddy API 密钥官方申请文档。

备注:需要通过 API 操作阿里云 DNS 或腾讯云 DNS 的记录,所以需要去域名服务商哪儿获取 API 密钥。

4:申请证书
特别说明: –manual-auth-hook 指定的 hook 文件四个任选其一(au.sh、autxy.sh、augodaddy.sh、python-version/au.sh),其他操作完全相同。

测试是否有错误
$ ./certbot-auto certonly -d *.example.com –manual –preferred-challenges dns –dry-run –manual-auth-hook /脚本目录/au.sh(autxy.sh 或 python-version/au.sh,下面统一以 au.sh 介绍)

实际申请
$ ./certbot-auto certonly -d *.example.com –manual –preferred-challenges dns –manual-auth-hook /脚本目录/au.sh

参数解释:
certonly:表示采用验证模式,只会获取证书,不会为web服务器配置证书
–manual:表示插件
–preferred-challenges dns:表示采用DNS验证申请者合法性(是不是域名的管理者)
–dry-run:在实际申请/更新证书前进行测试,强烈推荐
-d:表示需要为那个域名申请证书,可以有多个。
–manual-auth-hook:在执行命令的时候调用一个 hook 文件
如果你想为多个域名申请通配符证书(合并在一张证书中,也叫做 SAN 通配符证书),直接输入多个 -d 参数即可,比如:

$ ./certbot-auto certonly -d *.liwf1.net -d *.liwf2.net -d liwf.net –manual –preferred-challenges dns –dry-run –manual-auth-hook /脚本目录/au.sh

5、续期证书
1:对机器上所有证书 renew
$ ./certbot-auto renew –manual –preferred-challenges dns –manual-auth-hook /脚本目录/au.sh

2:对某一张证书进行续期
先看看机器上有多少证书:
$ ./certbot-auto certificates

renew 指定域名证书:
$ ./certbot-auto renew –cert-name simplehttps.com –manual-auth-hook /脚本目录/au.sh

6、加入 crontab自动更新证书
编辑文件 /etc/crontab,由于证书有效期是3个月,我们设置一个月进行一次更新
crontab -e
0 19 1 * * /bin/sh /opt/script/autossl.sh
autossl.sh:
/root/certbot-auto renew –cert-name liwf.net -deploy-hook “nginx -s reload” –manual-auth-hook ./dev/certbot-letencrypt-wildcardcertificates-alydns-au/au.sh
其中:renew成功后nginx自动重新加载配置

注意:只有单机建议这样运行,如果要将证书同步到多台web服务器,需要有别的方案

原文地址:https://blog.liwf.net/2019/02/25/certbot%E9%80%9A%E7%94%A8%E8%AF%81%E4%B9%A6%E7%94%B3%E8%AF%B7%E5%8F%8A%E8%87%AA%E5%8A%A8%E6%9B%B4%E6%96%B0/