Skip to content

安装

目标:在主流 Linux 发行版上装好 certbot 及所需插件,并注册账号。

1. 安装方式怎么选

方式优点缺点适用
snap(EFF 官方推荐)版本最新、自带自动更新、跨发行版一致需要 snapdUbuntu、Debian 及支持 snap 的发行版
apt(Debian/Ubuntu)简单、由发行版维护版本可能较旧求稳、不想装 snap
dnf/yum(RHEL 系,走 EPEL)原生需先启用 EPELCentOS/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-apache

apt 版会一并安装并启用 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-apache

CentOS 7(EOL)等旧系统用 yum 语法同理,但建议升级到受支持的发行版。

5. Fedora

bash
sudo dnf install -y certbot python3-certbot-nginx   # 或 python3-certbot-apache

6. 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。