Files
common-server-public/certbot/README.md
awei 58d65188a1 feat: 添加 Let's Encrypt SSL 证书管理
- 新增独立 certbot 服务项目
- nginx 配置添加 HTTPS 8443 端口监听
- frpc.toml HTTPS 代理指向 nginx:8443
- 支持自动续签并重载 nginx
- 共享 Docker volume 传递证书
2026-09-08 11:43:34 +00:00

118 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Let's Encrypt 证书管理服务
独立的 certbot 服务,用于管理 Let's Encrypt SSL证书的申请和自动续签。
## 架构说明
```
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 用户请求 │───▶│ frps (远程) │───▶│ frpc (本地) │
│ *.stonelan.cn │ │ 80/443端口 │ │ 80/8443端口 │
└─────────────────┘ └─────────────────┘ └─────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ nginx-proxy 容器 │
│ - 80端口: 处理 ACME 验证请求,转发到 certbot 容器 │
│ - 8443端口: HTTPS 反向代理,使用 Let's Encrypt 证书 │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ certbot 容器 │
│ - 处理 ACME HTTP-01 验证 │
│ - 定期续签证书 │
│ - 续签后自动重载 nginx (通过 docker.sock) │
└─────────────────────────────────────────────────────────────────┘
```
## 文件说明
- `Dockerfile` - certbot 镜像定义
- `docker-compose.yml` - certbot 容器配置
- `init-cert.sh` - 初始化申请证书脚本
- `renew-cert.sh` - 自动续签脚本
- `crontab.example` - cron 配置示例
## 使用方法
### 1. 首次申请证书
```bash
cd /home/awei/workspace/common-server-public/certbot
# 为每个域名申请证书
./init-cert.sh fastgpt.stonelan.cn admin@example.com
./init-cert.sh gitea.stonelan.cn admin@example.com
./init-cert.sh image.stonelan.cn admin@example.com
./init-cert.sh registry.stonelan.cn admin@example.com
./init-cert.sh www.stonelan.cn admin@example.com
```
### 2. 配置自动续签
```bash
# 编辑 crontab
crontab -e
# 添加以下行(每天凌晨 2 点检查续签)
0 2 * * * cd /home/awei/workspace/common-server-public/certbot && ./renew-cert.sh >> /var/log/certbot-renew.log 2>&1
```
### 3. 手动续签测试
```bash
cd /home/awei/workspace/common-server-public/certbot
./renew-cert.sh
```
## 证书路径
证书存储在 Docker volume `certbot-certs`nginx 容器通过共享 volume 访问:
- 证书: `/etc/letsencrypt/live/<域名>/fullchain.pem`
- 私钥: `/etc/letsencrypt/live/<域名>/privkey.pem`
## 端口映射
| 服务 | 容器端口 | 宿主机端口 | 说明 |
|------|----------|------------|------|
| nginx | 80 | 8081 | HTTP (ACME验证 + 重定向) |
| nginx | 8443 | 8443 | HTTPS (SSL反向代理) |
| frpc | 80 | 80 (远程) | HTTP 映射 |
| frpc | 8443 | 443 (远程) | HTTPS 映射 |
## 重启 nginx
续签后会自动通过 `docker kill -s HUP nginx-proxy` 重载 nginx。如需手动重启
```bash
docker kill -s HUP nginx-proxy
# 或
docker restart nginx-proxy
```
## 故障排查
### 查看证书状态
```bash
docker compose exec certbot certbot certificates
```
### 查看续签日志
```bash
tail -f /var/log/certbot-renew.log
```
### 检查 ACME 验证是否可达
```bash
curl http://fastgpt.stonelan.cn/.well-known/acme-challenge/test
```
## 注意事项
1. 确保域名 DNS 已正确解析到 frps 服务器 IP
2. 确保 frpc 和 nginx 容器在同一个 Docker 网络 (`trim-default`)
3. 证书有效期 90 天,建议每 60 天续签一次cron 已配置每天检查)
4. 首次申请证书前,确保 nginx 容器已启动并能处理 ACME 验证请求