安装
目标:在主流 Linux 发行版上装好 certbot 及所需插件,并注册账号。
1. 安装方式怎么选
| 方式 | 优点 | 缺点 | 适用 |
|---|---|---|---|
| snap(EFF 官方推荐) | 版本最新、自带自动更新、跨发行版一致 | 需要 snapd | Ubuntu、Debian 及支持 snap 的发行版 |
| apt(Debian/Ubuntu) | 简单、由发行版维护 | 版本可能较旧 | 求稳、不想装 snap |
| dnf/yum(RHEL 系,走 EPEL) | 原生 | 需先启用 EPEL | CentOS/Rocky/Alma/RHEL |
| pip | 版本新、可控 | 需自行管理依赖、无 systemd 集成 | 特殊环境、离线/源码 |
注意:历史工具
certbot-auto已废弃,不要再使用。
2. snap 安装(官方推荐)
bash
# 0. 确保 snapd 已安装(Ubuntu 一般自带)
sudo apt update && sudo apt install -y snapd # 如已存在可跳过
# 1. 安装 certbot(classic 模式以访问系统配置)
sudo snap install --classic certbot
# 2. 让命令可直接调用(snap 会把 certbot 放到 /snap/bin)
sudo ln -s /snap/bin/certbot /usr/bin/certbot
# 3. 安装插件(按需)
sudo snap set certbot trust-plugin-with-root=ok # 信任插件的 root 权限(DNS 插件常需要)
sudo snap install certbot-dns-cloudflare # 以 Cloudflare 为例snap 版自带 snap.certbot.renew.timer / snap.certbot.renew.service 的自动续期定时器。
3. Debian / Ubuntu(apt)
bash
sudo apt update
sudo apt install -y certbot python3-certbot-nginx # Nginx 场景
# 或 Apache 场景:
sudo apt install -y certbot python3-certbot-apacheapt 版会一并安装并启用 certbot.timer systemd 定时器,自动每天两次尝试续期。
4. RHEL / CentOS / Rocky / Alma(dnf,走 EPEL)
bash
# 启用 EPEL(Rocky/Alma 通常已预置 epel-release 仓库)
sudo dnf install -y epel-release
# 安装 certbot 与 Nginx 插件
sudo dnf install -y certbot python3-certbot-nginx
# 或 Apache 插件
sudo dnf install -y certbot python3-certbot-apacheCentOS 7(EOL)等旧系统用
yum语法同理,但建议升级到受支持的发行版。
5. Fedora
bash
sudo dnf install -y certbot python3-certbot-nginx # 或 python3-certbot-apache6. pip 安装(通用 / 离线)
bash
# 建议在虚拟环境或用户级安装,避免污染系统 Python
python3 -m pip install --user certbot certbot-nginx # certbot-apache、certbot-dns-cloudflare 同理
# 确保 ~/.local/bin 在 PATH 中pip 方式不会自动创建 systemd timer,续期需自己配 cron/systemd(见 续期与自动化)。
7. 验证安装
bash
certbot --version
# 示例输出:certbot 2.x.x查看当前已安装、可用的插件:
bash
certbot plugins
# 输出分两栏:可用插件(* 表示已启用)8. 注册账号(可选,通常自动完成)
首次签发时 Certbot 会自动注册并询问邮箱;也可以显式注册:
bash
sudo certbot register \
--email you@example.com \
--agree-tos \
--no-eff-email--agree-tos:同意服务条款(自动化脚本必加)。--no-eff-email:不接收 EFF 邮件(不强制)。-m/--email:用于证书到期/安全问题通知的邮箱,建议填写。- 账号信息存在
/etc/letsencrypt/accounts/,删除即等于换新账号(注意别误删)。
9. 常见坑
- 权限:certbot 操作
/etc/letsencrypt需要 root,命令一般加sudo。 - snap 的 PATH:如果
certbot找不到,检查/snap/bin是否在 PATH,或做上面的软链接。 - DNS 插件需要凭据文件:如
certbot-dns-cloudflare需要一个含 API token 的 ini 文件,权限设为 600。 - apt 版较旧:个别新特性(如某些 DNS 插件)可能缺失,必要时改用 snap 或 pip。
