Let's Encrypt 证书设置
Let’s Encrypt 证书设置
本节介绍如何使用 Let’s Encrypt 获取免费 TLS 证书。Let’s Encrypt 是目前最流行的免费证书颁发机构(CA),广泛用于 IoT 和 Web 服务。学习完成后,您将能够:
- 安装和配置 Certbot 工具
- 使用 Certbot 获取免费 TLS 证书
- 理解证书自动续期机制
- 评估 Let’s Encrypt 对 IoT 方案的适用性
在开始本节之前,请确保:
- 有一个可公开访问的域名(或 DynDNS 域名)
- 域名已解析到您的服务器 IP 地址
- 服务器端口 80(HTTP)可访问
- 拥有服务器的 root/sudo 权限
什么是 Let’s Encrypt?
Section titled “什么是 Let’s Encrypt?”Let’s Encrypt 是一个非营利性的证书颁发机构(CA),由 Internet Security Research Group (ISRG) 运营。
关键特性:
| 特性 | 说明 |
|---|---|
| 免费 | 无需支付任何费用 |
| 自动化 | 支持 Certbot 自动申请和续期 |
| 可信 | 所有主流操作系统和浏览器信任 |
| 有效期 | 90 天(鼓励自动化) |
| 支持类型 | 域名验证证书(DV) |
| 速率限制 | 每周最多 50 个证书 |
1. 安装 Certbot │2. 运行 Certbot ──→ 向 Let's Encrypt 发起申请 │3. 域名验证 ──→ Let's Encrypt 挑战域名所有权 (HTTP-01 challenge: 在服务器上放置指定文件) │4. 签发证书 ──→ Let's Encrypt 验证通过后签发证书 │5. 配置服务 ──→ 将证书配置到 Mosquitto / Nginx │6. 自动续期 ──→ Certbot 定时任务自动更新Install Certbot
Section titled “Install Certbot”# 检查操作系统版本cat /etc/os-release
# Ubuntu/Debian# 更新软件包索引sudo apt update
# 安装 snapd(snap 包管理器)sudo apt install snapd
# 更新 snapd 到最新版本sudo snap install core; sudo snap refresh core
# 安装 Certbotsudo snap install --classic certbot
# 创建符号链接(方便使用)sudo ln -s /snap/bin/certbot /usr/bin/certbot
# 验证安装certbot --version# 输出: certbot 2.x.xDocker 安装(可选)
Section titled “Docker 安装(可选)”# 使用 Docker 运行 Certbotdocker run -it --rm \ -v /etc/letsencrypt:/etc/letsencrypt \ -v /var/lib/letsencrypt:/var/lib/letsencrypt \ -p 80:80 \ certbot/certbot certonly --standaloneAcquire Certificate
Section titled “Acquire Certificate”# 使用 standalone 模式申请证书sudo certbot certonly --standalone \ --preferred-challenges http \ -d mqtt.example.com \ --email admin@example.com \ --agree-tos \ --non-interactive参数说明:
| 参数 | 说明 |
|---|---|
certonly | 仅获取证书,不自动配置 Web 服务器 |
--standalone | 使用内置 Web 服务器进行验证 |
--preferred-challenges http | 使用 HTTP 验证域名所有权 |
-d | 指定域名(可多个) |
--email | 用于证书过期提醒 |
--agree-tos | 同意 Let’s Encrypt 服务条款 |
--non-interactive | 非交互式运行(适合脚本) |
Certbot 执行以下步骤:
1. Let's Encrypt CA 生成一个验证令牌(token)2. Certbot 在服务器的 /.well-known/acme-challenge/ 目录下放置令牌3. Let's Encrypt 通过 HTTP 访问 http://mqtt.example.com/.well-known/acme-challenge/TOKEN4. 如果访问成功且内容匹配,证明域名所有权5. Let's Encrypt 签发证书申请成功后,证书文件保存在 /etc/letsencrypt/live/mqtt.example.com/:
sudo ls -la /etc/letsencrypt/live/mqtt.example.com/lrwxrwxrwx 1 root root 30 May 18 10:00 cert.pem -> ../../archive/mqtt.example.com/cert1.pemlrwxrwxrwx 1 root root 31 May 18 10:00 chain.pem -> ../../archive/mqtt.example.com/chain1.pemlrwxrwxrwx 1 root root 35 May 18 10:00 fullchain.pem -> ../../archive/mqtt.example.com/fullchain1.pemlrwxrwxrwx 1 root root 33 May 18 10:00 privkey.pem -> ../../archive/mqtt.example.com/privkey1.pem| 文件 | 用途 | 权限 |
|---|---|---|
cert.pem | 服务器证书 | 公开 |
privkey.pem | 服务器私钥 | 必须保密 (600) |
chain.pem | 中间证书链 | 公开 |
fullchain.pem | 服务器 + 中间证书 | 公开 |
Auto Renewal
Section titled “Auto Renewal”自动续期配置
Section titled “自动续期配置”Let’s Encrypt 证书有效期 90 天,Certbot 会自动配置续期任务:
# 测试续期sudo certbot renew --dry-run
# 查看定时任务sudo systemctl list-timers | grep certbot
# 或查看 crontabsudo crontab -l续期验证脚本:
#!/bin/bash# 尝试续期certbot renew --quiet --no-self-upgrade
if [ $? -eq 0 ]; then echo "证书续期成功,重新加载 Mosquitto"
# 复制证书到 Docker volume 目录 cp -L /etc/letsencrypt/live/mqtt.example.com/fullchain.pem \ /opt/mosquitto/certs/ cp -L /etc/letsencrypt/live/mqtt.example.com/privkey.pem \ /opt/mosquitto/certs/
# 重新加载 Mosquitto docker exec mosquitto mosquitto -c /mosquitto/config/mosquitto.conffiCertbot 续期流程:1. systemd timer 每天检查两次2. 如果证书剩余 < 30 天,自动续期3. 续期成功后运行 --post-hook(重新加载服务)4. 如果续期失败,发送邮件通知| 问题 | 原因 | 解决方案 |
|---|---|---|
| ”Failed to connect” | Let’s Encrypt 无法访问服务器 | 检查防火墙和 DNS 解析 |
| ”Too many certificates” | 超过速率限制 | 等待一周或合并域名 |
| ”Invalid challenge” | 验证文件路径错误 | 检查 HTTP 能否访问 /.well-known/ |
| “Permission denied” | 证书文件权限不足 | 设置正确权限:chmod 600 privkey.pem |
Pre-sales Key Points
Section titled “Pre-sales Key Points”| 价值 | 说明 | 沟通要点 |
|---|---|---|
| 零成本 | 免费证书 | ”使用 Let’s Encrypt 获取免费 TLS 证书” |
| 自动续期 | 自动维护 | ”配置好自动续期,不用担心证书过期” |
| 广泛信任 | 所有设备都信任 | ”证书被所有主流系统和浏览器信任” |
| 标准协议 | 广泛兼容 | ”使用行业标准 ACME 协议” |
Summary
Section titled “Summary”本节介绍了 Let’s Encrypt 证书的申请和配置:
- Let’s Encrypt:非营利 CA,提供免费 DV 证书,90 天有效期
- Certbot:官方 ACME 客户端,自动申请和续期
- 申请流程:安装 Certbot → 运行申请 → 域名验证 → 获取证书
- 证书文件:cert.pem(证书)、privkey.pem(私钥)、fullchain.pem(完整链)
- 自动续期:systemd timer 自动检查续期,无需手动操作