使用Let’s Encrypt+certbot 实现ssl证书申请及自动续期


使用 Let’s Encrypt 和 Certbot 配置免费 SSL 证书,并实现自动化续期,是提升网站安全性的标准且高效的做法。整个过程主要分为两个步骤:证书申请自动续期配置。下面将为你详细拆解。

📝 证书申请

申请证书前,请确保你的域名已正确解析到服务器,且服务器开放了 80 (HTTP) 和 443 (HTTPS) 端口。

Certbot 的安装和证书获取命令会因你的操作系统和 Web 服务器(如 Nginx, Apache)而异。以下是两种最常见场景的操作示例。

场景一:使用 Nginx 服务器 (推荐)

这种方式最为自动化,Certbot 不仅会获取证书,还会自动修改 Nginx 配置文件并启用 HTTPS。

1. 安装 Certbot 和 Nginx 插件

  • Debian / Ubuntu

    sudo apt update
    sudo apt install certbot python3-certbot-nginx -y
    
  • CentOS / RHEL

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

2. 获取并安装证书
执行以下命令,将 example.comwww.example.com 替换为你的实际域名。

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

在交互过程中,Certbot 会询问你的邮箱(用于紧急通知和续期提醒),并要求同意服务条款。之后,它会询问是否将 HTTP 流量自动重定向到 HTTPS,选择“是”即可。

场景二:仅获取证书,手动配置

如果你希望自己手动配置 Web 服务器,或者 Web 服务不在上述列表,可以使用 certonly 模式。这里以最通用的独立模式 (standalone) 为例,运行该命令时会临时启动一个 web 服务器来验证域名所有权。

1. 安装 Certbot
安装方法同上,但无需安装 Web 服务器插件。

2. 获取证书

sudo certbot certonly --standalone -d example.com -d www.example.com --email your-email@example.com --agree-tos -n
  • --standalone: 使用独立模式,需要确保 80 或 443 端口未被占用。
  • -d: 指定域名,可以多次使用以添加多个域名到同一张证书。
  • --email: 指定你的邮箱。
  • --agree-tos: 同意服务条款。
  • -n: 非交互模式,直接运行。

成功执行后,你的证书文件将被保存在 /etc/letsencrypt/live/your-domain/ 目录下,其中最重要的两个文件是 fullchain.pem(证书公钥)和 privkey.pem(证书私钥)。之后,你需要在你的 Web 服务器或应用程序配置中手动指定这两个文件的路径。

🔄 自动续期配置

Let’s Encrypt 证书有效期为 90 天,因此自动续期是确保证书长期有效的关键。

1. Certbot 的自动续期机制

当你通过 snap 或系统包管理器(如 apt, yum)安装 Certbot 后,它通常会自动在系统中设置好定时任务,无需你额外干预。这个定时任务每天检查两次,如果证书距离过期不足 30 天,就会自动续期。

你可以通过以下命令查看这个定时任务是否存在:

  • 检查 systemd 定时器 (现代 Linux 发行版)
    sudo systemctl status certbot.timer
    
    如果看到 active (waiting) 状态,说明自动续期机制已在运行。
  • 检查 crontab 任务
    sudo crontab -l | grep certbot
    
    你应该能看到类似 0 */12 * * * ... certbot renew 的条目。
2. 如何验证与测试续期

在完全信任自动续期前,强烈建议你手动模拟一次续期流程,确保配置无误。

sudo certbot renew --dry-run

如果一切正常,你将看到 Congratulations, all renewals succeeded 的提示。

3. 续期后执行额外操作 (Hook)

有些服务(如上面提到的独立模式应用、Docker 容器等)在证书更新后需要重启才能加载新证书。Certbot 提供了 deploy-hook 参数(或将其脚本放入 /etc/letsencrypt/renewal-hooks/deploy/ 目录),让你可以在成功续期后自动执行命令。

例如,如果你使用 Docker,可以创建一个钩子脚本:

# 1. 创建 hook 脚本
sudo tee /etc/letsencrypt/renewal-hooks/deploy/restart-myapp.sh << 'EOF'
#!/bin/sh
docker restart your_container_name
EOF

# 2. 赋予执行权限
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/restart-myapp.sh

此后,每次 Certbot 成功续期证书,都会自动重启你的 Docker 容器。

如果续期定时任务不存在

你的系统上没有配置 Certbot 的自动续期任务。虽然证书目前有效(到 2026-07-12),但必须配置自动续期,否则 90 天后证书过期会导致网站 HTTPS 无法访问。

🔧 手动配置 Certbot 自动续期

方法一:使用 systemd timer(推荐,CentOS 9 默认)

# 检查 certbot 是否安装了 systemd 服务
ls /usr/lib/systemd/system/certbot*

# 如果存在 certbot-renewal.service,启用 timer
if [ -f /usr/lib/systemd/system/certbot.timer ]; then
    sudo systemctl enable certbot.timer --now
    sudo systemctl start certbot.timer
else
    # 如果没有 systemd 文件,使用方法二(crontab)
    echo "certbot.timer not found, using crontab instead"
    (sudo crontab -l 2>/dev/null; echo "0 3 * * * /usr/bin/certbot renew --quiet --post-hook 'systemctl reload nginx'") | sudo crontab -
fi

# 查看 timer 状态
sudo systemctl status certbot.timer 2>/dev/null || echo "Using crontab instead"

方法二:使用 crontab(通用方法)

# 编辑 root 用户的 crontab
sudo crontab -e

# 在打开的文件中添加以下内容(每天凌晨 3 点检查并续期)
# 按 i 进入编辑模式,粘贴下面这行,然后按 ESC,输入 :wq 保存退出
0 3 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"

方法三(我正在使用的):创建 systemd timer(完整配置)

如果你的系统缺少 certbot timer,可以手动创建:

# 1. 创建服务文件
sudo tee /etc/systemd/system/certbot-renewal.service << 'EOF'
[Unit]
Description=Certbot Renewal
Documentation=https://certbot.eff.org/docs/using.html

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --agree-tos
ExecStartPost=/bin/systemctl reload nginx
PrivateTmp=true
EOF

# 2. 创建定时器文件
sudo tee /etc/systemd/system/certbot-renewal.timer << 'EOF'
[Unit]
Description=Certbot Renewal Timer
Documentation=https://certbot.eff.org/docs/using.html

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target
EOF

# 3. 启用并启动定时器
sudo systemctl daemon-reload
sudo systemctl enable certbot-renewal.timer --now
sudo systemctl start certbot-renewal.timer

# 4. 查看定时器状态
sudo systemctl list-timers | grep certbot

✅ 验证自动续期配置

配置完成后,进行以下验证:

# 1. 查看定时任务
sudo crontab -l | grep certbot
# 或者查看 systemd timer
sudo systemctl list-timers | grep certbot

# 2. 测试续期命令(干运行,不会实际续期)
sudo certbot renew --dry-run

# 预期输出:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ** DRY RUN: The dry run was successful. **
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# 3. 手动测试续期(会检查是否需要续期)
sudo certbot renew

📊 检查当前证书状态

# 查看证书详细信息
sudo certbot certificates

# 输出示例:
# Found the following certs:
#   Certificate Name: bztc.fun
#     Domains: bztc.fun www.bztc.fun
#     Expiry Date: 2026-07-12 (valid: 89 days)
#     Certificate Path: /etc/letsencrypt/live/bztc.fun/fullchain.pem

🔍 验证续期后 Nginx 重载

确保证书续期后 Nginx 能自动重载配置:

# 测试 post-hook 是否工作
sudo certbot renew --dry-run --post-hook "systemctl reload nginx"

# 查看 Nginx 错误日志(确保没有 reload 错误)
sudo tail -f /var/log/nginx/error.log

📝 完整的证书管理命令

# 查看所有证书
sudo certbot certificates

# 手动续期所有证书
sudo certbot renew

# 强制续期(即使还没到期)
sudo certbot renew --force-renewal

# 删除证书(如果需要重新申请)
sudo certbot delete --cert-name bztc.fun

# 查看 Certbot 日志
sudo tail -f /var/log/letsencrypt/letsencrypt.log
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖天才、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值