Certbot实现SSL免费证书自动续签(CentOS 7 + nginx/apache)

在 CentOS 上,你可以使用 Let's Encrypt 提供的 Certbot 工具来申请和自动续约免费的 SSL 证书。


1. 安装 Certbot

CentOS 7+ 安装 EPEL 和 Certbot

bash 复制代码
yum install -y epel-release
yum install -y certbot python3-certbot-nginx

如果使用的是 Apache:

bash 复制代码
yum install -y certbot python3-certbot-apache

CentOS 8+ (使用 DNF)

bash 复制代码
dnf install -y epel-release
dnf install -y certbot python3-certbot-nginx

2. 申请 SSL 证书

根据你的 Web 服务器,运行以下命令:

Nginx

bash 复制代码
certbot --nginx -d yourdomain.com -d www.yourdomain.com

Apache

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

首次运行时,它会:

  1. 验证域名所有权
  2. 自动配置 HTTPS 证书
  3. 证书默认有效期 90 天

PS:如果nginx不是默认的安装路径,可以手动指定并自行修改nginx的配置文件,参考Certbot实现SSL免费证书自动续签(CentOS 7版 + Docker部署的nginx) 中的方案2 webroot模式


3. 配置自动续约

Let's Encrypt 证书 90 天后会过期,因此需要定期续约。

检查 Certbot 是否正常续约

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

如果看到 "Congratulations, all renewals succeeded",说明续约正常。

设置定时任务(自动续约)

编辑 crontab:

bash 复制代码
crontab -e

添加以下行,每天凌晨 2 点自动检查并续约

Let's Encrypt 续约后,需要重启 Web 服务器使证书生效。可以在 crontab 里加上:

Nginx

bash 复制代码
0 2 * * * certbot renew --quiet --deplpy-hook "systemctl reload nginx"
或者
0 2 * * * certbot renew --quiet --deplpy-hook "nginx -s reload"

Apache

bash 复制代码
0 2 * * * certbot renew --quiet --deploy-hook "systemctl reload httpd"

5. 验证证书是否生效

bash 复制代码
certbot certificates
或者
openssl s_client -connect yourdomain.com:443 -servername yourdomain.com | openssl x509 -noout -dates

如果 Not After 日期更新,说明续约成功。✅

这样,你的 SSL 证书就会 自动续约,无需手动操作! 🚀

相关推荐
行思理7 小时前
Nginx 跨站请求(CORS)配置完全指南:从原理到生产实践
java·前端·nginx
RacentCLM7 小时前
证书即服务CaaS:从申请、续期到部署的自动化链路拆解
运维·数据库·自动化·ssl
李迟13 小时前
Nginx容器绑定81端口报拒绝访问错误的解决
运维·nginx
杨云龙UP13 小时前
生产实战:Keepalived + Nginx + MySQL 双向复制高可用架构部署与原理详解
运维·数据库·mysql·nginx·架构·keepalived·高可用架构
国际云,接待17 小时前
云服务器 WebSocket 空闲后频繁断开:查 Nginx 超时、心跳与负载均衡,再评估扩容
服务器·websocket·nginx
Linux界最忧郁之人17 小时前
OpenStack Victoria双节点实战|Packstack一键部署(CentOS‑Stream‑8)+华为云ECS实操
linux·运维·服务器·centos·华为云·openstack
庵中十三居士2 天前
用nginx代理DeepSeekHarness挂在域名下特定路径提供访问
运维·nginx·deepseek·harness
oyzz1202 天前
CentOS Stream 9 配置静态 IP 口令是多少
linux·tcp/ip·centos
fb_123452 天前
K8s 网络通关手册:Ingress-Nginx 生产级路由 + Calico 三种隧道模式 + NetworkPolicy 白名单实战
网络·nginx·kubernetes
承渊政道2 天前
新增一台服务器时还要重新装Nginx?Ansible只改hosts目标列表,同一份Playbook继续执行
运维·nginx·ansible·内网穿透·cpolar