Let's Encrypt SSL 证书过期了?5 分钟快速续期指南

作者:程序员罗尼 | 发布:2026-02-07 | 原文:https://mp.weixin.qq.com/s/1Vz8TYbsvxJT6aelQHNI_g


前言

最近在阿里云 ECS 上部署的服务突然无法访问,浏览器提示"证书已过期"。检查后发现是 Let's Encrypt 的 SSL 证书到期了。Let's Encrypt 证书有效期只有 90 天,虽然通常会自动续期,但偶尔也会出现需要手动处理的情况。

今天就来分享一下如何快速续期 Let's Encrypt 证书,让你的 HTTPS 服务恢复正常。

问题背景

Let's Encrypt 是目前最流行的免费 SSL 证书服务,但它的证书有效期只有 90 天。虽然 certbot 通常会配置自动续期,但在某些情况下(比如服务器重启、定时任务失效等),证书可能会过期。

快速续期方案

方法一:一键续期(推荐)

这是最简单快捷的方法,适用于证书即将过期或刚过期的情况:

# SSH 登录到你的 ECS 服务器后执行:  
# 1. 手动续期证书sudo certbot renew  
# 2. 如果续期成功,重新加载 nginx 使新证书生效sudo nginx -t && sudo systemctl reload nginx# 或使用 service 命令(取决于你的系统)sudo nginx -t && sudo service nginx reload

执行流程:

  • certbot renew 会检查所有已安装的证书,如果证书在 30 天内过期,会自动续期
  • nginx -t 测试配置文件语法是否正确
  • systemctl reload nginx 重新加载配置,无需停止服务

方法二:强制续期

如果方法一失败,或者证书已经过期较久,可以强制重新申请:

# 强制续期指定域名sudo certbot --nginx -d your-domain.com --force-renewal  
# 验证并重启 nginxsudo nginx -tsudo systemctl reload nginx

检查证书状态

续期完成后,建议检查一下证书是否更新成功:

# 查看所有证书状态sudo certbot certificates  
# 或直接查看证书有效期sudo openssl x509 -in /etc/letsencrypt/live/your-domain.com/fullchain.pem -noout -dates

输出会显示证书的生效日期和过期日期,确认是否已更新。

设置自动续期(重要!)

为了避免证书再次过期,建议确保自动续期功能正常工作。certbot 支持两种自动续期方式:systemd timer 和 cron。

方式一:使用 systemd timer(推荐)

如果你的系统支持 systemd,certbot 通常会自动安装 timer:

# 检查 certbot 定时任务状态sudo systemctl status certbot.timer  
# 如果没有启用,执行以下命令启用sudo systemctl enable certbot.timersudo systemctl start certbot.timer

方式二:使用 cron(如果 systemd timer 不存在)

如果遇到 Unit file certbot.timer does not exist 错误,说明你的系统可能没有 systemd timer,或者 certbot 是通过其他方式安装的。这时可以使用 cron 来设置自动续期:

# 编辑 root 用户的 crontabsudo crontab -e  
# 添加以下行(每天凌晨 3 点检查并续期证书,续期成功后重新加载 nginx)0 3 * * * certbot renew --quiet && systemctl reload nginx

说明:

  • 0 3 * * * 表示每天凌晨 3 点执行
  • --quiet 参数表示只在有错误时输出信息
  • && systemctl reload nginx 表示续期成功后重新加载 nginx

或者使用更详细的版本(推荐):

# 添加以下行到 crontab0 3 * * * /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx"

这个版本使用 --deploy-hook 参数,只有在证书实际更新时才会重新加载 nginx,更加高效。

验证自动续期配置

无论使用哪种方式,都可以通过以下命令验证:

# 检查 systemd timersudo systemctl list-timers | grep certbot  
# 或检查 crontabsudo crontab -l | grep certbot

确保自动续期功能正常工作,就能避免证书过期的问题。

常见问题排查

如果续期过程中遇到问题,可以按以下步骤排查:

1. 查看详细日志

# 查看 certbot 执行日志sudo tail -f /var/log/letsencrypt/letsencrypt.log

日志会显示具体的错误信息,比如域名验证失败、网络问题等。

2. 检查 nginx 配置

# 确认证书路径配置正确sudo grep -r "ssl_certificate" /etc/nginx/

确保 nginx 配置中的证书路径指向 Let's Encrypt 的证书目录(通常是 /etc/letsencrypt/live/your-domain.com/)。

3. 确保 80 端口开放

Let's Encrypt 使用 HTTP-01 验证方式,需要访问服务器的 80 端口:

# 检查 80 端口是否开放sudo netstat -tlnp | grep :80  
# 检查防火墙规则sudo iptables -L -n | grep 80# 或使用 firewalldsudo firewall-cmd --list-ports

4. 测试续期(不实际执行)

如果不确定续期是否会成功,可以先测试:

# 模拟续期过程,不会实际更新证书sudo certbot renew --dry-run

5. systemd timer 不存在的问题

如果执行 sudo systemctl enable certbot.timer 时提示 Unit file certbot.timer does not exist,说明你的系统可能:

  1. certbot 是通过 pip 或其他方式安装的,而不是通过系统包管理器
  2. 系统版本较旧,certbot 没有自动创建 timer
  3. 使用的是 cron 而不是 systemd

解决方案: 使用 cron 设置自动续期(见上面的"方式二:使用 cron"部分)。

最佳实践建议

  1. 定期检查:建议每月检查一次证书状态,确保自动续期正常工作
  2. 监控告警:可以设置监控脚本,在证书过期前 7 天发送告警
  3. 备份配置:续期前备份 nginx 配置文件,以防万一
  4. 使用 DNS 验证:如果服务器无法开放 80 端口,可以使用 DNS-01 验证方式

总结

Let's Encrypt 证书续期其实很简单,主要步骤就是:

  1. 执行 sudo certbot renew
  2. 重新加载 nginx 配置

关键是要确保自动续期功能正常工作,这样就能避免证书过期的问题。如果遇到问题,查看日志文件通常能找到原因。

希望这篇文章能帮助到遇到同样问题的朋友。如果你有其他问题或更好的方法,欢迎在评论区分享!


相关阅读:

  • Let's Encrypt 官方文档

  • Certbot 用户指南

  • 本文采用「人言兑.md」自动排版 -

END