Certbot临时申请SSL证书
Certbot 临时申请 SSL 证书
申请命令(未成功)
docker run -it --rm --name certbot \
-v "/data/certbot:/etc/letsencrypt" \
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
-p 8087:80 \
certbot/certbot certonly --standalone -d zhaopeng.site命令说明
| 参数 | 说明 |
|---|---|
-it | 交互式运行,便于查看输出 |
--rm | 容器退出后自动删除 |
--name certbot | 指定容器名称 |
-v "/data/certbot:/etc/letsencrypt" | 挂载证书目录,持久化证书 |
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" | 挂载工作目录 |
-p 8087:80 | 将宿主机 8087 端口映射到容器的 80 端口 |
certbot/certbot | 使用的 Certbot 镜像 |
certonly | 仅获取证书,不安装 |
--standalone | 使用内置 Web 服务器验证域名 |
-d zhaopeng.site | 指定要申请证书的域名 |
可能失败的原因及排查
1. 端口问题
--standalone模式需要占用容器的 80 端口- 确保宿主机 8087 端口未被占用
- 检查防火墙是否允许 8087 端口访问
2. 域名解析
- 确保域名
zhaopeng.site已正确解析到服务器 IP 验证命令:
nslookup zhaopeng.site # 或 dig zhaopeng.site
3. 网络访问
- 确保 Let's Encrypt API 可访问
- 检查服务器是否能够访问外网
4. 证书目录权限
- 确保挂载目录
/data/certbot有正确的读写权限
替代方案
使用 webroot 模式(推荐)
如果已有 Web 服务器运行:
docker run -it --rm --name certbot \
-v "/data/certbot:/etc/letsencrypt" \
-v "/var/www/html:/var/www/html" \
certbot/certbot certonly --webroot -w /var/www/html -d zhaopeng.site使用 DNS 验证模式
避免端口占用问题:
# 以 Cloudflare 为例
docker run -it --rm --name certbot \
-v "/data/certbot:/etc/letsencrypt" \
-e CLOUDFLARE_EMAIL=your-email@example.com \
-e CLOUDFLARE_API_KEY=your-api-key \
certbot/dns-cloudflare certonly --dns-cloudflare -d zhaopeng.site查看日志排查错误
# 查看 Certbot 日志
cat /data/certbot/logs/letsencrypt.log验证证书是否成功申请
# 查看证书文件
ls -la /data/certbot/live/zhaopeng.site/
# 应包含以下文件:
# - cert.pem # 证书
# - chain.pem # 证书链
# - fullchain.pem # 完整证书链
# - privkey.pem # 私钥Certbot 与 SSL 证书配置笔记
1. Webroot 路径
Nginx HTML 目录
/data/nginx/html2. Certbot 证书申请命令
尝试不同的 webroot 路径
# 使用 /var/www/html 作为 webroot
sudo certbot certonly --webroot -w /var/www/html -d xui.755855.xyz
# 使用 /data/nginx/html 作为 webroot
sudo certbot certonly --webroot -w /data/nginx/html -d xui.755855.xyz
# 使用 /data/cert 作为 webroot
sudo certbot certonly --webroot -w /data/cert -d xui.755855.xyz注册邮箱
zpcarry@gmail.com3. Nginx SSL 证书配置
证书路径
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/privkey.pem;4. 相关工具
scrcpy(Android 投屏工具)
5. 操作说明
Certbot 工作流程
- webroot 方式:Certbot 会在指定的 webroot 目录下创建临时验证文件
- 验证域名:Let's Encrypt 通过 HTTP 访问
http://xui.755855.xyz/.well-known/acme-challenge/验证域名所有权 - 证书生成:验证通过后,证书保存在
/etc/letsencrypt/live/xui.755855.xyz/ - 配置 Nginx:将证书链接到 Nginx 配置中使用的路径
注意事项
- 确保 webroot 目录可以被 Certbot 写入
- 确保域名解析正确指向服务器
- 80 端口需要开放供 Let's Encrypt 验证