安装Certbot
yum install -y epel-release
yum install -y certbot
执行续签命令
certbot certonly --preferred-challenges dns --manual -d *.domain1.com -d domain1.com -d *.domain2.com -d domain2.com -d *.domain3.com -d domain3.com --server https://acme-v02.api.letsencrypt.org/directory --register-unsafely-without-email
按提示前往dns解析添加对应的值
完成后回车
生成的证书默认位置
/etc/letsencrypt/archive/domain.com
一键更新
certbot renew
开启自动续订
查看自己certbot和nginx的安装位置
which certbot
which nginx
添加续订脚本
sudo vi /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 --quiet --no-random-sleep-on-renew; then
echo "[$(date)] 证书检查完成,正在重载 Nginx..." >> "$LOGFILE"
$NGINX -s reload
echo "[$(date)] Nginx 已重载" >> "$LOGFILE"
else
echo "[$(date)] Certbot 续订失败!" >> "$LOGFILE"
exit 1
fi
保存退出,添加可执行权限
sudo chmod +x /root/renew_nginx.sh
添加定时任务
sudo crontab -e
这个时候会弹出一个文本编辑器,里面可能已经有内容了,在下面插入一行新的内容,添加每天凌晨两点半执行一次脚本的任务
30 2 * * * /root/renew_nginx.sh >> /var/log/certbot-cron.log 2>&1
保存退出
测试脚本是否可用
sudo /root/renew_nginx.sh
查看执行的日志
sudo tail -f /var/log/certbot-renew.log