在 Ubuntu 中使用 Certbot 的操作指南
在 Ubuntu 系统中使用 Certbot 申请和管理 Let’s Encrypt 免费 SSL 证书,主要分为安装、申请、验证和配置自动续期几个步骤。下面详细介绍在 Ubuntu 环境下(以 Nginx 为例)的完整操作流程。
一、安装 Certbot
方法一:使用 apt 安装(适用于 Ubuntu 20.04 及更早版本)
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
如果需要使用 Apache,则安装 python3-certbot-apache 插件。
方法二:使用 Snap 安装(Ubuntu 22.04+ 推荐)
在较新的 Ubuntu LTS 版本(22.04、24.04、26.04)中,Certbot 官方推荐使用 Snap 方式安装:
# 安装 snapd
sudo apt update
sudo apt install snapd -y
# 刷新 core
sudo snap install core
sudo snap refresh core
# 移除可能冲突的 apt 版本
sudo apt remove certbot python3-certbot-nginx 2>/dev/null || true
# 安装 Certbot
sudo snap install --classic certbot
# 创建软链接使 certbot 命令可用
sudo ln -sf /snap/bin/certbot /usr/bin/certbot
# 验证安装
certbot --version
二、申请 SSL 证书
2.1 前置准备
在申请证书之前,需要确保以下几点:
- 域名解析正确:域名已解析到服务器的公网 IP(可通过
ping your-domain.com验证) - Web 服务器正常运行:Nginx 已安装并运行(
systemctl status nginx) - 防火墙开放端口:确保 80 端口(HTTP 验证)和 443 端口(HTTPS 服务)开放
sudo ufw allow 'Nginx Full' - Nginx 配置正确:Nginx 的
server_name需要指定对应的域名,如果配置为_会导致命令失败
2.2 自动配置方式(推荐)
如果使用 Nginx 且希望 Certbot 自动修改配置文件:
sudo certbot --nginx -d example.com -d www.example.com
对于 Apache 用户:
sudo certbot --apache -d example.com -d www.example.com
执行后,Certbot 会交互式地提示输入邮箱、同意服务条款,并询问是否将 HTTP 请求重定向到 HTTPS。证书申请成功后,Certbot 会自动修改 Web 服务器配置文件并重载服务。
2.3 仅获取证书方式
如果希望手动管理配置文件,可以使用 certonly 模式,仅获取证书而不自动安装:
sudo certbot certonly --nginx -d example.com -d www.example.com
2.4 申请泛域名证书(通配符证书)
泛域名证书(如 *.example.com)需要采用 DNS 验证方式,因为 HTTP 验证无法覆盖泛域名:
sudo certbot certonly --manual --preferred-challenges dns -d "*.example.com" -d "example.com"
执行后,Certbot 会提示在 DNS 服务商处添加 TXT 记录,例如:
Please deploy a DNS TXT record under the name:
_acme-challenge.example.com
with the following value:
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
添加完成后等待 DNS 生效(通常需要几分钟),按回车继续验证。如果同时为主域名和泛域名申请,可能需要添加两条 TXT 记录到同一个域名下。
注意:使用 --manual 方式申请的证书不会自动续期,需要配合认证钩子脚本(--manual-auth-hook)或改用 DNS 插件来实现自动化续期。
三、验证证书安装
3.1 查看证书信息
sudo certbot certificates
输出示例:
Found the following certs:
Certificate Name: example.com
Domains: example.com www.example.com
Expiry Date: 2026-12-31 23:59:59 (VALID: 90 days)
Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem
3.2 测试 Nginx 配置
sudo nginx -t
如果输出 syntax is ok 和 test is successful,表示配置正确。
3.3 访问网站验证
在浏览器中访问 https://your-domain.com,地址栏应显示安全锁标识。
四、配置自动续期
Let’s Encrypt 证书有效期为 90 天,需要通过自动续期来确保证书持续有效。
4.1 测试续期流程
sudo certbot renew --dry-run
如果输出 Congratulations, all renewals succeeded,说明续期配置正常。
4.2 启用 systemd 定时器
在 Ubuntu 系统中,Certbot 安装后通常会自带 systemd 定时器:
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
查看定时器状态:
sudo systemctl list-timers --all | grep certbot
4.3 手动添加 Cron 任务(备选)
如果需要手动控制续期时间,可以添加 crontab 任务:
sudo crontab -e
添加以下内容(每天凌晨 3 点检查续期):
0 3 * * * certbot renew --quiet --post-hook "systemctl reload nginx"
参数说明:
| 参数 | 说明 |
|---|---|
--quiet | 静默模式,不输出日志 |
--post-hook | 续期成功后执行的命令,用于重载 Web 服务 |
五、证书文件存储位置
成功申请后,证书文件保存在 /etc/letsencrypt/live/域名/ 目录下:
| 文件 | 说明 |
|---|---|
fullchain.pem | 服务器证书和中间证书链的完整文件 |
privkey.pem | 证书对应的私钥文件(请妥善保管) |
六、常见问题及排查
6.1 端口冲突
如果使用 --standalone 模式时提示端口被占用,可以先停止 Web 服务,或在命令中添加 --pre-hook 和 --post-hook 自动管理服务状态。
6.2 Nginx server_name 配置错误
若遇到 Could not automatically find a matching server block 错误,需要检查 Nginx 配置文件中 server_name 是否正确指定了域名,不能使用 _。
6.3 续期失败
排查步骤:
- 检查系统时间是否同步:
sudo timedatectl set-ntp true - 查看 Certbot 日志:
sudo tail -100 /var/log/letsencrypt/letsencrypt.log - 手动运行续期命令查看详细错误
6.4 关于测试环境与速率限制
在正式申请证书前,建议先使用 Let’s Encrypt 的测试环境(Staging Environment)进行调试,避免频繁测试消耗正式环境的申请限额(每个域名每周 50 张证书)。