由于一直用的Let Encrypt的泛域名证书,这种证书只有3个月有效期。最近被过期整烦了决定尝试自动续期了
首先安装Certbot
snap install --classic certbot #安装Certbot
创建连接
ln -s /snap/bin/certbot /usr/bin/certbot #创建一个符号链接,确保可以执行certbot命令(相当于快捷方式)
由于我的服务器上已经安装了就不运行上面命令了。
再去阿里云控制台申请AccessKey ID,AccessKey Secret
服务区ECS地域ID
安装阿里云CLI 工具
GitHub - justjavac/certbot-dns-aliyun: 阿里云 DNS 的 certbot 插件,用来解决阿里云 DNS 不能自动为通配符证书续期的问题
wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz tar xzvf aliyun-cli-linux-latest-amd64.tgz mv aliyun /usr/local/bin
配置阿里云凭证
如何在阿里云CLI中配置身份凭证_阿里云CLI(CLI)-阿里云帮助中心
aliyun configure set --profile AkProfile --mode AK --access-key-id **** --access-key-secret **** --region ****
激活刚配置的凭证
aliyun ecs DescribeInstances --profile AkProfile
凭证保留位置: ~/.aliyun/
安装 certbot-dns-aliyun 插件
刚开始用了个pip的好像不起作用
pip install certbot-dns-aliyun -i https://mirrors.aliyun.com/pypi/simple/ --default-timeout=100 --no-cache-dir
wget https://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh sudo mv 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
如果上面wget下载不下来可以在本机用迅雷下载后上传到服务器
申请证书
先测试是否能正确申请
certbot certonly -d *.example.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
参数说明 - -d http://example.com:指定域名(可以指定多个域名)。 - manual:手动模式。 - preferred-challenges dns:使用 DNS 挑战。 - manual-auth-hook "alidns":在 DNS 挑战时调用 alidns 脚本。 - manual-cleanup-hook "alidns clean":在 DNS 挑战完成后调用 alidns clean 脚本。 - --dry-run:模拟续期过程,不会实际续期证书。正式申请时去掉--dry-run参数
证书续期
先测试是否能正常续期
certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
同样,正式续期时去掉 --dry-run
如果证书未到期,Certbot 会跳过续期。可以通过以下命令强制续期:
certbot renew --force-renewal --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
貌似可以加下面的参数指定证书生成路径
--config-dir /root/web/ssl/kecq
默认生成的证书路径在 /etc/letsencrypt/live/kecq.com
包含以下文件
cert.pem:这是你的 SSL 证书 文件。
privkey.pem:这是你的 私钥 文件,务必妥善保管,切勿泄露。
chain.pem:这是 中间证书链 文件,用于验证证书的完整性。
fullchain.pem:这是 完整证书链 文件,通常用于 Nginx 或 Apache 的 SSL 配置中。
自动续期
添加定时任务crontab
crontab -e
按1再输入以下内容
0 2 * * * /usr/bin/certbot renew --quiet --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload" >> /var/log/certbot-renew.log 2>&1
这一段可以不要
>> /var/log/certbot-renew.log 2>&1
参数说明 - 0 2 * :每天凌晨 2:00 执行任务。 - --quiet:静默模式,减少日志输出。 - --preferred-challenges dns:指定使用 DNS 挑战。 - --manual-auth-hook "alidns":在 DNS 挑战时调用 alidns 脚本。 - --manual-cleanup-hook "alidns clean":在 DNS 挑战完成后调用 alidns clean 脚本。 - --deploy-hook "nginx -s reload":在证书成功续期后重新应用 Nginx配置。 - >> /var/log/certbot-renew.log 2>&1:将日志输出到 /var/log/certbot-renew.log。
我是用的
1 1 */1 * * root certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload"
用下面命令查看任务
crontab -l
编辑当前用户的 crontab 文件
crontab -e
删除当前用户的 crontab 文件
解释
检查证书状态
certbot certificates
然后我还将此证书运用到OSS了 所以还需要搞定自动更新OSS上的证书......
2025.3.8
今天发现另一个域名fyj.me 的证书早过期了,而且nginx设置得不对,导致网站fyj.me无法访问,但这个域名的dns解析在腾讯云上,所以还研究下腾讯云如何实现
首先安装腾讯云命令行工具TCCLI
sudo pip install tccli
安装完后检查版本是否安装成功
tccli --version
配置凭证输入下面命令进入交换模式
tccli configure
然后
$ tccli configure TencentCloud API secretId [*afcQ]:************** TencentCloud API secretKey [*ArFd]:************** region: ap-guangzhou output[json]:
region 貌似不影响就输入ap-guangzhou 好了
output 时输入json
密钥可以在下面菜单中找到
注意secretKey只有在创建的时候才能看到,后续也无法查看,所以需要将这个保存下来。
然后去找cli命令
可以先运行下看是否成功
然后需要编写一个脚本,我存在了 /root/dnspod.sh里
#!/bin/bash RECORD_FILE="/tmp/_acme-challenge.${CERTBOT_DOMAIN}_${CERTBOT_VALIDATION}" if ! command -v tccli >/dev/null; then echo "TCCLI is required: https://cloud.tencent.com/document/product/440" 1>&2 exit 1 fi if [ "$1" = "clean" ]; then # 删除 DNS TXT 记录 RECORD_ID=$(cat ${RECORD_FILE}) if [ -n "${RECORD_ID}" ]; then tccli dnspod DeleteRecord --cli-unfold-argument \ --Domain ${CERTBOT_DOMAIN} \ --RecordId ${RECORD_ID} \ >/dev/null fi rm -f ${RECORD_FILE} else # 创建 DNS TXT 记录 RECORD_ID=$( tccli dnspod CreateRecord --cli-unfold-argument \ --Domain ${CERTBOT_DOMAIN} \ --SubDomain _acme-challenge \ --RecordType TXT \ --RecordLine 默认 \ --Value ${CERTBOT_VALIDATION} \ --TTL 600 \ | grep "RecordId" | grep -Eo "[0-9]+" ) echo ${RECORD_ID} > ${RECORD_FILE} sleep 20 # 等待 DNS 记录传播 fi
脚本中涉及到一些环境变量比如CERTBOT_DOMAIN、CERTBOT_VALIDATION等等,这是由certbot自动将相关的环境变量传递给这些脚本的。
用下面命令测试是否成功
certbot certonly -d *.fyj.me --manual --preferred-challenges dns --manual-auth-hook "/root/dnspod.sh" --manual-cleanup-hook "/root/dnspod.sh clean" --dry-run
由于之前的续期命令没有指定域名
certbot renew --force-renewal --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
所以还需要加上参数,起初我是certbot renew -d *.fyj.me --manual........... 发现报错
certbot renew --cert-name *.fyj.me --manual 这样也报错、
正确的是 certbot renew --cert-name fyj.me --manual 这个cert-name名称是证书名称
测试续期
certbot renew --cert-name fyj.me --manual --preferred-challenges dns --manual-auth-hook "/root/dnspod.sh" --manual-cleanup-hook "/ro ot/dnspod.sh clean" --dry-run
顺便将上面的定时任务给改了
可以直接编辑
/var/spool/cron/crontabs/root 文件
顺便改成每10天了
1 2 */10 * *
然后就发现遇到坑了
访问主域名网站 fyj.me发现证书不受信任,看了一圈才发现是因为*.fyj.me 而主域名fyj.me没有加入,但kecq.com没这种情况...
那么需要重新申请,不要直接删除证书目录否则certbot certificates 会报错,重新申请证书名称也会是 fyj.me-0001
执行
certbot delete --cert-name fyj.me
删除证书
再在申请证书的命令加上主域名
certbot certonly -d *.fyj.me -d fyj.me --manual --preferred-challenges dns --manual-auth-hook "/root/dnspod.sh" --manual-cleanup-hook "/root/dnspod.sh clean" --dry-run
即有两个 -d参数