美文网首页
CentOS 7.9 配置免费 SSL 证书并自动续期

CentOS 7.9 配置免费 SSL 证书并自动续期

作者: 葵花点穴 | 来源:发表于2025-12-09 23:24 被阅读0次

在 CentOS 7.9 上使用 Python 虚拟环境安装 Certbot + Let's Encrypt 为 OpenResty 配置免费 SSL 证书(完整指南)

适用环境:CentOS 7.9 / Rocky Linux 7 + OpenResty
目标域名ipvv.net(请替换为你自己的域名)
Certbot 安装方式:Python 虚拟环境(路径 /data/certbot
验证方式:Webroot(推荐,无需停止服务)
自动续期:Cron + --post-hook 安全重载 OpenResty


一、前提条件

  1. 公网域名已正确解析到服务器 IP(如 ipvv.net1.2.3.4
  2. 防火墙/安全组已放行 80(HTTP)和 443(HTTPS)端口
  3. OpenResty 已安装并正常运行
```
1/usr/local/openresty/nginx/sbin/nginx -v
2# 示例输出:nginx version: openresty/1.25.x
```

</pre>
  1. 系统已安装 python3(CentOS 7 默认可能未安装,需手动启用或安装)

二、安装 Certbot(使用 Python 虚拟环境)

⚠️ 关键背景
CentOS 7 默认 OpenSSL 版本为 1.0.2k-fips,而 urllib3 >= 2.0 不兼容该版本,会导致:

ImportError: urllib3 v2 only supports OpenSSL 1.1.1+ ...

因此必须使用 虚拟环境 + 强制降级 urllib3

步骤 1:安装系统依赖

1sudo yum install -y gcc openssl-devel libffi-devel python3 python3-devel augeas-libs

✅ 这些是编译 cryptographypyOpenSSL 等 Python 包所必需的。

步骤 2:创建虚拟环境(路径 /data/certbot

1# 创建目录
2sudo mkdir -p /data/certbot
3
4# 创建 Python 3 虚拟环境
5sudo python3 -m venv /data/certbot

步骤 3:在虚拟环境中安装兼容版 Certbot

1# 安装兼容的 urllib3(必须 < 2.0)
2/data/certbot/bin/pip3 install "urllib3<2"
3
4# 安装 certbot 核心组件
5/data/certbot/bin/pip3 install certbot
6
7# 可选:安装 nginx 插件(即使使用 OpenResty,某些工具函数可能被间接依赖)
8/data/certbot/bin/pip3 install certbot-nginx

步骤 4:验证安装

1# 检查 urllib3 版本(应为 1.x,如 1.26.15)
2/data/certbot/bin/python -c "import urllib3; print(urllib3.__version__)"
3
4# 检查 certbot 是否可运行
5/data/certbot/bin/certbot --version

✅ 输出示例:

1certbot 2.10.0

📝 后续所有 certbot 命令必须使用完整路径
/data/certbot/bin/certbot


三、申请 SSL 证书(使用 Webroot 模式)

💡 Webroot 模式优势

  • 无需停止 OpenResty
  • 通过在网站根目录下放置验证文件完成域名所有权验证
  • 最适合生产环境

步骤 1:确认网站根目录(webroot)

假设你的 OpenResty 配置如下(/usr/local/openresty/nginx/conf/conf.d/ipvv.conf):

1server {
2    listen 80;
3    server_name ipvv.net;
4    root /data/www;  # ← 这就是 webroot 路径
5    index index.html;
6    ...
7}

→ 网站根目录为:/data/www

🔍 如果不确定,请检查你的 OpenResty 配置中的 root 指令。

步骤 2:确保 .well-known/acme-challenge/ 可公开访问

通常 OpenResty 默认允许访问该路径。若需显式配置,添加以下 location:

1server {
2    listen 80;
3    server_name ipvv.net;
4    root /data/www;
5
6    # ACME 验证路径(Let's Encrypt 必需)
7    location ^~ /.well-known/acme-challenge/ {
8        default_type "text/plain";
9    }
10
11    # 其他业务配置...
12}

重载 OpenResty 使配置生效:

1sudo /usr/local/openresty/nginx/sbin/nginx -s reload

步骤 3:申请证书

1sudo /data/certbot/bin/certbot certonly \
2    --webroot \
3    -w /data/www \
4    -d ipvv.net

✅ 成功后输出:

1Congratulations! Your certificate and chain have been saved at:
2/etc/letsencrypt/live/ipvv.net/fullchain.pem

📁 证书文件说明:

  • fullchain.pem:证书链(Nginx/OpenResty 的 ssl_certificate
  • privkey.pem:私钥(ssl_certificate_key

四、配置 OpenResty 启用 HTTPS

编辑站点配置文件(如 /usr/local/openresty/nginx/conf/conf.d/ipvv.conf):

1# HTTPS 服务(443 端口)
2server {
3    listen 443 ssl;
4    server_name ipvv.net;
5
6    # SSL 证书路径(由 Certbot 自动管理)
7    ssl_certificate     /etc/letsencrypt/live/ipvv.net/fullchain.pem;
8    ssl_certificate_key /etc/letsencrypt/live/ipvv.net/privkey.pem;
9
10    # 安全协议与加密套件
11    ssl_protocols       TLSv1.2 TLSv1.3;
12    ssl_ciphers         ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20:!aNULL:!MD5:!DSS;
13    ssl_prefer_server_ciphers off;
14
15    # 网站根目录
16    root /data/www;
17    index index.html;
18
19    location / {
20        # 你的业务逻辑(如 proxy_pass、Lua 脚本等)
21    }
22}
23
24# HTTP 强制跳转 HTTPS(可选但推荐)
25server {
26    listen 80;
27    server_name ipvv.net;
28    return 301 https://$host$request_uri;
29}

重载 OpenResty:

1sudo /usr/local/openresty/nginx/sbin/nginx -s reload

访问 https://ipvv.net,应显示安全锁标志 ✅


五、配置自动续期(关键!)

Let's Encrypt 证书 90 天过期,必须自动续期。

步骤 1:测试自动续期

1# 模拟续期(不会真正更新证书)
2sudo /data/certbot/bin/certbot renew --dry-run

✅ 成功输出:

1Congratulations, all simulated renewals succeeded.

❌ 如果失败,请检查:

  • /etc/letsencrypt/renewal/ipvv.net.conf 中是否为 authenticator = webroot
  • OpenResty 是否允许访问 http://ipvv.net/.well-known/acme-challenge/

步骤 2:设置定时任务(crontab)

1# 编辑 root 的 crontab
2sudo crontab -e

添加以下行(每天凌晨 2:30 检查):

130 2 * * * /data/certbot/bin/certbot renew --quiet --post-hook "/usr/local/openresty/nginx/sbin/nginx -s reload"

🔑 参数说明:

  • --quiet:仅出错时输出日志
  • --post-hook "..."仅当证书实际被更新后才执行重载命令(避免无谓重启)

六、OpenResty 用户特别注意事项

问题 解决方案
Certbot 找不到 nginx 命令 创建软链接:
sudo ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/bin/nginx
续期时 80 端口被占用 使用 Webroot 模式,无需停服务(本指南已采用)
自动重载失败 --post-hook显式指定 OpenResty 路径
证书路径变化 不会变!/etc/letsencrypt/live/ipvv.net/ 下的文件是软链接,自动指向最新版本
Python 3.8 警告 仅警告,不影响功能;未来迁移到新系统再升级

七、常见命令速查

功能 命令
查看证书到期时间 sudo openssl x509 -enddate -noout -in /etc/letsencrypt/live/ipvv.net/cert.pem
手动续期(生产) sudo /data/certbot/bin/certbot renew
测试续期(安全) sudo /data/certbot/bin/certbot renew --dry-run
重载 OpenResty sudo /usr/local/openresty/nginx/sbin/nginx -s reload
查看 Certbot 日志 tail -f /var/log/letsencrypt/letsencrypt.log
查看 urllib3 版本 /data/certbot/bin/python -c "import urllib3; print(urllib3.__version__)"

八、附录:续期配置文件说明

Certbot 自动生成的续期配置位于:

1/etc/letsencrypt/renewal/ipvv.net.conf

关键内容应包含:

1[renewalparams]
2authenticator = webroot
3webroot_path = /data/www,

✅ 确保没有 authenticator = standalone,否则续期会因 80 端口占用失败。


📌 最后建议:每季度检查一次 /var/log/letsencrypt/letsencrypt.log,确保续期健康。

相关文章

网友评论

      本文标题:CentOS 7.9 配置免费 SSL 证书并自动续期

      本文链接:https://www.haomeiwen.com/subject/ulslcstx.html