Certbot自动续签多域名

发布时间:2026/11/6 7:29:58
Certbot自动续签多域名

安装Certbot

yuminstall-yepel-release yuminstall-ycertbot

执行续签命令

certbot certonly --preferred-challenges dns--manual-d*.domain1.com-ddomain1.com-d*.domain2.com-ddomain2.com-d*.domain3.com-ddomain3.com--serverhttps://acme-v02.api.letsencrypt.org/directory --register-unsafely-without-email --config-dir"cert"

按提示前往dns解析添加对应的值
完成后回车

生成的证书默认位置

/etc/letsencrypt/archive/domain.com

–config-dir 指定生成的目录位置

一键更新

certbot renew

开启自动续订

查看自己certbot和nginx的安装位置

whichcertbotwhichnginx


添加续订脚本

sudovi/root/renew_nginx.sh

写入以下内容,注意对应的指令路径要和上面查出来的一致

#!/bin/bash# 日志文件LOGFILE="/var/log/certbot-renew.log"CERTBOT="/usr/bin/certbot"NGINX="/usr/local/nginx/sbin/nginx"echo"[$(date)] 开始证书续订检查...">>"$LOGFILE"# 如果指定了路径需要修改为下面的指令# if $CERTBOT renew --config-dir /usr/local/nginx/conf/cert(目录) --quiet --no-random-sleep-on-renew; thenif$CERTBOTrenew--quiet--no-random-sleep-on-renew;thenecho"[$(date)] 证书检查完成,正在重载 Nginx...">>"$LOGFILE"$NGINX-sreloadecho"[$(date)] Nginx 已重载">>"$LOGFILE"elseecho"[$(date)] Certbot 续订失败!">>"$LOGFILE"exit1fi

保存退出,添加可执行权限

sudochmod+x /root/renew_nginx.sh

添加定时任务

sudocrontab-e

这个时候会弹出一个文本编辑器,里面可能已经有内容了,在下面插入一行新的内容,添加每天凌晨两点半执行一次脚本的任务

302* * * /root/renew_nginx.sh>>/var/log/certbot-cron.log2>&1

保存退出

测试脚本是否可用

sudo/root/renew_nginx.sh

查看执行的日志

sudotail-f/var/log/certbot-renew.log