主题
Certbot
1. 简介
Certbot 是一个用于自动化获取和安装 Let's Encrypt SSL 证书的工具。它提供了一系列的命令来处理证书的整个生命周期,包括申请、安装、更新和撤销。
2. 安装
2.1 Ubuntu/Debian 安装
bash
# 更新包索引
sudo apt-get update
# 安装 Certbot 和 Nginx 插件
sudo apt-get install certbot python3-certbot-nginx2.2 CentOS/RHEL 安装
bash
# 安装 EPEL 仓库
sudo yum install epel-release
# 安装 Certbot 和 Nginx 插件
sudo yum install certbot python3-certbot-nginx3. 基本配置
Certbot 安装完成后无需额外配置即可使用。可以通过以下命令验证安装:
bash
# 查看 Certbot 版本
certbot --version4. 常用命令
4.1 证书申请
bash
# 基本证书申请
certbot certonly
# 使用 standalone 模式申请证书
certbot certonly --standalone
# 使用 Nginx 插件申请证书(自动配置)
certbot --nginx
# 使用 Apache 插件申请证书(自动配置)
certbot --apache
# 使用 webroot 模式申请证书
certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com4.2 证书安装
bash
# 安装证书并自动配置 Nginx
certbot --nginx
# 安装证书并自动配置 Apache
certbot --apache
# 手动安装证书
# 证书文件默认位于 /etc/letsencrypt/live/example.com/4.3 证书更新
bash
# 更新所有即将到期的证书
certbot renew
# 强制更新所有证书
certbot renew --force-renewal
# 测试证书更新(不实际更新)
certbot renew --dry-run
# 安静模式更新证书
certbot renew --quiet --no-self-upgrade4.4 证书管理
bash
# 列出所有证书
certbot certificates
# 查看证书详细信息
certbot certificates --cert-name example.com
# 删除证书
certbot delete --cert-name example.com
# 撤销证书
certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem4.5 自动更新配置
bash
# 添加自动更新的 crontab 任务
# 每天凌晨 2 点自动更新证书
(echo "0 2 * * * /usr/bin/certbot renew --quiet"; crontab -l) | crontab -
# 查看 crontab 任务
crontab -l5. 最佳实践
- 使用自动续期:设置自动续期任务,确保证书不会过期(Let's Encrypt 证书有效期为 90 天)
- 选择合适的插件:根据 Web 服务器选择对应的插件(如
--nginx或--apache) - 备份证书:定期备份证书文件,默认位于
/etc/letsencrypt/live/ - 使用多个域名:使用
-d参数为多个域名(包括 www 子域名)申请同一张证书 - 监控证书状态:定期检查证书状态,确保自动更新正常工作
- 保持软件更新:定期更新 Certbot 到最新版本,获取新功能和安全修复