获取 TLS 证书指南
Astralyn

本文介绍两种常见 TLS 证书获取方式:Let’s Encrypt(免费自动续期)和 CloudFlare Origin 证书(仅用于 CDN 场景)。

前置条件

  • 拥有已解析的域名
  • 服务器开放 80/443 端口
  • 对于 Let’s Encrypt:域名需正确解析到服务器 IP

Let’s Encrypt 证书

推荐使用 certbot 自动化获取和续期。

1. 安装 certbot(Debian/Ubuntu)

1
2
3
sudo apt update && sudo apt install snapd
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

2. 申请证书

1
sudo certbot certonly --standalone -d example.com -d www.example.com

-d 指定域名,支持多域名。standalone 模式会临时占用 80 端口。

3. 证书文件说明

1
2
3
/etc/letsencrypt/live/example.com/
├── fullchain.pem # 完整证书链(Nginx/Apache 需要)
└── privkey.pem # 私钥

4. 测试自动续期

1
sudo certbot renew --dry-run

certbot 安装时会自动配置 systemd timer,无需手动添加 cron 任务。

CloudFlare Origin 证书

适用于 CloudFlare CDN 与源站之间的加密。

特点:

  • 证书仅由 CloudFlare 信任
  • 不会过期(生成后永久有效)
  • 在 CloudFlare Dashboard → SSL/TLS → Origin Server 生成

常见问题

Q: certbot 申请失败?

  • 检查域名是否解析到当前服务器 IP
  • 确保 80 端口未被占用
  • 暂时关闭 Nginx/Apache

Q: 如何在 Nginx 中使用 Let’s Encrypt 证书?

1
2
3
4
5
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}