免费HTTPS证书 Let’s Encrypt的安装及自动续期

OpenResty + Certbot 免费证书安装与续期(193839.com)

本文记录在 RHEL/CentOS/Alma 8 系统上,使用 OpenResty + Certbot 申请并续期 Let’s Encrypt 免费 HTTPS 证书的完整流程,便于后续复用。

一、环境信息

  • 系统:RHEL/CentOS/Alma 8
  • Web 服务:OpenResty
  • 域名:193839.com
  • 站点根目录:/usr/local/openresty/nginx/html
  • OpenResty nginx 可执行路径:/usr/local/openresty/nginx/sbin/nginx

二、准备条件

  1. 域名 193839.com 已解析到服务器公网 IP。
  2. 服务器 80/443 端口对外开放。
  3. OpenResty 已正常运行。

三、安装 Certbot

sudo dnf install -y epel-release
sudo dnf install -y certbot

四、配置 ACME 验证路径(HTTP)

在 HTTP server 块中加入以下配置,用于 Let’s Encrypt 验证:

location /.well-known/acme-challenge/ {
    root /usr/local/openresty/nginx/html;
}

验证并重载:

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

五、申请证书(Webroot 模式)

sudo certbot certonly --webroot \
  -w /usr/local/openresty/nginx/html \
  -d 193839.com

生成证书路径:

  • /etc/letsencrypt/live/193839.com/fullchain.pem
  • /etc/letsencrypt/live/193839.com/privkey.pem

六、配置 HTTPS(443)

新增或更新 HTTPS server

server {
    listen 443 ssl;
    server_name 193839.com;

    ssl_certificate /etc/letsencrypt/live/193839.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/193839.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }

    location /v1 {
        proxy_pass http://127.0.0.1:8281;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }

    location /ws/ {
        proxy_pass http://127.0.0.1:8281;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

验证并重载:

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

七、自动续期配置(Cron)

Let’s Encrypt 证书有效期 90 天,建议设置每日自动续期:

sudo crontab -e

加入一行:

0 3 * * * /usr/bin/certbot renew --quiet --deploy-hook "/usr/local/openresty/nginx/sbin/nginx -s reload"

八、续期验证

模拟续期:

sudo certbot renew --dry-run

查看证书列表:

sudo certbot certificates

九、常见问题

  • Mixed Content 报错:HTTPS 页面请求 HTTP 接口会被浏览器拦截,确保 API 走 HTTPS。
  • 验证失败:检查 DNS 解析是否生效、80 端口是否开放、ACME 路径是否正确。
  • 续期不生效:确认 cron 已配置并可执行 certbot renew
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容