免费申请 SSL 证书教程(Let’s Encrypt + Certbot)

Xin-dudu 发布于 2025-08-12 154 次阅读


本教程教你如何为你的域名(如 xindudu.online)申请 免费的 HTTPS 证书,并自动配置到 Nginx 服务器上,解决浏览器“不安全”警告。

适用于 Ubuntu/Debian 服务器(其他系统类似)。


📌 前提条件

  1. 拥有一个域名(如 xindudu.online)。
  2. 服务器已安装 Nginx/Apache(本教程以 Nginx 为例)。
  3. 域名已解析到服务器 IP(通过 ping 你的域名 检查)。
  4. 服务器开放 80(HTTP)和 443(HTTPS)端口(确保防火墙允许访问)。

🛠️ 步骤 1:安装 Certbot

Certbot 是 Let's Encrypt 官方推荐的证书管理工具。

① 更新系统并安装 Certbot

sudo apt update && sudo apt upgrade -y
sudo apt install certbot python3-certbot-nginx -y

(如果是 Apache,替换 python3-certbot-nginxpython3-certbot-apache


🔐 步骤 2:申请 SSL 证书

运行以下命令(替换 xindudu.online 为你的域名):

sudo certbot --nginx -d xindudu.online -d www.xindudu.online

过程中会要求你输入:

  1. 邮箱(用于紧急通知和证书续期提醒,输入你的有效邮箱)。
  2. 同意服务条款(输入 Y 并按回车)。
  3. 是否接收 EFF 的推广邮件(可选,输入 YN)。

如果成功,你会看到:

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/xindudu.online/fullchain.pem
Key is saved at: /etc/letsencrypt/live/xindudu.online/privkey.pem

🎉 恭喜!你的网站现在已启用 HTTPS!


⚙️ 步骤 3(可选):检查证书自动续期

Let's Encrypt 证书 90 天过期,但 Certbot 会自动续期。你可以手动测试续期:

sudo certbot renew --dry-run

如果显示 The test was successful,说明自动续期配置正常。


🔍 步骤 4:验证 HTTPS 是否生效

  1. 访问 https://xindudu.online,浏览器应该显示 🔒 安全锁
  2. SSL Labs 测试 检查证书配置是否安全。

🚨 常见问题

❌ 错误:Failed to connect to xindudu.online for verification

  • 原因:域名未解析到服务器,或 80 端口被防火墙拦截。
  • 解决
  • 检查 ping xindudu.online 是否返回你的服务器 IP。
  • 运行 sudo ufw allow 80sudo ufw allow 443 开放端口。

❌ 错误:Certbot couldn't find Nginx

  • 原因:Nginx 未安装或 Certbot 插件错误。
  • 解决:安装 Nginx sudo apt install nginx -y,然后重新运行 Certbot。

📌 总结

步骤操作命令
1安装 Certbotsudo apt install certbot python3-certbot-nginx
2申请证书sudo certbot --nginx -d 你的域名 -d www.你的域名
3测试自动续期sudo certbot renew --dry-run
4验证 HTTPS访问 https://你的域名
此作者没有提供个人介绍。
最后更新于 2025-08-12