OpenResty + Certbot 实战(Webroot + Manual)
安装 Certbotwebroot方式OpenResty 配置路径certbot certonly --webroot -w /path -d 域名获取证书manual方式(不支持定时刷新)HTTP 验证(适合已有 Web 服务)DNS验证配置 OpenResty与设置定时任务。
一、 安装 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.conf 或 conf.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就表示续期流程没问题。
✅ 完整流程总结
- 安装 Certbot
- webroot方式
- OpenResty 配置
/.well-known/acme-challenge/路径 certbot certonly --webroot -w /path -d 域名获取证书
- OpenResty 配置
- manual方式(不支持定时刷新)
- HTTP 验证(适合已有 Web 服务)
- DNS验证
- 配置 OpenResty
ssl_certificate与ssl_certificate_key - 设置定时任务
certbot renew --post-hook "systemctl reload openresty"
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)