当前位置: 首页 > news >正文

CentOS8+Nginx环境使用Certbot+Let‘s Encrypt 配置免费ssl证书及自动续期

在 CentOS 8 上配置 SSL 证书并自动续期,使用 Let’s Encrypt 的免费证书和 Certbot 工具。
Certbot 是 Let’s Encrypt 官方推荐的客户端工具,其默认行为是向 Let’s Encrypt 的 ACME 服务器申请证书,无需手动指定 CA。Let’s Encrypt 证书有效期为 90 天。

一、准备工作

1. 安装必要工具

sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y  # 如果使用 Nginx

2. 确保防火墙开放 HTTP/HTTPS

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

二、生成 SSL 证书

方式 1:自动配置(推荐)
sudo certbot --nginx   # 使用 Nginx
  • 按提示输入域名和邮箱,Certbot 会自动完成证书申请和 Web 服务器配置
  • 注意:若 Nginx 是离线编译安装的(路径为 /usr/local/nginx),需通过以下方式指定路径:
方法 1:通过 Certbot 配置文件指定路径(推荐)
sudo vim /etc/letsencrypt/cli.ini

添加内容:

# 指定 Nginx 二进制路径和配置目录
nginx-ctl = /usr/local/nginx/sbin/nginx
nginx-server-root = /usr/local/nginx/conf

重新运行 Certbot:

sudo certbot --nginx
方法 2:使用符号链接统一路径
sudo ln -s /usr/local/nginx/conf /etc/nginx
sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
sudo certbot --nginx --dry-run  # 验证
方法 3:使用命令参数指定
sudo certbot --nginx \
  --nginx-ctl /usr/local/nginx/sbin/nginx \
  --nginx-server-root /usr/local/nginx/conf

方式 2:手动配置(适合自定义需求)

sudo certbot certonly --standalone -d example.com -d www.example.com

证书保存路径:/etc/letsencrypt/live/example.com/


三、配置 Web 服务器

自动配置(推荐)

sudo certbot --nginx

手动配置示例

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers on;
}

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;  # 强制 HTTPS 重定向
}

重启 Nginx:

sudo nginx -t        # 测试配置
sudo nginx -s reload

四、配置自动续期

1. 手动测试续期

sudo certbot renew --dry-run

成功输出:Congratulations, all renewals succeeded

2. 添加定时任务

echo "0 0,12 * * * root /usr/bin/certbot renew --quiet" | sudo tee -a /etc/cron.d/certbot

3. 续期后自动重启 Web 服务器

创建续期钩子脚本:

sudo mkdir -p /etc/letsencrypt/renewal-hooks/deploy
sudo vi /etc/letsencrypt/renewal-hooks/deploy/restart-nginx.sh

脚本内容:

#!/bin/bash
/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload

赋予权限:

sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/restart-nginx.sh

五、验证与维护

1. 检查证书状态

sudo certbot certificates

2. 查看自动续期日志

journalctl -u certbot.service

3. 定期检查

  • 确保域名解析正确
  • 避免修改 /etc/letsencrypt/ 目录权限
  • 更新 Certbot 和 Web 服务器软件

附录

certbot renew --dry-run 命令详解

命令作用
sudo certbot renew --dry-run
  • 核心功能:模拟 SSL 证书自动续期流程(不实际颁发新证书)
  • 核心价值:提前发现配置错误、网络问题或权限故障,规避证书过期风险
  • 注意事项
    • 不会延长证书有效期
    • 实际续期需等待证书到期前 30 天自动触发
    • 同一域名每小时最多执行 5 次(避免触发 Let’s Encrypt 速率限制)

输出结果解析
成功场景
----------------------------------------
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/example.com/fullchain.pem (success)
----------------------------------------
  • 解读:所有证书模拟续期测试通过,实际续期成功率 >95%
失败场景
----------------------------------------
The following errors were reported by the server:
  Domain: example.com
  Type:   connection
  Detail: Fetching http://example.com/.well-known/acme-challenge/xxx:
    Timeout during connect (likely firewall problem)
----------------------------------------

echo "0 0,12 * * * root /usr/bin/certbot renew --quiet" | sudo tee -a /etc/cron.d/certbot 命令详解

1. 时间规则拆解
0 0,12 * * *
字段含义
分钟0整点执行
小时0,12UTC 时间 0 点和 12 点
日/月/周*每天/每月/每周生效
2. 命令执行逻辑
组件技术说明
root以 root 权限运行(访问证书文件需特权)
/usr/bin/certbot renew续期所有到期前 30 天内的证书
--quiet静默模式(抑制非关键日志输出)
3. 配置文件写入
| sudo tee -a /etc/cron.d/certbot
  • 管道符 |:传递 echo 命令的输出流
  • tee -a:追加写入系统级 Cron 配置
  • 文件权限/etc/cron.d/ 目录需 root 权限

renewal和renewal-hooks目录作用

目录功能对比
目录核心作用典型文件
/etc/letsencrypt/renewal存储证书续订元数据(域名/有效期/验证方式)example.com.conf
/etc/letsencrypt/renewal-hooks管理续订生命周期钩子脚本post/restart-nginx.sh
1. renewal 目录实例
# example.com.conf
renew_before_expiry = 30 days    # 到期前30天触发续订
preferred_chain = ISRG Root X1   # 优先使用ISRG根证书链
authenticator = nginx            # 使用Nginx插件验证
account = xxxxxxxxxxxxxxx        # Let's Encrypt账户ID
2. renewal-hooks 工作流程
/etc/letsencrypt/renewal-hooks/
├── pre/    # 续订前操作(示例:暂停负载均衡)
├── post/   # 续订后操作(示例:重启服务)
└── deploy/ # 证书部署操作(示例:同步到CDN)
钩子脚本示例

post/restart-nginx.sh

#!/bin/sh
# 安全重启Nginx服务
/usr/local/nginx/sbin/nginx -t && \
/usr/local/nginx/sbin/nginx -s reload

权限配置:

sudo chmod +x /etc/letsencrypt/renewal-hooks/post/restart-nginx.sh
http://www.dtcms.com/a/100047.html

相关文章:

  • Windows Server2019搭建FTP服务器
  • iOS审核被拒:Missing privacy manifest 第三方库添加隐私声明文件
  • 5G_WiFi_CE_标称带宽/占用带宽
  • SALV无废话教程
  • Python小练习系列 Vol.11:回文数筛选(filter + 字符串反转)
  • AF3 nonensembled_transform_fns函数解读
  • AI日报 - 2025年03月31日
  • Qt WebSockets使用
  • 《非暴力沟通》第七章 “用全身心倾听” 总结
  • 算法每日一练 (23)
  • 深入理解 Windows 进程管理:taskkill 命令详解
  • 《电子武林争霸赛:MOSFET少侠 vs 三极管长老》
  • axios使用
  • 网络空间安全(44)Web实战篇
  • 硬件学习笔记--56 电击防护方式分类介绍
  • 目标检测中COCO评估指标中每个指标的具体含义说明:AP、AR
  • 基于YOLOv8的PCB缺陷检测--补充实验
  • [C++面试] 智能指针面试点(重点)续3
  • 【视觉提示学习】3.28阅读随想
  • 11. STL的使用
  • springBoot统一响应类型3.4版本
  • LlamaIndex实现(基于PDF|CSV文件)RAG检索增强生成:NaiveRAG
  • 量子计算:未来计算技术的革命性突破
  • 【奶茶经济学的符号暴力本质】
  • 【软件开发】可复用的数据库导入工具类
  • J2EE框架技术 第五章 Spring注入与作用域
  • 【C++】STL库_stack_queue 的模拟实现
  • 【leetcode】通过两种遍历方式构造二叉树
  • 前端页面缓存问题
  • opencv之指纹验证