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

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


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

相关推荐
姜鱼问生25 分钟前
三层防火墙排查:Nginx 配好了浏览器还打不开
nginx·安全
打工仔折腾 AI1 小时前
飞牛OS上用Docker Compose部署ExerciseDiary运动记录并配置远程访问
运维·人工智能·后端·python·docker·容器·ai agent 实战
天远数科1 小时前
零信任架构实战:基于天远运营商三要素简版V即时版查询构建自动化买手实名核验网关
运维·人工智能·架构·自动化
天衍四九-2 小时前
Docker Compose企业实战系列(八·终章):多环境隔离实战,开发/测试/生产一键切换
运维·docker·容器
IT大白鼠2 小时前
搜索系列 · 第 04 篇——部署实操:内网集群落地
运维·elasticsearch·nosql
Elastic 中国社区官方博客2 小时前
使用 Jev 作为 search reranker:基准测试与实现方法
大数据·运维·elasticsearch·全文检索
红红谈说3 小时前
浏览器自动化环境老是起不来?依赖治理与启动自检的一次复盘
运维·自动化·浏览器自动化·进程残留·环境依赖·启动自检·版本比较
赵民勇3 小时前
glib-compile-schemas命令详解
linux·运维
小小、码农4 小时前
〖Linux进程间通信〗IPC全家桶:匿名管道、FIFO、共享内存、消息队列与信号量一篇打通
linux·运维·服务器
阳光九叶草LXGZXJ4 小时前
达梦数据库-学习-67-SSL加密认证
linux·运维·数据库·sql·学习·ssl