惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
C
Check Point Blog
博客园 - Franky
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
V2EX - 技术
V2EX - 技术
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Jina AI
Jina AI
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
H
Hackread – Cybersecurity News, Data Breaches, AI and More
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
Google Developers Blog
G
GRAHAM CLULEY
V
V2EX
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
Help Net Security
Help Net Security
大猫的无限游戏
大猫的无限游戏
C
CERT Recently Published Vulnerability Notes
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
S
Secure Thoughts
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
NISL@THU
NISL@THU
K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
宝玉的分享
宝玉的分享
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
博客园_首页
A
Arctic Wolf

博客园 - 丁少华

Neon白嫖免费pgsql 域名利用cloudflare免费图床 nginx反代CloudflarePages提示502 vite使用shadcn vite使用biome Window下Nginx winserver2022安装不上软件 mac已损坏无法打开 代码高亮 命令运行器之task 命令运行器之just windows开启wsl 白嫖Redis 白嫖MongoDB vercel无服务函数 无服务器函数完全指南 在线 mock 方案 解释型语言和编译型语言 何时该使用monorepo monorepo前端专属吗 Cursor锁区问题 Windows给文件夹别名 ai 常识 nestjs逆向工程Prisma与DTO nestjs的orm之Prisma
CentOS9上Let’s Encrypt自动续签
丁少华 · 2025-12-02 · via 博客园 - 丁少华

前言

要开启自动续签,我们需要切换到自动化模式。最佳方式是安装并使用阿里云 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 包版本(pip),建议升级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 天 证书就会过期。