通过Certbot自动申请更新HTTPS网站的SSL证书

原文地址:通过Certbot自动申请更新HTTPS网站的SSL证书

现在很多云服务运营商免费的HTTPS证书有效期只有3个月,对于个人网站来说,这就很麻烦,现在可以用 Certbot + Crontab 自动申请并定期更新 HTTPS 网站的 SSL 证书,来解决我们这个痛点。

需要云服务器可以看这里:云服务器优惠合集。

1. 安装 Certbot

不同系统安装方式略有不同,这里给你 CentOS / RHEL 和 Debian / Ubuntu 的常用方法。

CentOS / RHEL 7/8/9

bash 复制代码
# 安装 EPEL
sudo yum install epel-release -y

# 安装 Certbot 和 Nginx 插件(如果用 Apache 就换成 python3-certbot-apache)
sudo yum install certbot python3-certbot-nginx -y

Debian / Ubuntu

bash 复制代码
sudo apt update
sudo apt install certbot python3-certbot-nginx -y

2. 申请 SSL 证书

如果你用 Nginx

bash 复制代码
sudo certbot --nginx -d example.com -d www.example.com
  • -d 参数后面写你的域名(可以多个)
  • Certbot 会自动修改 Nginx 配置并申请证书
  • 申请过程中会让你选择是否重定向 HTTP → HTTPS

注意:这里需要你的nginx的conf文件里面的sercer_name指定对应的域名,如果你配置的是下划线 "_",会导致该命令运行失败,报下面的错误。

bash 复制代码
Could not automatically find a matching server block for zhangfeidezhu.com. Set the `server_name` directive to use the Nginx installer.

如果你用 Apache

bash 复制代码
sudo certbot --apache -d example.com -d www.example.com

3. 测试证书是否生效

申请完成后,可以在浏览器访问你的域名,查看是否已经是有效的 Let's Encrypt 证书。

4. 自动更新证书

Let's Encrypt 证书有效期是 90 天,所以要自动续签。

Certbot 自带定时任务(通常是 /etc/cron.d/certbot 或 systemd 定时器),也可以手动添加 crontab:

bash 复制代码
sudo crontab -e

加入:

bash 复制代码
0 3 1 */3 * certbot renew --quiet --post-hook "nginx -s reload"

解释:

  • 0 3 1 */3 * → 每隔 3 个月的1号3点执行一次
  • --quiet → 静默模式
  • --post-hook "nginx -s reload" → 续签后自动重载 Nginx

5. 检查自动续签是否正常

手动测试续签:

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

如果提示 Congratulations, all renewals succeeded,说明没问题。

✅ 这样配置后,你的网站 HTTPS 证书会在到期前自动续签,Nginx 会自动加载新证书,几乎可以做到"一劳永逸"。

相关推荐
北京耐用通信2 小时前
工业通信优选:耐达讯自动化实现CC-Link IE转Modbus RTU稳定传输
人工智能·物联网·网络协议·自动化·信息与通信
半壶清水2 小时前
[软考网规考点笔记]-局域网之HDLC 协议
网络·笔记·网络协议·考试
zjjsctcdl2 小时前
springBoot发布https服务及调用
spring boot·后端·https
进击的雷神3 小时前
突破POST分页与IP封锁:基于表单提交和代理转发的新闻爬虫设计
爬虫·网络协议·tcp/ip
孤影过客3 小时前
互联网谍战:HTTPS如何守护数据,以及头顶的量子阴云
网络协议·http·https
ETA85 小时前
面试官问SSE和WebSocket的区别?看这篇就够了(含心跳机制详解)
websocket·网络协议
汤愈韬5 小时前
BGP知识点解析
网络协议·网络安全·security
F1FJJ7 小时前
Shield CLI 的 PostgreSQL 插件 v0.4.0 已发布:支持 ER 图设计表关系,还能多人协作
网络·网络协议·postgresql·数据分析·开源软件
萝卜白菜。8 小时前
Http GET / 请求返回值不同的问题
网络·网络协议·http
liulilittle8 小时前
eBPF tc prog
服务器·网络·c++·网络协议·tcp/ip·性能·perf