centos nginx ssl证书如何续期
导读:CentOS 上 Nginx 的 SSL 证书续期指南 一 准备与检查 确认域名解析已指向服务器,且防火墙放行 TCP 80/443;证书续期需要通过 HTTP-01 或 DNS-01 验证域名所有权。 检查 Nginx 配置与目录:...
CentOS 上 Nginx 的 SSL 证书续期指南
一 准备与检查
- 确认域名解析已指向服务器,且防火墙放行 TCP 80/443;证书续期需要通过 HTTP-01 或 DNS-01 验证域名所有权。
- 检查 Nginx 配置与目录:
- 测试配置:
sudo nginx -t - 查看包含文件与根目录:
sudo nginx -T(便于确认 server 配置与根路径)
- 测试配置:
- 安装或更新 Certbot 及 Nginx 插件(以 CentOS 7/8 为例):
- 安装 EPEL:
sudo yum install -y epel-release - 安装 Certbot 与 Nginx 插件:
sudo yum install -y certbot python2-certbot-nginx - 验证插件:
sudo certbot plugins(应能看到 nginx 插件)
- 安装 EPEL:
- 证书路径通常为:/etc/letsencrypt/live/你的域名/fullchain.pem 与 privkey.pem。
二 自动续期方式
- 方式 A(推荐)使用 systemd timer(若系统提供):
- 查看状态:
sudo systemctl status certbot.timer - 如未启用,可启用并自启:
sudo systemctl enable --now certbot.timer - 说明:该方式由系统定时自动执行续期,无需手动维护 cron。
- 查看状态:
- 方式 B 使用 cron 定时任务:
- 编辑 root 定时任务:
sudo crontab -e - 每日检查并在续期后重载 Nginx(示例):
0 2 * * * /usr/bin/certbot renew --quiet --post-hook "systemctl reload nginx"
- 保存后可用
sudo crontab -l查看。
- 编辑 root 定时任务:
三 测试与验证
- 先做一次干跑测试:
sudo certbot renew --dry-run,看到 “The dry run was successful” 表示续期流程正常。 - 如需临时强制续期(仅测试):
sudo certbot renew --force-renewal(生产环境不建议频繁使用)。 - 验证 Nginx 配置与重载:
sudo nginx -t & & sudo systemctl reload nginx。 - 检查证书到期时间:
sudo certbot certificates。
四 特殊场景与常见问题
- 新增域名到现有证书:
- 扩展证书:
sudo certbot --nginx --expand -d 已有域名 -d 新域名
- 扩展证书:
- 无公网或无法开放 80/443 时,用 DNS 验证:
- 申请/续期:
sudo certbot certonly --manual --preferred-challenges dns -d 你的域名 - 按提示在 DNS 服务商处添加 TXT 记录完成验证;续期需再次添加,建议结合 DNS 服务商 API Hook 实现自动化。
- 申请/续期:
- 手动管理证书(不使用 --nginx 插件时):
- 在 Nginx 配置 80 端口的 server 块中放行验证路径:
location ^~ /.well-known/acme-challenge/ { root /var/www/html; }
- 验证通过后,在 443 端口 server 块中指定证书:
ssl_certificate /etc/letsencrypt/live/你的域名/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/你的域名/privkey.pem;
- 然后重载 Nginx:
sudo systemctl reload nginx。
- 在 Nginx 配置 80 端口的 server 块中放行验证路径:
- 防火墙与端口:确保 80/443 已放行(如 firewalld/云安全组)。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: centos nginx ssl证书如何续期
本文地址: https://pptw.com/jishu/787949.html
