一、 安装 Certbot

推荐用 Snap 安装(保持版本最新):

snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

#也可通过pip安装
pip install certbot

检查是否安装成功:

certbot --version

二、使用 Certbot 申请证书

1. Webroot 方式(自动化推荐 ✅)

1.1 配置 OpenResty 支持验证目录

在 OpenResty 配置文件(如 /usr/local/openresty/nginx/conf/nginx.conf/etc/nginx/conf.d/default.conf)中,为域名添加 临时 http server 块

server {
    listen 80;
    server_name example.com www.example.com;

    # 用于 Let’s Encrypt 验证
    location /.well-known/acme-challenge/ {
        root /usr/local/openresty/nginx/html;
    }

    # 其他请求可重定向到 https(可选,申请前建议先不用重定向)
    location / {
        return 404;
    }
}

创建目录(如果不存在)

mkdir -p /usr/local/openresty/nginx/html/.well-known/acme-challenge/

重启 OpenResty 生效:

systemctl reload openresty

1.2 申请证书

certbot certonly --webroot \
  -w /usr/local/openresty/nginx/html \
  -d example.com -d www.example.com

成功后证书路径:

/etc/letsencrypt/live/example.com/
├── fullchain.pem   # 证书链
└── privkey.pem     # 私钥

2. Manual 方式(备用手动模式 ✋)

Manual 模式适合:

  • 临时测试
  • 没有固定 Webroot 的情况
  • 需要申请 泛域名证书*.example.com

⚠️ 注意:Manual 方式 不能自动续期,证书到期必须手动重新申请。

方式 1:HTTP 验证(适合已有 Web 服务)

1. 执行命令:

certbot certonly --manual --preferred-challenges http -d example.com -d www.example.com

2. Certbot会提示创建一个文件,例如

请在 http://example.com/.well-known/acme-challenge/xxxx 文件中写入以下内容:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

3. 手动创建文件:

mkdir -p /usr/local/openresty/nginx/html/.well-known/acme-challenge/echo"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" > /usr/local/openresty/nginx/html/.well-known/acme-challenge/xxxx

4. 确认公网可访问:

curl http://example.com/.well-known/acme-challenge/xxxx

5. 回到 Certbot 界面按回车继续,成功后证书会生成在:

​​​​​​​/etc/letsencrypt/live/example.com/

方式 2:DNS 验证(支持泛域名)

 1. 执行命令:

​​​​​​​certbot certonly --manual --preferred-challenges dns \
  -d example.com -d "*.example.com"

2. Certbot 会提示你在 DNS 添加 TXT 记录,例如:

​​​​​​​_acme-challenge.example.com   TXT   "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

3. 登录域名服务商后台,添加 TXT 解析。 用 dig 验证是否生效:

​​​​​​​dig TXT _acme-challenge.example.com

4. 确认生效后,回到 Certbot 界面按回车继续。 证书同样会生成在:

​​​​​​​/etc/letsencrypt/live/example.com/

三、 配置 OpenResty 使用证书

1. 修改 https 配置(nginx.confconf.d/ssl.conf):

​​​​​​​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_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /usr/local/openresty/nginx/html;
    index index.html index.htm;
}

2. 重启 OpenResty:

​​​​​​​systemctl reload openresty

此时访问 https://example.com 即可。

四、 自动续期

Let’s Encrypt 证书有效期 90 天,推荐每月自动续期一次。

编辑定时任务:

​​​​​​​crontab -e

加入:

0 3 * * * certbot renew --quiet --post-hook "systemctl reload openresty"

意思是:每天凌晨 3 点检查证书是否需要续期,续期成功后自动重载 OpenResty。

五、 验证续期是否正常

手动执行一次:

​​​​​​​certbot renew --dry-run

如果显示 Congratulations就表示续期流程没问题。

✅ 完整流程总结

  1. 安装 Certbot
  2. webroot方式
    1. OpenResty 配置 /.well-known/acme-challenge/ 路径
    2. certbot certonly --webroot -w /path -d 域名 获取证书
  3. manual方式(不支持定时刷新)
    1. HTTP 验证(适合已有 Web 服务)
    2. DNS验证
  4. 配置 OpenResty ssl_certificatessl_certificate_key
  5. 设置定时任务 certbot renew --post-hook "systemctl reload openresty"

Logo

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

更多推荐