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

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


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

相关推荐
kdxiaojie1 小时前
Linux 驱动研究 —— V4L2 (14)
linux·运维·笔记·学习
如若1231 小时前
Ubuntu 无 sudo 安装花生壳并实现 SSH 内网穿透:Conda 环境部署、冲突排查与自动启动
linux·运维·ubuntu·ssh·内网穿透
承渊政道2 小时前
Linux系统学习【掌握Ext系列⽂件系统的相关内容】
linux·运维·学习·文件系统·存储结构·inode
池以遇2 小时前
云原生——LVS
运维·云原生·lvs
Ivan CloudBay2 小时前
服务器关机后,网站会发生什么?
运维·服务器
数智化管理手记4 小时前
手工统计指标误差大、效率低?指标管理系统如何告别人工算数痛点?
大数据·运维·数据库·人工智能·云计算
实心儿儿11 小时前
Linux —— 进程间关系和守护进程
linux·运维·服务器
FII工业富联科技服务11 小时前
工业设备运维如何Agentic化?拆解Factory Brain的设备运维架构
大数据·运维·人工智能·架构·制造
Dawn-bit12 小时前
Linux磁盘管理详解
linux·运维·服务器·计算机网络·云计算