在 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,12 UTC 时间 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
Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐