ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Certbot Standalone 五分钟申请免费 SSL 证书完整教程

Certbot Standalone 五分钟申请免费 SSL 证书完整教程

Certbot Standalone 五分钟申请免费 SSL 证书完整教程

【免费下载链接】certbotCertbot is EFF's tool to obtain certs from Let's Encrypt and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol.项目地址: https://gitcode.com/gh_mirrors/ce/certbot

临时测试机刚上线,没装 Nginx,业务方却要求今晚就开 HTTPS。不用先装 Web 服务器,用 Certbot Standalone 就能申请到免费 SSL 证书:它自己起一个临时 HTTP 服务完成域名验证,拿到证书后立刻退出。从一条命令到证书落地,五分钟能跑完。

先判断:Standalone 是不是你的环境该用的模式

结论先说:80 端口能空出来,且没有必须常驻的 Web 服务,就选它。

  • 80 端口当前空闲,或能临时停掉占用服务:适用
  • 机器没装 Web 服务器,装一个也不划算:适用
  • 已有 Web 服务器在跑,且能写它的网站根目录:换 webroot,不中断服务
  • 需要通配符证书,或 80 端口彻底开不了:换 dns-01,走 DNS 记录验证
  • 服务必须 7×24 不中断,连停几秒都不行:不适用

⚠️ Standalone 验证期间独占 80 端口,期间有服务占用会直接验证失败。规避动作:申请前先停服务,或用 --pre-hook 自动停。

webroot 和 dns-01 的差别一句话带过:webroot 往现有 Web 服务器的根目录写验证文件,dns-01 改 DNS 记录,两者都不需要独占 80 端口。

五分钟流程:一条命令到证书落地

Standalone 申请证书的一条命令

最简命令只有一行,跑通就是成功:

certbot certonly --standalone -d example.com --email admin@example.com --agree-tos

它让 Certbot Standalone 插件临时占用 80 端口完成验证,证书落到 /etc/letsencrypt/live/example.com/,不碰任何 Web 服务器配置。

参数逐行拆解:

  • --standalone:启用独立 Web 服务器模式
  • certonly:只申请证书,不修改任何服务器配置
  • -d:指定域名,可重复写多个,如-d example.com -d www.example.com
  • --email:接收到期通知,必填
  • --agree-tos:自动同意 Let's Encrypt 服务条款

80 被占用时的非标准端口

--http-01-port只改本地监听端口,不改外部验证入口:

certbot certonly --standalone -d example.com --http-01-port 8080

执行后 Certbot 改听 8080,外部流量仍要从 80 进来再转到 8080。

⚠️ ACME 验证服务器仍会连接服务器的 80 端口,不做 80 → 8080 转发必然验证失败。规避动作:先配好转发规则,再跑命令。

证书产物路径

申请成功后,文件默认在/etc/letsencrypt/live/example.com/:

  • fullchain.pem:服务器证书 + 中间 CA
  • privkey.pem:私钥,权限要收紧
  • cert.pem:服务器证书单文件

密钥算法方面,2.0+ 默认 ECDSA(secp256r1 曲线)。示例配置 certbot/examples/cli.ini 里用的是key-type = ecdsa加elliptic-curve = secp384r1;要兼容旧设备就写rsa-key-size = 4096。

Standalone 申请失败怎么排查

三个现场都出在 80 端口上,逐个拆。

80 端口被占用怎么解决

报错原文:Problem binding to port 80: Could not bind to IPv4 or IPv6.

原因:端口已被别的服务占走。

修复:sudo lsof -i :80找出进程,systemctl stop nginx停掉它再申请;成功后systemctl start nginx恢复服务。

防火墙没放行 80 端口

报错表现:申请过程没有绑定错误,挑战验证超时几分钟后失败。

原因:防火墙或云安全组没开 80,验证流量进不来。

修复:入站规则加一条——协议 TCP、端口 80、源地址 0.0.0.0/0。Let's Encrypt 验证 IP 不固定,别只对单一来源放行。

多网卡绑定指定 IP

表现:机器有多块网卡,Certbot 默认绑定错地址,挑战请求落到错误的 IP 上。

修复:用--http-01-address指定绑定地址,即certbot certonly --standalone --http-01-address 192.168.1.100 -d example.com。

证书拿下来之后:续期、备份、排障入口

Let's Encrypt 证书有效期 90 天,续期交给 cron;80 被占用时用 hook 停启 nginx:

0 0 1 * * certbot renew --quiet --standalone --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

这是一条 cron 任务:每月 1 日 0 点检查 Let's Encrypt 自动续期,续期前停 nginx,完成后自动拉起。

  • 首次运行:加--test-cert测试,别在生产触发 Let's Encrypt 速率限制
  • 备份:定期备份整个 /etc/letsencrypt/ 目录,私钥和配置都在里面
  • 排障入口:看 /var/log/letsencrypt/letsencrypt.log,申请失败的原因全在这里

收尾

Standalone 适合临时服务、端口冲突的机器和 CI/CD 流水线里的证书自动化。完整参数查 certbot/docs/using.rst,命令行直接跑certbot --help standalone看当前版本支持哪些选项。

【免费下载链接】certbotCertbot is EFF's tool to obtain certs from Let's Encrypt and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol.项目地址: https://gitcode.com/gh_mirrors/ce/certbot

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

返回列表