使用Certbot自动续期Let's Encrypt SSL证书

Let's Encrypt是一个免费的自动化证书颁发机构,让HTTPS对所有人触手可及。但很多服务器管理员容易忽视的一点是:Let's Encrypt证书每90天过期一次。如果没有正确配置自动续期,证书一旦过期,网站就会立即显示"不安全"警告。本文将手把手教你配置可靠的自动续期机制。

为什么Let's Encrypt证书每90天过期

90天的有效期是Let's Encrypt的有意设计,而非限制,其背后有充分的安全考量:

Certbot会在证书剩余不到30天时自动续期,实际上大约每60天续期一次,留有充足的缓冲时间。

💡 小贴士:通过Snap安装的Certbot(Ubuntu)会自动创建systemd timer。通过apt/yum安装的Certbot可能需要根据发行版手动设置cron job。

在Ubuntu/Debian/CentOS上安装Certbot

2026年推荐使用Snap包安装,这样可以始终获得最新版本的Certbot,并自动配置systemd timer。

Ubuntu 22.04 / 24.04(推荐)

sudo apt update sudo apt install snapd -y sudo snap install --classic certbot sudo ln -s /snap/bin/certbot /usr/bin/certbot

Debian 12

sudo apt update && sudo apt install certbot python3-certbot-nginx -y # 对于Apache: sudo apt install certbot python3-certbot-apache -y

CentOS / AlmaLinux / Rocky Linux

sudo dnf install epel-release -y sudo dnf install certbot python3-certbot-nginx -y

验证安装是否成功:

certbot --version # 预期输出:certbot 2.x.x

申请首张证书

配置自动续期之前,需要先申请初始证书。Certbot可以自动配置Web服务器:

Nginx

sudo certbot --nginx -d example.com -d www.example.com

Apache

sudo certbot --apache -d example.com -d www.example.com

独立模式(无Web服务器)

sudo certbot certonly --standalone -d example.com

Certbot会要求填写邮箱地址用于接收到期提醒,请填写真实且常用的邮箱。

⚠️ 注意:申请证书和续期时,防火墙必须开放80和443端口。Let's Encrypt使用HTTP-01或DNS-01验证来确认域名所有权。

使用systemd Timer设置自动续期

通过Snap安装的Certbot会自动创建名为snap.certbot.renew.timer的systemd timer,检查其状态:

sudo systemctl status snap.certbot.renew.timer # 对于apt安装的Certbot: sudo systemctl status certbot.timer

状态显示active (waiting)表示Certbot每天都会自动检查并续期证书。

查看下次运行时间:

sudo systemctl list-timers | grep certbot

如果timer未运行,手动启用:

sudo systemctl enable --now certbot.timer sudo systemctl enable --now snap.certbot.renew.timer

使用Cron Job设置自动续期

对于没有systemd的系统,或需要自定义执行时间,可以使用cron job:

sudo crontab -e

添加以下行,在每天凌晨3:30和下午3:30各执行一次续期检查(Let's Encrypt官方推荐):

0 3,15 * * * /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx"

对于Apache,将nginx替换为apache2:

0 3,15 * * * /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload apache2"

--quiet只在出错时输出信息,--deploy-hook仅在实际发生续期时才重载Web服务器。

验证自动续期是否正常工作

务必在证书实际过期前完成测试。

干运行测试(Dry Run)

sudo certbot renew --dry-run

看到Congratulations, all renewals succeeded表示配置正确。

查看当前证书过期时间

sudo certbot certificates

列出所有证书及其过期日期和剩余天数。

通过OpenSSL验证

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | \ openssl x509 -noout -dates

查看Certbot日志

sudo tail -50 /var/log/letsencrypt/letsencrypt.log

常见续期错误排查

错误:无法绑定IPv4/IPv6(端口80被占用)

使用--standalone模式时,Web服务器占用了端口80:

sudo systemctl stop nginx sudo certbot renew sudo systemctl start nginx

更好的方案:改用--nginx--apache插件,无需停止服务器。

错误:请求过多 / 超出频率限制

Let's Encrypt每域名每周限制50张证书。测试时使用暂存环境:

sudo certbot --nginx --staging -d example.com

错误:域名验证失败

请检查以下几点:

sudo ufw status nslookup example.com curl http://example.com/.well-known/acme-challenge/test

证书已续期但网站仍显示旧证书

Web服务器未重载,运行:

sudo systemctl reload nginx # 或 sudo systemctl reload apache2

或在Certbot续期配置文件中添加deploy hook:

sudo nano /etc/letsencrypt/renewal/example.com.conf # 添加: deploy_hook = systemctl reload nginx

Let's Encrypt与付费SSL对比

功能 Let's Encrypt 付费SSL(DV/OV/EV)
费用 免费 约¥200/年起
证书有效期 90天(自动续期) 1-3年
验证类型 仅DV DV / OV / EV
通配符SSL 支持(需DNS-01验证) 支持
多域名(SAN) 支持(最多100个域名) 支持
企业身份验证 不支持 支持(OV/EV显示公司名称)
赔付保障 $10,000–$1,750,000 USD
技术支持 社区论坛 24/7专业支持
适用场景 个人网站、开发、初创 企业、电商、金融

💡 建议:Let's Encrypt非常适合个人网站、博客和非支付类应用。对于电商或处理支付卡信息的商业网站,建议使用具有企业身份验证和赔付保障的OV或EV SSL证书。

在DirectAdmin中使用Let's Encrypt

使用DirectAdmin虚拟主机的用户无需安装Certbot,DirectAdmin已内置Let's Encrypt集成:

  1. 登录DirectAdmin控制面板
  2. 在账户管理中进入SSL证书页面
  3. 选择Let's Encrypt免费自动证书
  4. 点击保存
  5. DirectAdmin将自动颁发证书并配置自动续期

AsiaGB的所有主机方案均通过DirectAdmin内置了Let's Encrypt,无需额外费用,无需命令行操作。

常见问题解答

Let's Encrypt SSL证书多久过期一次?

Let's Encrypt SSL证书每90天过期一次。Certbot在剩余不到30天时自动续期,实际上大约每60天续期一次。

Certbot自动续期是如何工作的?

Certbot通过systemd timer或cron job每天检查证书。当证书剩余有效期少于30天时,自动下载新证书并重载Web服务器,全程无需停机。

Certbot续期失败会导致网站宕机吗?

旧证书在实际过期前仍然有效。一两次续期失败不会立即影响网站。但若续期持续失败至证书过期,浏览器将显示SSL错误警告。建议配置邮件提醒,及时发现续期异常。

DirectAdmin可以使用Let's Encrypt吗?

可以。DirectAdmin内置Let's Encrypt支持。在控制面板SSL证书页面启用即可,自动续期会自动配置,无需安装Certbot。

certbot renew --dry-run是什么?

--dry-run参数模拟整个续期流程,但不实际下载新证书。用于验证配置是否正确。在生产服务器上配置完成后建议先进行干运行测试。

Let's Encrypt支持通配符SSL吗?

支持,但需要DNS-01验证方式。运行:certbot certonly --manual --preferred-challenges dns -d "*.example.com",然后按提示在DNS中添加TXT记录即可。

总结

使用Certbot配置Let's Encrypt自动续期并不复杂,关键步骤如下:

AsiaGB的DirectAdmin虚拟主机用户无需进行上述任何操作,Let's Encrypt自动续期已在控制面板层面完成配置。如果您需要更高信任级别的SSL证书,欢迎了解AsiaGB提供的各类SSL产品。

需要更高信任级别的SSL证书?

AsiaGB提供DV、OV、EV及通配符SSL证书,含赔付保障和24/7专业支持,满足企业级网站需求。

查看SSL证书方案 →