Files
2026-05-16 23:27:38 +08:00

40 lines
1.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Let's Encrypt 证书续期脚本
# 流程:停服 → certbot renewstandalone)→ 修正证书权限 → 重启服务
# 由 crontab 每月执行一次(证书90天过期,提前30天续期)
ROOT="/home/ubuntu/change_cloth_server"
LOG="/home/ubuntu/log/renew_cert.log"
mkdir -p "$(dirname "$LOG")"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] === 开始证书续期 ===" >> "$LOG"
# 停止服务,释放 443 端口(certbot standalone 需要 80 端口,实际上不需要停止443)
# 但 certbot --standalone 需要 80 端口空闲即可,无需停止 443 服务
# 保险起见仍记录当前状态
pkill -f "${ROOT}/change_app.py" 2>/dev/null
echo " 服务已停止" >> "$LOG"
sleep 2
# 续期(standalone 模式使用 80 端口验证)
certbot renew --standalone --non-interactive >> "$LOG" 2>&1
RENEW_EXIT=$?
if [ $RENEW_EXIT -eq 0 ]; then
echo " 证书续期成功" >> "$LOG"
else
echo " 证书续期失败,退出码: $RENEW_EXIT" >> "$LOG"
fi
# 修正证书文件权限(确保非 root 用户可读)
chmod 644 /etc/letsencrypt/archive/xiangsilian.com/privkey*.pem 2>/dev/null
chmod 755 /etc/letsencrypt/live /etc/letsencrypt/archive 2>/dev/null
# 重启服务
export PATH="/home/ubuntu/.local/bin:$PATH"
nohup python3 "${ROOT}/change_app.py" >> "/home/ubuntu/log/change_app.log" 2>&1 &
echo " 服务已重启 (PID: $!)" >> "$LOG"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] === 续期流程结束 ===" >> "$LOG"