Certbot 证书自动化教程 / 第 2 章:安装与环境准备
第 2 章:安装与环境准备
2.1 系统要求
在安装 Certbot 之前,确认服务器满足以下基本要求:
| 要求 | 说明 |
|---|---|
| 操作系统 | Linux(Ubuntu、Debian、CentOS、Fedora、Alpine 等) |
| Python 版本 | Python 3.8+(Snap 安装方式无此依赖) |
| 网络 | 80 端口和/或 443 端口可达(HTTP-01 验证需要) |
| 权限 | root 或 sudo 权限 |
| DNS | 域名已正确解析到服务器 IP |
环境检查
# 检查操作系统
cat /etc/os-release
# 检查 Python 版本
python3 --version
# 检查 80 端口是否可用
sudo ss -tlnp | grep :80
# 检查 443 端口是否可用
sudo ss -tlnp | grep :443
# 检查域名解析
dig +short example.com A
dig +short example.com AAAA
2.2 Snap 安装(推荐)
Snap 是 Certbot 官方推荐的安装方式,适用于大多数 Linux 发行版。Snap 包自动包含所有依赖,且支持自动更新。
安装 Snapd
# Ubuntu / Debian
sudo apt update
sudo apt install -y snapd
# CentOS / RHEL / Fedora
sudo dnf install -y epel-release
sudo dnf install -y snapd
sudo systemctl enable --now snapd.socket
# 创建符号链接
sudo ln -s /var/lib/snapd/snap /snap
通过 Snap 安装 Certbot
# 确保 snapd 是最新的
sudo snap install core; sudo snap refresh core
# 安装 certbot
sudo snap install --classic certbot
# 创建符号链接(使 certbot 命令可用)
sudo ln -sf /snap/bin/certbot /usr/bin/certbot
# 验证安装
certbot --version
# 输出示例: certbot 2.x.x
管理 Snap 版本
# 查看已安装的 snap 包
snap list certbot
# 手动更新 certbot
sudo snap refresh certbot
# 查看 certbot 的频道信息
sudo snap info certbot
# 切换到特定频道(如 beta)
sudo snap switch --channel=beta certbot
sudo snap refresh certbot
Snap 安装的文件位置
| 路径 | 说明 |
|---|---|
/snap/bin/certbot | Certbot 可执行文件 |
/etc/letsencrypt/ | 证书和配置存储目录 |
/var/log/letsencrypt/ | Certbot 日志目录 |
/var/lib/letsencrypt/ | 临时文件目录 |
注意: 使用 Snap 安装的 Certbot 时,
certbot命令需要 root 权限运行,因为证书存储在/etc/letsencrypt/下。
2.3 包管理器安装
部分 Linux 发行版的官方仓库中也提供了 Certbot,但版本可能滞后于 Snap 版本。
Debian / Ubuntu
# 更新包索引
sudo apt update
# 安装 certbot(基础版本)
sudo apt install -y certbot
# 安装 Nginx 插件
sudo apt install -y python3-certbot-nginx
# 安装 Apache 插件
sudo apt install -y python3-certbot-apache
# 验证安装
certbot --version
CentOS / RHEL / Fedora
# CentOS 8+ / RHEL 8+ / Fedora
sudo dnf install -y certbot
# 安装 Nginx 插件
sudo dnf install -y python3-certbot-nginx
# 安装 Apache 插件
sudo dnf install -y python3-certbot-apache
# CentOS 7(已停止维护,不推荐)
sudo yum install -y epel-release
sudo yum install -y certbot
Alpine Linux
# 更新包索引
apk update
# 安装 certbot
apk add certbot
# 安装插件
apk add certbot-nginx
apk add certbot-apache
Arch Linux
# 安装 certbot
sudo pacman -S certbot
# 安装插件
sudo pacman -S certbot-nginx
sudo pacman -S certbot-apache
各发行版安装方式对比
| 发行版 | 包管理器 | 命令 | 版本新鲜度 |
|---|---|---|---|
| Ubuntu | apt | apt install certbot | ⭐⭐⭐ |
| Debian | apt | apt install certbot | ⭐⭐ |
| Fedora | dnf | dnf install certbot | ⭐⭐⭐ |
| CentOS Stream | dnf | dnf install certbot | ⭐⭐ |
| Alpine | apk | apk add certbot | ⭐⭐ |
| Arch | pacman | pacman -S certbot | ⭐⭐⭐⭐ |
提示: 包管理器安装的版本通常落后于 Snap,如果需要最新功能,建议使用 Snap 方式。
2.4 pip 安装(Python 包管理器)
对于需要精确控制版本的场景,可以使用 pip 安装:
# 创建虚拟环境(推荐)
python3 -m venv /opt/certbot
source /opt/certbot/bin/activate
# 安装 certbot
pip install certbot
# 安装 Nginx 插件
pip install certbot-nginx
# 安装 Apache 插件
pip install certbot-apache
# 验证安装
certbot --version
# 退出虚拟环境
deactivate
通过 pip 安装的全局配置
# 创建符号链接以便全局使用
sudo ln -sf /opt/certbot/bin/certbot /usr/local/bin/certbot
# 设置自动更新脚本
cat > /opt/certbot/update.sh << 'EOF'
#!/bin/bash
source /opt/certbot/bin/activate
pip install --upgrade certbot certbot-nginx
deactivate
EOF
chmod +x /opt/certbot/update.sh
注意: pip 安装方式不支持自动更新,需要手动或通过脚本定期升级。
2.5 Docker 安装
Docker 方式适合容器化环境或不想污染主机系统的场景。
基本使用
# 拉取官方镜像
docker pull certbot/certbot
# 查看版本
docker run --rm certbot/certbot --version
# 申请证书(standalone 模式)
docker run --rm -it \
-p 80:80 \
-v /etc/letsencrypt:/etc/letsencrypt \
-v /var/log/letsencrypt:/var/log/letsencrypt \
certbot/certbot certonly --standalone \
-d example.com
# 申请证书(webroot 模式)
docker run --rm -it \
-v /etc/letsencrypt:/etc/letsencrypt \
-v /var/log/letsencrypt:/var/log/letsencrypt \
-v /var/www/certbot:/var/www/certbot \
certbot/certbot certonly --webroot \
-w /var/www/certbot \
-d example.com
Docker 常用参数
| 参数 | 说明 |
|---|---|
--rm | 容器退出后自动删除 |
-it | 交互模式,方便输入确认 |
-p 80:80 | 映射 80 端口(standalone 模式需要) |
-v /etc/letsencrypt:/etc/letsencrypt | 挂载证书存储目录 |
-v /var/log/letsencrypt:/var/log/letsencrypt | 挂载日志目录 |
查看证书
docker run --rm \
-v /etc/letsencrypt:/etc/letsencrypt \
certbot/certbot certificates
Docker 镜像标签
| 标签 | 说明 |
|---|---|
latest | 最新稳定版 |
v2.x.x | 特定版本 |
arm64v8-latest | ARM64 架构 |
2.6 插件安装
Certbot 通过插件支持不同的验证方式和 Web 服务器集成。
插件类型
| 插件类型 | 功能 | 示例 |
|---|---|---|
| Authenticator | 完成域名验证 | standalone, webroot, dns-cloudflare |
| Installer | 配置 Web 服务器 | nginx, apache |
| 兼具两者 | 验证+配置 | nginx, apache |
Web 服务器插件
# Nginx 插件
# Snap 安装方式(已内置)
sudo snap install --classic certbot # 包含 nginx 插件
# apt 安装方式
sudo apt install -y python3-certbot-nginx
# Apache 插件
# apt 安装方式
sudo apt install -y python3-certbot-apache
DNS 插件
# Cloudflare
sudo snap install certbot-dns-cloudflare
# 或
pip install certbot-dns-cloudflare
# Route53 (AWS)
pip install certbot-dns-route53
# Google Cloud DNS
pip install certbot-dns-google
# DigitalOcean
sudo snap install certbot-dns-digitalocean
# 或
pip install certbot-dns-digitalocean
# RFC2136 (通用 DNS 更新)
pip install certbot-dns-rfc2136
查看已安装的插件
# 列出所有可用插件
certbot plugins
# 列出已安装的认证插件
certbot plugins --authenticators
# 列出已安装的安装插件
certbot plugins --installers
DNS 插件安装汇总表
| DNS 提供商 | 插件包名 | Snap 安装 | pip 安装 |
|---|---|---|---|
| Cloudflare | certbot-dns-cloudflare | ✅ | ✅ |
| AWS Route53 | certbot-dns-route53 | ❌ | ✅ |
| Google Cloud | certbot-dns-google | ❌ | ✅ |
| DigitalOcean | certbot-dns-digitalocean | ✅ | ✅ |
| CloudXNS | certbot-dns-cloudxns | ❌ | ✅ |
| DNSimple | certbot-dns-dnsimple | ❌ | ✅ |
| DNS Made Easy | certbot-dns-dnsmadeeasy | ❌ | ✅ |
| Gandi | certbot-dns-gandi | ❌ | ✅ |
| GoDaddy | certbot-dns-godaddy | ❌ | ✅ |
| Hetzner | certbot-dns-hetzner | ❌ | ✅ |
| Linode | certbot-dns-linode | ❌ | ✅ |
| LuaDNS | certbot-dns-luadns | ❌ | ✅ |
| NS1 | certbot-dns-nsone | ❌ | ✅ |
| OVH | certbot-dns-ovh | ❌ | ✅ |
| RFC 2136 | certbot-dns-rfc2136 | ❌ | ✅ |
| Sakura Cloud | certbot-dns-sakuracloud | ❌ | ✅ |
2.7 安装后配置
创建必要目录
# Webroot 验证所需的目录
sudo mkdir -p /var/www/certbot
# 设置正确的权限
sudo chown -R www-data:www-data /var/www/certbot
配置防火墙
# UFW (Ubuntu)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
# firewalld (CentOS / Fedora)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
# iptables
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables-save | sudo tee /etc/iptables/rules.v4
全局配置文件
Certbot 的全局配置文件位于 /etc/letsencrypt/cli.ini:
# /etc/letsencrypt/cli.ini
# 默认邮箱(用于接收证书到期通知)
email = [email protected]
# 同意服务条款
agree-tos = true
# 使用非交互模式
non-interactive = true
# 使用 Webroot 验证方式
authenticator = webroot
webroot-path = /var/www/certbot
# 日志级别
max-log-backups = 10
测试安装
# 查看版本
certbot --version
# 查看帮助
certbot --help
# 查看所有可用子命令
certbot --help all
# 使用 staging 环境测试
sudo certbot certonly --standalone --staging \
-d test.example.com \
--agree-tos \
--email [email protected] \
--non-interactive
2.8 卸载 Certbot
Snap 方式卸载
sudo snap remove certbot
apt 方式卸载
sudo apt remove --purge certbot python3-certbot-nginx python3-certbot-apache
sudo apt autoremove
清理证书数据
# 备份后删除
sudo cp -r /etc/letsencrypt /etc/letsencrypt.backup
sudo rm -rf /etc/letsencrypt
sudo rm -rf /var/log/letsencrypt
sudo rm -rf /var/lib/letsencrypt
警告: 删除
/etc/letsencrypt会导致所有已签发的证书丢失。在卸载前请确保已不再需要这些证书,或已做好备份。
2.9 常见安装问题
问题一:Snap 安装后找不到 certbot 命令
# 原因:snap bin 目录不在 PATH 中
# 解决方案 1:创建符号链接
sudo ln -sf /snap/bin/certbot /usr/bin/certbot
# 解决方案 2:添加 PATH
echo 'export PATH=/snap/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
问题二:pip 安装时依赖冲突
# 原因:系统 Python 包版本冲突
# 解决方案:使用虚拟环境
python3 -m venv /opt/certbot-venv
source /opt/certbot-venv/bin/activate
pip install certbot certbot-nginx
问题三:DNS 插件安装后不被识别
# 原因:Snap 安装的 certbot 无法访问 pip 安装的插件
# 解决方案 1:通过 snap 安装插件
sudo snap install certbot-dns-cloudflare
# 解决方案 2:使用 pip 安装 certbot 本体
pip install certbot certbot-dns-cloudflare
注意事项
- 安装方式一致性: 建议统一使用 Snap 安装 Certbot 及其插件,避免混用导致的路径和依赖冲突
- root 权限: Certbot 需要 root 权限来修改系统文件和绑定 80 端口
- 定期更新: 保持 Certbot 为最新版本,以获取安全修复和新功能
- 防火墙: 确保 80 端口开放(HTTP-01 验证必须)