1. 简介
Certbot 是一个非常流行的自动化工具,用于从 Let’s Encrypt 证书颁发机构申请和续订免费的 TLS/SSL 证书。它简化了在 Web 服务器上配置 HTTPS 的流程,从而保障了通信的安全性。此外,Certbot 也适用于 Spring Boot 应用、Postfix 邮件服务器、Nginx 反向代理、PostgreSQL 数据库等需要 TLS/SSL 支持的服务。
Certbot 插件扩展了 Certbot 的功能,主要分为两类:
- 认证插件(Authenticator):用于验证域名所有权
- 安装插件(Installer):用于配置服务器以使用获取的证书
本文将介绍如何安装 Certbot 插件以扩展其功能。
2. 建议与限制
本文主要介绍官方插件的安装方法,因为第三方插件可能有各自的安装说明,通常在其 GitHub 页面上。只有通过 snap 分发的第三方插件可以像官方插件一样安装。
安装 Certbot 及其插件时需注意以下限制:
✅ 必须直接在服务器上运行 Certbot(如通过 SSH 登录的 VPS)
❌ Certbot 自 2024 年 2 月起不再支持 Windows,macOS 支持也不完善,因此本文专注于 GNU/Linux 服务器
✅ 推荐使用 root 权限运行 Certbot,以便正确配置 TLS/SSL
❌ Certbot 无法在 Docker 容器中自动安装证书或配置 Web 服务器
✅ 官方推荐使用 snap 安装 Certbot 及其插件
⚠️ 一些第三方插件可能需要通过 pip 在 虚拟环境 中安装
✅ 也可以使用包管理器安装 Certbot 及部分插件,但这些版本容易过时
✅ 请确保使用同一种方式(snap、pip 或包管理器)安装 Certbot 及其插件,以避免兼容性问题
❌ 已废弃的 certbot-auto 脚本(常用于旧服务器)应被移除
使用 snap 安装 Certbot 的优势包括自动更新、自动配置证书续订、支持所有 DNS 插件等。但 ⚠️ 在使用 snap 安装 Certbot 插件前,应先卸载 certbot-auto 和系统自带的 Certbot 包。
对于共享主机,可参考 Certbot 关于主机提供商的文档。
3. 通过 snap 安装 Certbot 插件
snap 是一种 Linux 软件包管理系统,允许我们在隔离环境中安装和运行软件包(称为 snap),简化了安装过程,因为 snap 包含了所有依赖项,并几乎兼容所有 Linux 发行版。
默认情况下,snap 是相互隔离的,但 Certbot 需要使用 --classic
标志启用经典模式,以便访问系统资源和用户文件,类似于传统 Linux 包。
3.1. 安装 snap
snap 通常预装在 KDE Neon、Solus、Ubuntu 18.04 及以上版本、Manjaro 和 Zorin OS 等发行版中。这些系统无需额外配置即可直接使用 snap。
对于 Debian、Linux Mint、Arch Linux、Fedora、openSUSE 等其他发行版,则需根据 snap 安装指南 手动启用经典模式支持。
3.2. 安装 Certbot Snap
确保已安装 snap 并移除了系统自带的 Certbot 包后,可在任意 Linux 发行版中使用以下命令安装 Certbot:
$ sudo snap install --classic certbot
certbot 2.10.0 from Certbot Project (certbot-eff✓) installed
建议创建符号链接,方便后续使用:
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
查看当前已安装插件:
$ certbot plugins
[...]
* apache
[...]
* nginx
[...]
* standalone
[...]
* webroot
[...]
查看可用插件列表:
$ snap find certbot
Name Version Publisher Notes Summary
certbot 2.10.0 certbot-eff✓ classic Automatically configure HTTPS using Let's Encrypt
certbot-dns-cloudxns 1.32.2 certbot-eff✓ - CloudXNS DNS Authenticator plugin for Certbot
certbot-dns-cloudflare 2.10.0 certbot-eff✓ - Cloudflare DNS Authenticator plugin for Certbot
certbot-dns-dnsmadeeasy 2.10.0 certbot-eff✓ - DNS Made Easy DNS Authenticator plugin for Certbot
certbot-dns-dnsimple 2.10.0 certbot-eff✓ - DNSimple DNS Authenticator plugin for Certbot
[...]
设置插件权限:
$ sudo snap set certbot trust-plugin-with-root=ok
以安装 Cloudflare DNS 插件为例:
$ sudo snap install certbot-dns-cloudflare
certbot-dns-cloudflare 2.10.0 from Certbot Project (certbot-eff✓) installed
验证插件是否可用:
$ certbot plugins
[...]
* dns-cloudflare
Description: Obtain certificates using a DNS TXT record (if you are using
Cloudflare for DNS).
[...]
如需使用该插件,请参考官方文档:DNS 插件使用指南
4. 通过包管理器安装 Certbot 插件
尽管 snap 是推荐方式,但仍有技术与哲学原因倾向于使用发行版自带的包管理器。例如 Linux Mint 默认禁用 snap。
以 Linux Mint 21 为例,查看相关包:
$ apt-cache search --names-only certbot
[...]
python3-certbot - main library for certbot
python3-certbot-apache - Apache plugin for Certbot
python3-certbot-dns-cloudflare - Cloudflare DNS plugin for Certbot
[...]
安装所需插件即可,依赖会自动安装:
$ sudo apt update && sudo apt install python3-certbot-dns-cloudflare
[...]
The following NEW packages will be installed:
certbot python3-acme python3-certbot python3-certbot-dns-cloudflare
python3-cloudflare python3-configargparse python3-josepy
python3-openssl python3-parsedatetime python3-zope.component
python3-zope.event python3-zope.hookable python3-zope.interface
[...]
验证插件是否安装成功:
$ sudo certbot plugins
[...]
* dns-cloudflare
[...]
⚠️ 但 Linux Mint 21 上安装的 Certbot 版本为 1.21.0,明显落后于当前版本 2.10.0(发布于 2024 年 4 月 3 日)。这可能导致兼容性问题,建议考虑使用 snap 或 pip 安装。
Debian、Fedora、openSUSE 等主流发行版通常提供较新的 Certbot 包,如 openSUSE 已更新至 2.10.0,无需额外操作。
5. 通过 pip 安装 Certbot 插件
大多数第三方插件仅可通过 pip 安装。pip 是 Python 的包管理工具,适用于所有 Linux 发行版,只要 Python 版本一致即可。
5.1. 安装 Python 及依赖
Certbot 需要 Python 3 及相关依赖。在基于 apt 的发行版中安装:
$ sudo apt update
$ sudo apt install python3 python3-venv libaugeas0
在基于 rpm 的发行版中安装:
$ sudo dnf install python3 augeas-libs
其他发行版可能需要不同包名。
5.2. 在虚拟环境中安装 Certbot
创建虚拟环境:
$ sudo python3 -m venv /opt/certbot/
升级 pip:
$ sudo /opt/certbot/bin/pip install --upgrade pip
搜索可用插件:
$ sudo /opt/certbot/bin/python -m pypisearch -p 1-99 certbot
certbot (2.10.0) ACME client
certbot-dns-azure (2.5.0) Azure DNS Authenticator plugin for Certbot
certbot-dns-powerdns (0.2.1) PowerDNS DNS Authenticator plugin for Certbot
[...]
安装 Certbot 及 Cloudflare 插件:
$ sudo /opt/certbot/bin/pip install certbot certbot-dns-cloudflare
创建符号链接:
$ sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
验证插件:
$ certbot plugins
[...]
* dns-cloudflare
[...]
⚠️ pip 安装的主要缺点是:
- 不会自动更新 Certbot 及其依赖
- 不自动配置证书续订
5.3. 管理更新
手动配置证书自动续订任务(crontab):
$ echo "0 0,12 * * * root /opt/certbot/bin/python -c 'import random; import time; \
time.sleep(random.random() * 3600)' && sudo certbot renew -q" | \
sudo tee -a /etc/crontab > /dev/null
此方式引入了随机延迟,避免多个服务器在同一时间请求续订,从而分散证书服务器负载。
定期手动更新 Certbot 及插件:
$ sudo /opt/certbot/bin/pip install --upgrade certbot certbot-dns-cloudflare
不建议使用 crontab 自动更新,以便及时发现和处理更新中的问题。
6. 小结
本文介绍了如何通过三种方式安装 Certbot 插件:
✅ 推荐使用 snap:安装简单,支持自动更新与证书续订
✅ 包管理器:适用于特定发行版,版本可能较旧
✅ pip:适用于第三方插件或特定环境,但需手动维护更新
无论采用哪种方式,保持 Certbot 及其插件更新是确保安全的关键。选择合适的安装方式,有助于更高效地管理 TLS/SSL 证书。