在 CentOS 7 上使用 Certbot 自动部署和续期 SSL 证书的推荐方式涉及使用 EPEL (Extra Packages for Enterprise Linux) 仓库,并确保 Certbot 自动进行续期


Let's Encrypt官网:https://letsencrypt.org/


前置操作安装nginx可参考:CentOS安装nginx并配置环境变量


1. 添加 EPEL 仓库

首先,您需要确保 EPEL 仓库已经添加到您的 CentOS 系统中。这可以通过运行以下命令来完成:

sudo yum install epel-release

2. 安装 Certbot

安装 Certbot 以及它针对 Nginx 的插件:

sudo yum install certbot python2-certbot-nginx

3. 重启 Nginx 以应用配置更改:

/usr/local/nginx/sbin/nginx -s reload

4. 获取 SSL 证书

运行 Certbot,并让它自动配置 SSL 证书和更新 Nginx 配置:

sudo certbot --nginx --nginx-server-root /usr/local/nginx/conf \
--nginx-ctl /usr/local/nginx/sbin/nginx -d example.com -d www.example.com

执行命令时如果提示:

Requesting a certificate for xxx.com and www.example.com
Performing the following challenges:
http-01 challenge for example.com
http-01 challenge for www.example.com
Waiting for verification...
Challenge failed for domain example.com
Challenge failed for domain www.example.com
http-01 challenge for example.com
http-01 challenge for www.example.com
Cleaning up challenges
Some challenges have failed.

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: www.example.com
   Type:   unauthorized
   Detail: xx.xxx.xx.xx: Invalid response from
   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address.

使用 Certbot 获取 SSL 证书时遇到 DNS 查询超时的问题,这通常意味着 Let's Encrypt 无法从外部解析你的域名。在这种情况下,一个可靠的解决方法是使用 DNS-01 挑战来证明你对域名的控制权。这种方法涉及到在你的 DNS 记录中添加一条 TXT 记录。

使用 DNS-01 挑战配置 TXT 记录

  1. 安装 Certbot:确保 Certbot 已经安装在你的系统上。如果还未安装,你可以通过官方推荐的方式进行安装。

  2. 生成 DNS 挑战:运行 Certbot 并指定使用 DNS 挑战验证:

sudo certbot certonly --manual --preferred-challenges dns -d example.com -d example.com

这个命令会告诉 Certbot 仅获取证书,使用手动方式,选择 DNS 作为首选的验证方法,并且针对 example.comwww.example.com

添加 TXT 记录:Certbot 会提供一条 TXT 记录的信息,例如:

Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:

XYZ1234567890ABCDEFGH
  1. 登录到你的 DNS 提供商的管理界面,添加一条 TXT 记录。记录名称通常是 _acme-challenge.example.com,值为 Certbot 提供的 XYZ1234567890ABCDEFGH

  2. 等待 DNS 传播:DNS 更改可能需要一些时间才能全球传播。这个时间从几分钟到几小时不等,取决于你的 DNS 提供商和 TTL(生存时间)设置。

  3. 完成验证并获取证书:确认 TXT 记录已正确添加并传播后,继续执行 Certbot 指令完成验证。如果验证成功,Certbot 会下载证书和密钥文件到指定的目录。

注意事项

  • 在你添加 TXT 记录并验证之前,不要删除或修改记录。Let's Encrypt 可能会在几个不同的时间点对记录进行检查。

  • 确保你的域名解析设置是公开的,并且没有 DNS 服务器的可访问性问题。

  • 如果你的域名正在使用像 Cloudflare 这样的 CDN 或代理服务,请确保正确配置以允许 Let's Encrypt 的服务器进行 DNS 查找。

使用 DNS-01 挑战方式,虽然步骤较多,但能有效解决在某些复杂网络环境中出现的证书验证问题。


txt解析完成后再次执行 4. 获取 SSL 证书 命令

按照提示操作,包括提供电子邮件地址,同意服务条款,以及选择是否自动重定向 HTTP 流量到 HTTPS。

5. 自动续期设置

Certbot 默认已经配置了证书的自动续期。它通过创建一个 cron 或 systemd 任务来实现。您可以检查这个任务是否正确安装:

sudo systemctl list-timers | grep certbot

或者检查 cron 任务:

sudo crontab -l

6. 测试续期过程

您可以模拟证书续期过程,确保续期配置无误:

sudo certbot renew --dry-run

如果这个命令执行过程中没有错误,那么您的证书应该能够自动续期。

以上步骤可以帮助您在 CentOS 7 上通过 Certbot 和 Nginx 自动部署和续期 Let's Encrypt SSL 证书。如果您遇到任何问题,

检查 Certbot 的日志文件通常很有帮助,日志位于 /var/log/letsencrypt 目录下。


7. 关闭证书的自动续期任务

使用 cron 任务的系统

  1. 查看 cron 任务:首先,检查当前用户的 cron 任务列表。

crontab -l

如果 Certbot 是以 root 用户安装的,你可能需要检查 root 的 cron 任务:

sudo crontab -l

编辑或删除 Certbot 任务:如果找到了与 Certbot 相关的任务,你可以编辑 cron 任务来删除或注释这一行。

crontab -e

然后找到类似以下的行:

0 */12 * * * root certbot renew --quiet

你可以将其注释掉(在行首添加 #):

# 0 */12 * * * root certbot renew --quiet

或者直接删除这一行。保存并退出编辑器。