通过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 会自动加载新证书,几乎可以做到"一劳永逸"。

相关推荐
DONSEE广东东信智能读卡器9 小时前
用PowerShell实现Windows 本地 WSS/HTTPS 自签名证书配置方法
windows·网络协议·https·powershell·身份证阅读器
2501_916007479 小时前
iOS开发中抓取HTTPS请求的完整解决方法与步骤详解
android·网络协议·ios·小程序·https·uni-app·iphone
Irissgwe9 小时前
一、网络基础概念
linux·网络·websocket·网络协议·socket·linux网络编程
treesforest10 小时前
2026年,IP地理位置精准查询的几个硬核技术变化
运维·网络·网络协议·tcp/ip·ip
yqcoder11 小时前
数据的“包装方式”:深入解析 HTTP Content-Type
网络·网络协议·http
pengyi87101513 小时前
共享 IP 防封维护策略,降低被封率、延长 IP 寿命
网络·网络协议·tcp/ip
风度前端13 小时前
阿里云宝塔面板部署https证书
linux·后端·https
wu@5555513 小时前
使用acme生成免费https泛域名证书(通配符证书)
网络协议·http·https
Bat U14 小时前
JavaEE|网络原理TCP/IP
网络·网络协议·tcp/ip
冰冰的米咖14 小时前
20260521网络配置-NAT Server实现外网访问内网服务
网络·网络协议·智能路由器