Let‘s Encrypt 证书自动续期并自动应用到Nginx

文章目录

前言

就在今天,我又遇到了 Let's Encrypt 证书过期的情况,这已经是今年的底3次,巧的是每次过期都能在第二天发现,原以为3个月一次的过期我总能想起来,可现实还是太自信了,虽然是周期性的任务,但是单单靠人脑去记住这些是很不靠谱的,所以我还是把续期这件事交给定时任务吧。

其实安装certbot的时候我已经启动了证书定时续期的任务,关于certbot可以看看之前的文章《使用 Let's Encrypt 获取免费SSL证书》《Let's Encrypt免费证书的应用示例》

bash 复制代码
root@minigame:~# systemctl list-timers | grep certbot
Wed 2026-07-29 21:46:18 CST 9h left       Wed 2026-07-29 00:05:20 CST 11h ago      certbot.timer                certbot.service

但证书过期体现在应用端nginx,虽然证书正常续期了,但是nginx并没有加载这个新的正式,导致访问失效,web访问提示网站不安全,而Unity程序提示

复制代码
Error: 11:19:28-106 [BOOT] request version
Exception
Cysharp.Threading.Tasks.UnityWebRequestException:
Unknown Error
  at
Cysharp.Threading.Tasks.UnityAsyncExtensions+UnityWe
() [0x00000] in
<00000000000000000000000000000000>:0 , url [https://xxx.xxxx.cn:10499/infol, urllen = 52

解决的办法很简单,执行 nginx -s reload 就可以了,但是我不想下次还要收到执行了,所以我决定加一个定时任务,使用crontab每月执行一次,但是AI给了我一些更好的方案,我在这里只记录一个自认为最好的,操作步骤如下:

具体操作步骤

前提确认

certbot 自带定时任务,会自动检查证书到期时间并续期,无需自己写 cron 触发续期:

bash 复制代码
systemctl list-timers | grep certbot

只有临近过期(默认剩余 30 天内)的证书才会被真正续期,其余证书跳过。

步骤一:创建全局 deploy-hook 目录(如不存在)

bash 复制代码
sudo mkdir -p /etc/letsencrypt/renewal-hooks/deploy

步骤二:创建 reload 脚本

bash 复制代码
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh << 'EOF'
#!/bin/bash
systemctl reload nginx
EOF

步骤三:赋予可执行权限

bash 复制代码
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

验证权限(需带 x):

bash 复制代码
ls -l /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
# 期望输出: -rwxr-xr-x ...

步骤四:dry-run 验证 hook 是否生效

bash 复制代码
sudo certbot renew --dry-run

看到以下内容即为成功(dry-run 模式下会 skip 实际执行,属正常现象):

复制代码
Dry run: skipping deploy hook command: /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
...
Congratulations, all renewals succeeded.

实测输出

复制代码
root@minigame-nppa:/etc/letsencrypt/renewal-hooks/deploy# sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/bsdev.qnwl.cn.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for bsdev.qnwl.cn
Using the webroot path /var/www/letsencrypt for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Dry run: skipping deploy hook command: /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/bsdev.qnwl.cn/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/bsdev.qnwl.cn/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.

原理说明

项目 说明
触发条件 每次 certbot renew(由 systemd timer 每天跑 2 次触发),只有证书被真正续期成功才会执行 deploy-hook
作用范围 renewal-hooks/deploy/ 目录下的脚本对所有域名证书生效,新增证书无需再单独配置
优点 相比"每月固定 reload",只在证书真的更新后才 reload,避免无意义操作,也避免续期失败时误 reload
后续维护 无需人工干预;如新增域名证书,只要仍用 certbot 管理,会自动复用这个全局 hook

总结

  • 如果安装了certbot证书会自动更新,这种情况提示证书过期执行 nginx -s reload 就行了
  • 如果不想每个证书单独更新,按照文中操作将命令 systemctl reload nginx 写入可执行脚本 reload-nginx.sh
  • 然后将脚本放到 /etc/letsencrypt/renewal-hooks/deploy/ 目录下即可

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==


花未全开月未圆,半山微醉尽余欢。何须多虑盈亏事,终归小满胜万全。人生不必求圆满,留些清欢渡流年。持仓看淡涨跌势,心安即是上上签。

相关推荐
fatcoder9 小时前
玩转Nginx 04 — 反向代理:给 nginx 接上后端
前端·后端·nginx
明志数科10 小时前
宇树科技IPO背后的产业逻辑:人形机器人从“讲故事“到“交数据“
运维·服务器·数据库
外滩运维专家10 小时前
验证码接口上线三天就被刷了,我后来加的几道防线
运维
跨境小彭10 小时前
店群运营实操复盘:批量活动申报自动化优化方案
大数据·运维·人工智能·自动化·跨境电商·temu·temu电商运营
Jlzn888810 小时前
2026年锂电CCS组装线:车规储能双场景兼容自动化方案的技术架构与实践
运维·架构·自动化
AC赳赳老秦11 小时前
语义采集进阶实战:利用 OpenClaw AI 语义识别自动提取网页核心信息,无需手动编写选择器
java·运维·服务器·python·信息可视化·deepseek·openclaw
代码方舟11 小时前
企业级对公银行 KYC 架构:基于天远人脸身份证比对A构建自动化客户尽调网关
运维·人工智能·架构·自动化
fatcoder12 小时前
玩转Nginx 03 — location 匹配规则:让不同的路径各回各家
前端·后端·nginx
ShiXZ21312 小时前
网络调试四剑客:ping / telnet / nc / netstat 速查指令集
运维·开发语言·网络·php
小五传输12 小时前
Serv-U替代方案怎么选?政务机关文件安全传输对比与迁移指南
大数据·运维·安全