
Certbot 使用教程
Certbot 是一个免费、开源的软件工具,由电子前沿基金会 (EFF) 开发,旨在简化获取、安装和自动续订 Let’s Encrypt 等证书颁发机构 (CA) 提供的 SSL/TLS 证书的过程。通过使用 Certbot,您可以轻松地为您的网站启用 HTTPS 加密,提升网站的安全性和可信度。
目录
1. 准备工作
在开始使用 Certbot 之前,请确保满足以下条件:
- 域名所有权:您必须拥有一个指向您服务器公网 IP 地址的域名(例如
example.com
)。 - 服务器访问权限:拥有对运行 Web 服务器的 Linux 服务器的 root 或 sudo 权限。
- Web 服务器运行:您的 Web 服务器(如 Apache 或 Nginx)必须正在运行,并且可以通过 HTTP (端口 80) 访问。Certbot 需要通过 HTTP 验证您对域名的控制权。
- 开放端口:确保服务器的 80 (HTTP) 和 443 (HTTPS) 端口在防火墙和安全组中是开放的。
2. 安装 Certbot
Certbot 的安装方法取决于您的操作系统和 Web 服务器软件。以下是几种常见方式:
通用方法:Snap 包管理器 (推荐)
Snap 包适用于大多数 Linux 发行版,能确保您获得最新版本的 Certbot。
# 确保 snapd 已安装并更新
sudo snap install core
sudo snap refresh core
# 安装 certbot
sudo snap install --classic certbot
# 通过符号链接使 certbot 命令可用
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Ubuntu/Debian (使用 APT)
# 更新包列表
sudo apt update
# 安装 certbot 和相应的 Web 服务器插件
# 对于 Apache
sudo apt install certbot python3-certbot-apache
# 对于 Nginx
sudo apt install certbot python3-certbot-nginx
CentOS/RHEL/Rocky Linux/AlmaLinux (使用 DNF/YUM)
# 启用 EPEL 仓库 (如果尚未启用)
sudo dnf install epel-release
# 安装 certbot 和相应的 Web 服务器插件
# 对于 Apache
sudo dnf install certbot python3-certbot-apache
# 对于 Nginx
sudo dnf install certbot python3-certbot-nginx
其他发行版
请参考 Certbot 官方网站的 安装指南 获取特定于您操作系统的安装说明。
3. 获取并安装证书
Certbot 提供了多种插件来与不同的 Web 服务器交互。最常用的是 --apache
和 --nginx
插件,它们可以自动配置您的 Web 服务器以使用 HTTPS。
方法一:使用 Web 服务器插件 (推荐)
此方法会自动获取证书并修改您的 Web 服务器配置。
对于 Apache:
sudo certbot --apache -d example.com -d www.example.com
对于 Nginx:
sudo certbot --nginx -d example.com -d www.example.com
说明:
--apache
或--nginx
: 指定使用的插件。-d example.com
: 指定要为其获取证书的域名。可以使用多个-d
参数添加多个域名(主域名和子域名)。
运行命令后,Certbot 会引导您完成以下步骤:
- 输入您的电子邮件地址(用于紧急通知和恢复)。
- 同意 Let’s Encrypt 的服务条款。
- 选择是否共享您的电子邮件地址(可选)。
- Certbot 会自动完成域名验证(通常通过 HTTP-01 挑战),获取证书,并修改您的 Apache 或 Nginx 配置文件以启用 HTTPS。
- 询问您是否要将 HTTP 流量重定向到 HTTPS(强烈建议选择 Yes)。
方法二:独立模式 (Standalone)
当您的 Web 服务器暂时无法运行,或者您想在没有 Web 服务器的机器上获取证书时使用。此模式会启动一个临时的 Web 服务器来完成验证。
sudo certbot certonly --standalone -d example.com -d www.example.com
说明:
certonly
: 表示只获取证书,不安装(需要您手动配置 Web 服务器)。--standalone
: 使用 Certbot 自带的临时 Web 服务器进行验证。
注意:使用此模式时,您需要在运行命令前停止占用 80 端口的 Web 服务器(sudo systemctl stop apache2
或 sudo systemctl stop nginx
),验证完成后 Certbot 会自动停止其临时服务器,您需要再手动启动您的 Web 服务器。
方法三:Webroot 模式
如果您希望 Certbot 将验证文件放在您网站的特定目录下,而不中断当前 Web 服务器的运行。
sudo certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com
说明:
--webroot
: 指定使用 webroot 插件。-w /var/www/example
: 指定您网站根目录的路径,Certbot 会将验证文件放在.well-known/acme-challenge/
子目录下。
您需要确保您的 Web 服务器配置允许访问 .well-known/acme-challenge/
路径下的文件。
4. 验证证书
证书获取成功后,您可以进行验证:
-
检查证书文件: Certbot 默认将证书存储在
/etc/letsencrypt/live/<your_domain>/
目录下。包含fullchain.pem
(证书链) 和privkey.pem
(私钥) 文件。 -
访问网站: 在浏览器中访问
https://your_domain
,检查是否出现安全锁标志,并确认证书信息正确。 -
使用 OpenSSL 检查:
openssl x509 -in /etc/letsencrypt/live/example.com/fullchain.pem -text -noout
5. 自动续订
Let’s Encrypt 证书有效期为 90 天。Certbot 会自动设置一个系统定时任务(cron job 或 systemd timer)来定期检查并续订即将过期的证书。
-
检查自动续订状态:
sudo certbot renew --dry-run
此命令模拟续订过程,不实际更改任何证书,用于测试自动续订是否配置正确。
-
手动触发续订:
sudo certbot renew
此命令会检查所有已注册的证书,如果任何证书在 30 天内到期,则进行续订。通常不需要手动运行,由系统自动执行。
重要:确保您的 Web 服务器配置(特别是使用 --apache
或 --nginx
安装的)在续订后能正确加载新证书。大多数情况下,Certbot 会处理好。
6. 常见问题与故障排除
-
“Failed to connect to host for DVSNI challenge” 或 “Connection refused”:
- 确保域名正确解析到服务器 IP。
- 确保 80 端口开放且 Web 服务器正在运行(使用插件或 webroot 模式时)。
- 检查防火墙设置。
-
“Client with the currently selected authenticator does not support any combination of challenges”:
- 可能是插件问题。尝试使用
--standalone
模式,或确保插件正确安装。
- 可能是插件问题。尝试使用
-
“Too Many Requests”:
- Let’s Encrypt 对请求频率有限制。避免在短时间内重复请求相同域名。可以使用
--dry-run
进行测试。
- Let’s Encrypt 对请求频率有限制。避免在短时间内重复请求相同域名。可以使用
-
证书续订失败:
- 运行
sudo certbot renew --dry-run
检查问题。 - 检查磁盘空间、网络连接和 Web 服务器状态。
- 查看 Certbot 日志 (
/var/log/letsencrypt/letsencrypt.log
)。
- 运行
7. 高级用法
- 指定密钥长度:
--rsa-key-size 4096
- 设置证书有效期:
--cert-name example.com
(用于管理) - 撤销证书:
sudo certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem
- 删除证书:
sudo certbot delete --cert-name example.com
8. 参考资料
- Certbot 官方网站: https://certbot.eff.org
- Let’s Encrypt 文档: https://letsencrypt.org/docs/
- Certbot 命令行参考: https://certbot.eff.org/docs/using.html
通过遵循本教程,您应该能够成功地为您的网站获取、安装和自动续订免费的 SSL/TLS 证书,让您的网站更加安全可靠。