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/ 目录下即可

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


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

相关推荐
程序员-Benothing3 小时前
Linux安全加固指南:账号、权限、服务、审计基线
linux·运维·安全
Mininglamp_27183 小时前
接口调用 vs 屏幕理解:两种 GUI 自动化架构的技术取舍
运维·架构·自动化
爱喝水的鱼丶6 小时前
SAP-ABAP:ECC→S/4HANA ABAP 迁移升级指南——程序改造、语法兼容与性能优化 导语
运维·开发语言·性能优化·sap·abap·经验交流·开发交流
木子欢儿6 小时前
openconnect Linux客户端安装 以及开机自启和登录
linux·运维·服务器
..Dauntless..6 小时前
【Linux】文件系统(一)
linux·运维·服务器
天空属于哈夫克36 小时前
企业微信API二次开发 赋能私域运营自动化
运维·自动化·企业微信
天远API7 小时前
零信任架构实战:基于天远运营商二要素V即时版构建自动化分时租赁出行会员身份一致性审核网关
运维·人工智能·架构·自动化
极光通讯7 小时前
液冷服务器对内存和SSD的适配要求:高密度部署下的散热与信号完整性
运维·服务器
2501_937767169 小时前
面向设计 / 研发部门:终端文件统一归档 + 服务器定时备份实践
运维·安全
重生之高冷汉堡包10 小时前
ChatGPT 如何打造自动化账号:从零搭建到高效运营
运维·chatgpt·自动化