🔥 为什么需要 Let's Encrypt 证书?
Let's Encrypt 是由ISRG推出的免费、自动化、开放的证书颁发机构(CA),旨在推动全网HTTPS化。Certbot是其官方推荐的工具,支持一键申请和自动续期🔒,90天有效期 + 自动续签的特性帮你彻底告别证书过期的噩梦!
🛠️ 环境准备(Ubuntu 24.04)
- 更新系统包:
sudo apt update && sudo apt upgrade -y
- 安装必要依赖:
sudo apt install snapd -y
(Ubuntu 24.04强制使用Snap安装) - 验证80/443端口开放:确保防火墙放行
sudo ufw allow 'Nginx Full'
🚀 三步安装 Certbot
❏ 复制
# 1. 安装Certbot核心组件
sudo snap install --classic certbot
# 2. 创建软链接到系统路径
sudo ln -s /snap/bin/certbot /usr/bin/certbot
💡 通过certbot --version
验证安装,成功会显示类似certbot 2.10.0的版本号
🌐 申请证书(Nginx示例)
▸ 自动模式(推荐给小白)
❏ 复制
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
✨ 系统会自动修改Nginx配置,开启HTTPS重定向,全程交互式提示输入邮箱和协议条款。
▸ 手动模式(适合自定义配置)
❏ 复制
sudo certbot certonly --webroot \
-w /var/www/html \ # 站点根目录
-d yourdomain.com \
-d api.yourdomain.com
⚠️ 使用前需在Nginx配置中添加验证路由:
location /.well-known/acme-challenge/ { root /var/www/html; }
⏰ 搞定自动续期!
Certbot已自动创建续期定时任务,检查方法:
❏ 复制
sudo systemctl list-timers | grep certbot
# 应显示:snap.certbot.renew.timer → 每天自动触发
📌 手动测试续期:sudo certbot renew --dry-run
🚨 常见踩坑解决方案
- 端口占用错误:停用占用80端口的服务(如Apache)
sudo systemctl stop apache2
- 申请通配符证书:需DNS验证
sudo certbot certonly --manual --preferred-challenges=dns -d *.yourdomain.com
- 证书存储位置:所有证书保存在
/etc/letsencrypt/live/yourdomain.com/
💎 终极提示:用
curl https://yourdomain.com
测试HTTPS状态,浏览器锁图标亮起就是胜利!
🎯 写在最后
Let's Encrypt + Ubuntu 24.04的组合让HTTPS部署成本归零🆓。现在就去为你的网站穿上加密盔甲吧!遇到问题?欢迎在评论区开火提问~ 👇
文章评论