Certbot Standalone 申请SSL证书:3分钟拿证,80端口被占也有解法
2026/8/24 8:44:42 网站建设 项目流程

Certbot Standalone 申请SSL证书:3分钟拿证,80端口被占也有解法

【免费下载链接】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

机器上 80 端口已被别的程序占着,或者根本就没装 Web 服务器,现在又必须尽快拿到一张 Let's Encrypt 证书——这是 Certbot Standalone 模式的典型场景。它自己临时起一个 HTTP 服务完成 http-01 域名验证,一条命令几分钟内完成签发,验证结束自动释放端口。

什么时候该用 Standalone,什么时候别用

Standalone 是 Certbot 内置的认证器之一,不依赖任何现有 Web 服务:它在自己进程内开一个临时 TCP 监听,在/.well-known/acme-challenge/路径下响应验证请求,挑战完成后即关闭(实现见certbot/src/certbot/_internal/plugins/standalone.py)。

它适合三类场景:

  • 机器上没装 Nginx/Apache 的临时服务器、Docker 宿主机、CI 构建机
  • 有 Web 服务,但能在几分钟内停掉的机器
  • 端口冲突 申请证书 时不想改 Web 服务器配置

它不适合:通配符域名(仅支持 http-01,官方插件描述明确写了 wildcards not supported),以及 80 端口长期被占、既停不掉服务、又无法做端口转发的机器——后者应改用 dns-01。

认证方式机制挑战类型典型场景
standalone自建临时 HTTP 服务http-01 (80)无Web服务器、端口冲突 申请证书
webroot往现有站点目录写文件http-01 (80)Web 服务持续运行、目录可写
dns-01改 DNS TXT 记录dns-01通配符、80 完全不可达

前提条件:运行 certbot 需要 root 权限(默认监听 80 低于 1024);80 端口必须允许互联网入站访问,云服务器要同时配好系统防火墙和安全组。

最小可跑流程:一条命令拿证

执行申请

certbot certonly --standalone -d example.com \ --email admin@example.com \ --agree-tos \ --non-interactive # certonly:只申请证书,不动 Web 服务器配置 # --standalone:由 Certbot 临时监听 80 端口完成验证 # 不指定密钥类型时默认 ECDSA / secp256r1

参数说明:

  • -d:域名,可重复多个
  • --email:注册与到期通知邮箱,首次申请必填
  • --agree-tos:同意 Let's Encrypt 服务条款
  • --non-interactive:跳过交互提示,适合脚本

验证成功标志:输出末尾出现Successfully received certificate.,且提示证书已存入/etc/letsencrypt/live/example.com/

确认证书文件

certbot certificates

会打印该证书的 Domains、Expiry Date(Let's Encrypt 有效期 90 天)和证书目录。/etc/letsencrypt/live/example.com/下可见cert.pem(服务器证书)、fullchain.pem(证书+中间 CA,Web 服务器通常配这个)和privkey.pem(私钥)。

80 端口被占了怎么办

绑定 80 端口失败时,源码里的处理逻辑分两种:端口被占(EADDRINUSE)会提示Could not bind TCP port 80 because it is already in use by another process on this system;权限不足(EACCES)会提示you don't have the appropriate permissions,解法就是换 root 执行。

被占场景下有三种选择,按推荐顺序:

1. 用钩子临时停掉占用进程。这是端口冲突 申请证书 最稳的做法,钩子逻辑同样适用于自动续期:

certbot certonly --standalone -d example.com \ --pre-hook "systemctl stop nginx" \ --post-hook "systemctl start nginx" # pre-hook:验证前停掉占用 80 的服务 # post-hook:验证后自动拉起

验证方式:命令跑完后systemctl is-active nginx应输出active

2. 换监听端口--http-01-port 8080让 Certbot 监听 8080。

注意:换端口不等于改协议,Let's Encrypt 仍从 80 抓取验证文件,必须在防火墙上把外部 80 的流量转发到本机 8080,否则验证必然失败。

3. 指定网卡绑定:多网卡机器用--http-01-address 192.168.1.100(默认空值即绑定所有接口,见certbot/src/certbot/_internal/constants.py)。另外--http-01-port 0会随机挑一个空闲端口,常配合--debug-challenges做本地调试——它会停在挑战提交前等你确认,加-v还能打印完整的挑战 URL,方便手动 curl 验证。

到期前它自己做了什么

Let's Encrypt 证书 90 天过期,靠定时任务驱动续期:

certbot renew

由 cron 或 systemd timer 周期性执行即可,它只做幂等检查:只对剩余有效期不足 30 天的证书重新签发,其余跳过。注意:certbot renew不会沿用你手动申请时的--standalone,而是使用每张证书申请时记录的 authenticator 参数;如果当时是 standalone 方式签的,续期时同样会临时占用 80 端口,需要时加--pre-hook/--post-hook

certbot renew --pre-hook "systemctl stop nginx" \ --post-hook "systemctl start nginx"

首次接入生产前建议先打一张测试证书确认流程:certbot certonly --standalone --test-cert -d test.example.com--test-cert走 staging 环境(等价于--server指向 staging),签出的证书浏览器不信任,但不会占用正式环境的速率配额。验证方式:staging 证书同样落盘到/etc/letsencrypt/live/,可用certbot certificates看到 Expiry Date。

验证失败的排查路径

场景一:端口被占。报错原文:Could not bind TCP port 80 because it is already in use by another process on this system (such as a web server). Please stop the program in question and then try again.定位三步:sudo lsof -i :80找出占用进程 → 确认无其他程序在抢端口 →systemctl stop <服务>后重跑。解决:重跑同一条 certbot 命令,或改用上一节的 pre-hook 方案。

场景二:CA 抓不到验证文件。报错原文:The Certificate Authority failed to download the challenge files from the temporary standalone webserver started by Certbot on port 80. Ensure that the listed domains point to this machine and that it can accept inbound connections from the internet.这是证书流程失败,不是端口问题——Certbot 本地监听成功了,但外部连不进来。定位三步:dig example.com +short确认 A 记录指向本机公网 IP;检查安全组/防火墙入站是否放行 TCP 80;加--debug-challenges -v重跑,复制打印出的挑战 URL 在本机外网环境 curl 一遍。解决:修 DNS 或放行端口后重跑。

场景三:权限。报错原文含you don't have the appropriate permissions (for example, you aren't running this program as root),用 root 或 sudo 重跑即可。

所有申请过程的详细日志在/var/log/letsencrypt/letsencrypt.log,上面三步的输出都在里面。

边界与自查入口

Standalone 适合"无 Web 服务器"或"可短暂停服务"的 http-01 单域名申请,90 天有效期加certbot renew可做到免运维续期;如果你的域名需要通配符,或 80 端口完全无法腾出,应转向 dns-01 认证(Certbot 提供了一组 DNS 插件,如 certbot-dns-route53、certbot-dns-cloudflare)。自查入口:certbot --help standalone查看当前版本的参数,认证方式对比见certbot/docs/using.rst,配置文件的写法示例见certbot/examples/cli.ini

【免费下载链接】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),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询