Files
certbot-aliyun-docker/README.md
2025-08-02 15:52:49 +08:00

97 lines
3.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
## 使用阿里云 DNS 实现 Certbot 自动续签证书的 Docker 镜像
### 构建镜像
```shell
cd derper
git clone https://github.com/hotwa/certbot-dns-aliyun
cd certbot-dns-aliyun
docker build -t hotwa/certbot:latest .
```
### 手动续期
1. 续期证书
适用于你已经用 certbot/letsencrypt 申请过,想自动脚本化续期的场景。
```shell
docker run --rm \
-e REGION=YOUR_REGEION \
-e ACCESS_KEY_ID=YOUR_ACCESS_KEY \
-e ACCESS_KEY_SECRET=YOUR_ACCESS_SECRET \
-e DOMAIN=YOUR_DOMAIN \
-e EMAIL=YOUR_NOTIFICATION_EMAIL \
-e CRON_SCHEDULE="0 0 * * *" \
-v "$PWD/letsencrypt:/etc/letsencrypt" \
-v "$PWD/certs:/app/certs" \
-v "$PWD/logs:/var/log/certbot" \
hotwa/derper-certbot:latest \
certbot renew \
--manual \
--preferred-challenges dns \
--manual-auth-hook "alidns" \
--manual-cleanup-hook "alidns clean" \
--deploy-hook "/app/scripts/webhook.sh" \
--no-random-sleep-on-renew \
-v
```
2. 首次申请证书
适用于第一次为一个新域名/通配符域名申请 SSL 证书
```shell
docker run --rm \
-e REGION=YOUR_REGEION \
-e ACCESS_KEY_ID=YOUR_ACCESS_KEY \
-e ACCESS_KEY_SECRET=YOUR_ACCESS_SECRET \
-e DOMAIN=YOUR_DOMAIN \
-e EMAIL=YOUR_NOTIFICATION_EMAIL \ // 证书刷新通知邮箱
-e CRON_SCHEDULE="0 0 * * *" \ // 自定义证书刷新间隔
-v "$PWD/derper/letsencrypt:/etc/letsencrypt" \
-v "$PWD/derper/certs:/app/certs" \
-v "$PWD/derper/logs:/var/log/certbot" \
hotwa/derper-certbot:latest \
certbot certonly \
-d "*.headscale.jmsu.top" \
--manual \
--preferred-challenges dns \
--manual-auth-hook "alidns" \
--manual-cleanup-hook "alidns clean" \
--email your@email.com \
--agree-tos \
--non-interactive \
-v
```
## 定时续期
```shell
chmod +x renew_restart.sh
ln -s ${PWD}/renew_restart.sh /usr/local/bin/renew-restart.sh
crontab -e
```
```shell
0 3 * * * root /usr/local/bin/renew-cert.sh >> /var/log/certbot/renew.log 2>&1
```
## 自动管理证书
Tailscale 自带的derper在设计上确实有一个「自动获取与续期证书」的模式参数是-certmode letsencrypt对应环境变量 DERP_CERT_MODE=letsencrypt。这种模式会使用内置的 ACME 客户端向 Lets Encrypt 申请证书并在到期前自动续期。但它有两个重要前提:
**必须使用 ACME 支持的端口进行验证。**Lets Encrypt 的 HTTP01 挑战只能通过公网上的 80 端口完成TLSALPN01 挑战只能通过 443 端口完成
community.letsencrypt.org
。因此derper 必须监听 443默认并且允许 Lets Encrypt 的验证服务器访问 80 或 443才能自动完成申请和续期。
**域名需要直接解析到运行derper的服务器。**只有这样 Lets Encrypt 的验证请求才能到达derper。
当你把derper改为监听 3477 这类高端口时Lets Encrypt 无法再通过 80/443 进行验证。国内博文也指出,如果自建 DERP 服务器使用高位端口,就无法通过自动验证,只能手动申请证书
alampy.com
。所以在这种情况下derper 自身的自动证书功能会失效,需要自行管理证书并挂载进容器。
总结:
如果能让 derper 监听 443并开放 80/443 两个端口,你可以启用 -certmode letsencryptderper 会自动申请并定期续期证书,完全不需要手动管理。
如果不能开放 80/443例如用的是 3477 端口或服务器 behind NATLets Encrypt 的 HTTP/TLSALPN 挑战无法完成derper 就不能自动续期。你需要使用 DNS01 挑战通过 certbot 等工具申请证书,并在续期后重启 derper或手动申请更新证书然后挂载到容器