Let's Encrypt (Certbot) 完整配置指南:从安装到自动续期

1. 文档概述

本文档提供在 AlmaLinux / RHEL 8/9 系列操作系统上,从零开始安装 Certbot、为 Nginx 服务器申请 SSL 证书并配置自动续期的完整流程。

2. 适用环境

  • 操作系统: AlmaLinux 8/9, Rocky Linux 8/9, RHEL 8/9, CentOS Stream 8/9。
  • Web 服务器: Nginx (已安装并运行,且域名已解析到服务器 IP)。
  • 网络要求: 服务器必须开放 80 (HTTP)443 (HTTPS) 端口供 Let's Encrypt 验证使用。
  • 权限要求: 需要 rootsudo 权限。

3. 第一阶段:安装 Certbot 及插件

在 AlmaLinux/RHEL 系统中,Certbot 通常包含在 EPEL (Extra Packages for Enterprise Linux) 仓库中。

3.1 启用 EPEL 仓库

如果尚未启用 EPEL,请先执行以下命令:

sudo dnf install epel-release -y

3.2 安装 Certbot 和 Nginx 插件

安装主程序及专门用于 Nginx 的插件(该插件能自动修改 Nginx 配置):

sudo dnf install certbot python3-certbot-nginx -y

3.3 验证安装

确认安装成功并查看可用的插件:

certbot --version
certbot plugins

输出中应包含 nginx 插件,表明安装正确。


4. 第二阶段:申请并配置 SSL 证书

前置条件

  1. 域名(例如 example.com)已正确解析到当前服务器的公网 IP。
  2. Nginx 服务正在运行 (systemctl status nginx)。
  3. 防火墙已放行 80 端口。

4.1 执行自动申请命令

使用 --nginx 参数,Certbot 会自动检测 Nginx 配置并完成证书申请与安装。

# 将 example.com 替换为您的真实域名
# 如需同时保护 www 子域名,可添加 -d www.example.com
sudo certbot --nginx -d example.com -d www.example.com

4.2 交互式配置说明

运行命令后,系统会提示以下信息,请按指引操作:

  1. 输入邮箱地址: 用于接收续期通知和紧急安全提醒(必填)。
  2. 同意服务条款: 输入 AY 同意。
  3. 是否加入 EFF 邮件列表: 输入 Y (同意) 或 N (拒绝),不影响功能。
  4. 重定向 HTTP 到 HTTPS:
    • 系统会询问是否将 HTTP 流量重定向到 HTTPS。
    • 推荐选择: 2 (Redirect - 强制跳转 HTTPS),这是最佳安全实践。

4.3 申请成功标志

如果看到以下输出,说明证书已成功申请并配置:

Congratulations! You have successfully enabled https://example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Your cert will expire on 2026-06-07. To obtain a new or tweaked version of this
certificate in the future, simply run certbot again with the "certonly" option.
To non-interactively renew *all* your certificates, run "certbot renew"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

此时,访问 http://example.com 应该会自动跳转到 https://example.com


5. 第三阶段:配置自动续期 (关键)

Let's Encrypt 证书有效期为 90 天,必须配置自动续期。现代 Linux 系统使用 systemd 定时器管理。

5.1 检查定时器状态

查看续期定时器是否已启用:

systemctl list-unit-files | grep certbot

应看到 certbot-renew.timer 状态为 enabled

5.2 激活定时器

重要:即使显示 enabled,定时器可能处于非活跃状态。必须手动启动一次以激活调度:

sudo systemctl start certbot-renew.timer

5.3 验证调度计划

确认定时器已设定下一次运行时间:

systemctl list-timers certbot-renew.timer

成功标志NEXT 列显示未来的具体时间(不再是 -)。

5.4 执行干跑测试 (Dry-Run)

模拟续期过程,确保脚本逻辑、网络和 Nginx 重载均无问题:

sudo certbot renew --dry-run

预期输出Congratulations, all simulated renewals succeeded


6. 维护与故障排查

6.1 查看日志

  • Systemd 日志: journalctl -u certbot-renew.service
  • Certbot 详细日志: /var/log/letsencrypt/letsencrypt.log

6.2 手动强制续期

如果不想等待自动触发,可手动执行续期(仅当证书剩余有效期 < 30 天时才真正更新):

sudo certbot renew

强制更新(即使未过期,用于测试):

sudo certbot renew --force-renewal

6.3 常见问题

问题 解决方案
端口 80 被占用 确保没有其他程序占用 80 端口,或暂时停止 Nginx (systemctl stop nginx) 后再运行申请命令(使用 standalone 模式)。
DNS 解析失败 确认域名已正确解析到服务器 IP,且全球 DNS 已生效。
防火墙阻挡 检查 firewalldiptables 是否放行 TCP 80/443 端口。
Nginx 配置错误 运行 nginx -t 检查配置文件语法是否正确。

7. 总结清单

完成以下所有步骤即表示配置成功:

  • [ ] 安装 certbotpython3-certbot-nginx
  • [ ] 成功运行 certbot --nginx 并获得证书。
  • [ ] 浏览器访问 HTTPS 正常,HTTP 自动跳转。
  • [ ] 运行 systemctl start certbot-renew.timer 激活定时器。
  • [ ] systemctl list-timers 显示下次运行时间。
  • [ ] certbot renew --dry-run 测试通过。

至此,您的网站已拥有免费、自动续期的 HTTPS 加密保护。

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注