Certbot使用教程:一键HTTPS的懒人方案

a
admin
作者
2026-07-17
发布日期
约 12 分钟
阅读时间

Certbot是EFF(电子前哨基金会)开发的ACME客户端,是Let’s Encrypt官方推荐的工具。

和acme.sh相比,Certbot更”重”但更”智能”——它能自动修改Nginx/Apache配置,真正实现一键HTTPS。

Certbot vs acme.sh

两个工具怎么选?看对比:

Certbot的优势

  • 自动修改Web服务器配置,真正一键
  • 官方推荐,文档最全
  • 插件生态丰富
  • 适合Python环境

acme.sh的优势

  • 无依赖,纯Shell
  • DNS API支持更广
  • 体积更小
  • 适合嵌入式和小服务器

选择建议

  • 单服务器、想一键自动化 → Certbot
  • 多服务器、要精细控制 → acme.sh
  • 宝塔面板用户 → 用面板内置功能即可

安装Certbot

Ubuntu/Debian

apt update
apt install certbot python3-certbot-nginx

CentOS/RHEL

yum install epel-release
yum install certbot python3-certbot-nginx

Docker

docker run -it --rm \
    -v /etc/letsencrypt:/etc/letsencrypt \
    -v /var/www/html:/var/www/html \
    certbot/certbot certonly

Snap(推荐,官方方式)

snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

一键HTTPS(Certbot最强功能)

Certbot的杀手锏是能自动改Nginx配置:

certbot --nginx -d example.com -d www.example.com

这一条命令做了什么

  1. 申请SSL证书
  2. 修改Nginx配置,添加SSL相关指令
  3. 配置HTTP 301跳转到HTTPS
  4. 自动重载Nginx
  5. 配置自动续签cron

全程不需要手动改任何配置文件,执行完命令网站就是HTTPS了。

Apache同理:

certbot --apache -d example.com -d www.example.com

只申请证书(不改配置)

如果不想让Certbot改配置,只申请证书:

certbot certonly --nginx -d example.com

证书位置:

  • 证书:/etc/letsencrypt/live/example.com/fullchain.pem
  • 私钥:/etc/letsencrypt/live/example.com/privkey.pem

然后手动配置Nginx:

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

Webroot模式

适合不想停Web服务的场景:

certbot certonly --webroot \
    -w /var/www/html \
    -d example.com \
    -d www.example.com

Certbot会在 /var/www/html/.well-known/acme-challenge/ 放验证文件,CA访问该文件完成验证。

DNS验证

通配符证书必须用DNS验证:

certbot certonly --manual \
    --preferred-challenges dns \
    -d example.com -d *.example.com

问题:手动DNS验证每次续签都要手动加TXT记录,不实用。

解决方案:用DNS插件实现自动化。

阿里云DNS插件

certbot certonly \
    --authenticator dns-aliyun \
    --dns-aliyun-credentials /etc/letsencrypt/aliyun.ini \
    -d example.com -d *.example.com

/etc/letsencrypt/aliyun.ini 内容:

dns_aliyun_access_key = LTAI4xxxxx
dns_aliyun_access_key_secret = xxxxxxx

注意:dns-aliyun插件不在官方包里,需要单独安装:

pip install certbot-dns-aliyun

Cloudflare插件(官方支持)

apt install python3-certbot-dns-cloudflare

certbot certonly \
    --dns-cloudflare \
    --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
    -d example.com -d *.example.com

/etc/letsencrypt/cloudflare.ini

dns_cloudflare_api_token = xxxxxxx

自动续签

Certbot安装时自动配置了systemd timer或cron,定期检查并续签。

查看timer状态

systemctl status certbot.timer

手动测试续签

certbot renew --dry-run

强制续签

certbot renew --force-renewal

续签后重载服务

certbot renew --deploy-reload-hook "systemctl reload nginx"

高级配置

申请ECC证书

默认申请RSA证书,可以指定ECC:

certbot certonly --nginx \
    -d example.com \
    --key-type ecdsa \
    --elliptic-curve secp256r1

同时申请RSA和ECC

certbot certonly --nginx \
    -d example.com \
    --key-type rsa \
    --key-type ecdsa

使用ZeroSSL

默认用Let’s Encrypt,可以切换:

certbot certonly --nginx \
    -d example.com \
    --server https://acme.zerossl.com/v2/DV90 \
    -m your@email.com

撤销证书

certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem

删除证书

certbot delete --cert-name example.com

常见问题

问题一:Nginx插件报错

报错Error while running nginx -t

原因:Nginx配置本身有错误,Certbot无法解析。

解决:先用 nginx -t 检查配置,修复错误后再跑Certbot。

问题二:Webroot路径错误

报错Connection refused404

原因:webroot路径不对,或Nginx没正确配置 .well-known 路径。

解决

  • 确认webroot是网站根目录
  • Nginx配置确保 .well-known 可访问

问题三:DNS插件未安装

报错The requested dns-aliyun plugin does not appear to be installed

解决:用pip安装对应插件。

问题四:续签失败

原因:配置变更、服务停止、端口冲突。

解决

  • certbot renew --dry-run 看具体错误
  • 检查Web服务器是否正常运行
  • 检查80端口是否开放(standalone模式需要)

问题五:证书路径变了

Certbot默认用域名作为目录名,如果证书包含多个域名,目录名是第一个域名。

查看所有证书

certbot certificates

最佳实践

1. 优先用Nginx/Apache插件

能自动改配置、自动重载,最省心。

2. 通配符用DNS插件

手动DNS验证不可持续,必须用DNS插件实现自动化。

3. 定期测试续签

certbot renew --dry-run

每月跑一次,确保续签能成功。

4. 监控证书有效期

用Nagios、Zabbix或Prometheus监控证书到期时间,提前告警。

5. 备份/etc/letsencrypt

整个letsencrypt目录包含所有证书和配置,定期备份。

结语

Certbot是SSL自动化的”懒人方案”——一条命令完成申请、部署、续签配置。如果你不想折腾配置文件,Certbot是最好的选择。但要注意它的DNS插件生态不如acme.sh丰富,通配符证书场景可能需要额外配置。

本文到此结束