CentOS9上Let’s Encrypt自动续签

前言

要开启自动续签,我们需要切换到自动化模式。最佳方式是安装并使用阿里云 DNS 插件(certbot-dns-aliyun,它与标准的 certbot 兼容),这样 certbot 可以自动处理 DNS 验证,无需手动干预。
使用 certbot-dns-aliyun 插件时,将 certbot 也改为 Python/pip 版本(例如在虚拟环境 venv 中安装)确实是最佳实践,能有效避免依赖调度冲突,确保环境统一(如系统的 certbot 找不到 certbot-dns-aliyun)。

centos9 已经内置了 python3,所以我们无需手动安装 python 环境了,不过我们最好更新 pip

python3 -m pip install --upgrade pip

创建 python 虚拟环境

建议我们搞得这个 证书颁发,创建一个专属的虚拟环境,避免全局污染

创建专用的 venv(如果已经创建过,先删除 rm -rf ~/certbot-venv)

python3 -m venv ~/certbot-venv
source ~/certbot-venv/bin/activate  # 进入 env 环境(第一进入会自动激活 venv),退出请输入 deactivate

装云服务商的 DNS 插件

# 阿里云
pip install certbot-dns-aliyun
# 腾讯云
pip install certbot-dns-tencentcloud

验证是否安装成功:

pip3 list | grep aliyun
certbot plugins

配置云 API 凭证

创建凭证文件

mkdir -p ~/.secrets/certbot

添加内容(替换为你的真实密钥,阿里云在这里获取,腾讯云在这里获取。记得给这个账号配置管理 dns 的权限):

# 针对阿里云
vim ~/.secrets/certbot/aliyun.ini
dns_aliyun_access_key = your_access_key_id_here
dns_aliyun_access_key_secret = your_access_key_secret_here

#针对腾讯云
vim ~/.secrets/certbot/tencentcloud.ini
dns_tencentcloud_secret_id = xxxxxx
dns_tencentcloud_secret_key = AKxxxxxxxx

保存后设置权限

# 针对阿里云
chmod 600 ~/.secrets/certbot/aliyun.ini
#针对腾讯云
chmod 600 ~/.secrets/certbot/tencentcloud.ini

颁发证书

运行以下命令获取新证书(它是自动化版本的替代):

# 阿里云
certbot certonly \
  --authenticator dns-aliyun \
  --dns-aliyun-credentials ~/.secrets/certbot/aliyun.ini \
  -d "*.dingshaohua.com" -d "dingshaohua.com" \
  --preferred-challenges dns-01

# 腾讯云
certbot certonly \
  --authenticator dns-tencentcloud \
  --dns-tencentcloud-credentials ~/.secrets/certbot/tencentcloud.ini \
  -d "*.han96.com" -d "han96.com" \
  --preferred-challenges dns-01 \
  --email han96@email.com \
  --agree-tos \
  --non-interactive

这会自动添加 DNS TXT 记录、验证域名、获取证书。成功后,新证书将存储在 /etc/letsencrypt/live/dingshaohua-0001/ 或类似目录(certbot 会自动编号)。

注意:法“转换”现有 --manual 证书为自动模式,且之前的 --manual 证书不会被删除;你可以用 certbot delete 删除旧证书,如果不需要。

设置自动续签

测试续签(干运行):

certbot renew --dry-run
  • 如果成功,它会模拟续签新证书(不会实际申请)。

添加 cron 定时任务(每日早上 2:00 续签):

crontab -e
  • 添加一行(如果用 Apache,重载它;否则根据你的服务器调整):

    0 2 * * * /usr/bin/certbot renew --quiet --deploy-hook "sudo systemctl reload httpd" 2>> /var/log/letsencrypt.log
    
    • --quiet:静默运行。
    • --deploy-hook:续签后重载 Apache(reload httpd);如果不是 Apache,改为你的服务器命令(如 Nginx 的 reload nginx)。
    • 日志到 /var/log/letsencrypt.log,便于检查错误。
  • certbot 会自动检测即将过期的证书并续签(提前 30 天触发)。如果测试通过,你无需进一步操作。

迁移 Web 服务器配置(如适用)

  • 如果你用 Apache,certbot 已经处理;否则,手动配置:

    vi /etc/httpd/conf.d/ssl.conf  # 示例 Apache 配置
    
    • SSLCertificateFileSSLCertificateKeyFile 指向新证书路径。
  • 重载服务器:sudo systemctl reload httpd

其它

卸载旧的环境

如果在此之前,你已经搞过了全局 certbot,建议清理掉
卸载掉之前旧的全局安装方式

dnf remove certbot python3-certbot-apache python3-certbot-nginx #卸载旧的 certbot
dnf autoremove  # 清理无用依赖
rm -rf /etc/letsencrypt/*  # 删除旧的证书文件

检查 certbot 安装和证书状态

  • 检查 certbot 版本和已安装证书:

    certbot --version
    certbot certificates
    
    • 如果看到你的 dingshaohua.com 证书,确认其有效期(用 --manual 创建的,不能自动续签)。
  • 通常 90 天 证书就会过期。

posted @ 2025-12-02 17:51  丁少华  阅读(1)  评论(0)    收藏  举报