Let's Encrypt官网提供了多种安装方法,今天我在这里主要使用最通用的snap方式(类似apt,yum包管理工具)来安装证书的自动化工具certbot,Let's Encrypt提供了90天的免费证书,同时可以配置为自动续期来达到永久使用的目的。
首先,我们先了解下Let's Encrypt、Certbot是什么。
? 一、Let’s Encrypt 简介
Let’s Encrypt 是由 ISRG(Internet Security Research Group)主导的免费证书颁发机构(CA)。 它的目标是让每个网站都能免费、自动、安全地使用 HTTPS。
特点:
✅ 完全免费;
✅ 自动化申请与续期;
✅ 浏览器完全信任;
✅ 开源、标准化。
? 二、Certbot 是什么?
Certbot 是 Let’s Encrypt 官方推荐的自动化工具,用来:
验证域名所有权;
获取与安装 SSL/TLS 证书;
自动配置 Web 服务器(如 Nginx、Apache);
定期续期证书。
Let’s Encrypt 官方已将 Snap 作为主要发布渠道,因此推荐使用 Snap 方式安装最新版本的 Certbot。
? 三、系统准备
1️⃣ 环境要求
操作系统:Ubuntu / Debian / CentOS / Rocky / AlmaLinux / RHEL;
已安装并运行的 Nginx;
域名已解析到该服务器;
80 端口可被公网访问(用于 HTTP 验证),也可以使用DNS验证。
2️⃣ 检查 Nginx 是否运行
sudo systemctl status nginx
? 四、安装 Snap 与 Certbot
1️⃣ 安装 snapd
sudo apt update
sudo apt install snapd -y
sudo systemctl enable --now snapd.socket
? 如果是 CentOS:
sudo yum install epel-release -y
sudo yum install snapd -y
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap
2️⃣ 删除 certbot-auto 和任何 Certbot OS 软件包
根据你的操作系统所依赖的包管理工具,选择以下相应的命令来移除系统自带certbot工具。
sudo apt-get remove certbot
or
sudo dnf remove certbot
or
sudo yum remove certbot
3️⃣ 安装 Certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
验证安装:
certbot --version
? 五、申请免费 HTTPS 证书(自动配置 Nginx)
使用以下命令一键生成证书并自动配置 Nginx:
sudo certbot --nginx -d example.com -d www.example.com
如果你只想生成证书,自己配置nginx,可以使用参数certonly,执行以下命令:
sudo certbot certonly --nginx -d example.com -d www.example.com
执行后 Certbot 会:
1、自动检测 Nginx 配置;
2、创建临时验证文件,完成域名所有权验证;
3、向 Let’s Encrypt 请求签发证书;
4、修改 Nginx 配置文件,添加 SSL 证书路径;
5、询问是否将 HTTP 自动重定向到 HTTPS(建议选择 “2”)。
执行成功后会提示:
Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
访问:
https://example.com
即可看到绿色锁标志。
? 六、验证证书配置
检查 Nginx 配置:
sudo nginx -t
sudo systemctl reload nginx
证书文件路径:
# 证书:/etc/letsencrypt/live/example.com/fullchain.pem# 私钥:/etc/letsencrypt/live/example.com/privkey.pem
? 七、自动续期配置
Let’s Encrypt 证书有效期为 90 天,Certbot 会自动续期。 你可以测试续期是否正常:
sudo certbot renew --dry-run
输出:
Congratulations! All renewals succeeded.
说明自动续期功能可用。
自动续期定时任务(systemd)
Certbot(Snap 安装)默认已注册 systemd 定时任务(如果证书 将在 30 天内过期,就会自动续期),可通过以下命令查看:
sudo systemctl list-timers | grep certbot
若希望手动设置,可添加 cron:
sudo crontab -e
加入:
0 3 * * * certbot renew --quiet && systemctl reload nginx
? 八、验证机制(原理简述)
Let’s Encrypt 使用 HTTP 验证(HTTP-01 Challenge):
1、Certbot 向 Let’s Encrypt 申请证书;
2、Let’s Encrypt 要求服务器在特定路径放置一个验证文件;
3、Certbot 临时在 Nginx 中创建路径:
http://example.com/.well-known/acme-challenge/
5、Let’s Encrypt 验证文件内容;
6、验证通过后颁发证书。
若想使用DNS验证,验证命令如下:
sudo certbot certonly --manual --preferred-challenges dns -d "*.example.com" -d example.com
命令执行后,按提示在dns解析里添加一条 TXT 记录,添加后等待几分钟,等待dns更新完成,不然又要重来。
这种方式要实现证书自动延续,若你使用的是aliyun,那么可以安装一个DNS 插件: certbot-dns-aliyun,来达到自动续期的目的。
? 九、常见问题与排查
1、Connection refused -> 80端口未放行
解决方法:开放HTTP:sudo ufw allow 80 或云防火墙放行
2、Timeout during connect > DNS未生效 / 网络不通
解决方法:检查 ping example.com
3、Invalid response > .well-known 路径被拦截
解决方法:检查 Nginx rewrite 规则
4、重定向导致验证失败 > 所有HTTP请求被强制跳转HTTPS
解决方法:临时注释重定向或保留验证路径
? 十、结语
通过以上步骤,你的 Nginx 网站已启用 Let’s Encrypt 免费 HTTPS 证书,并具备 自动续期与安全加密 能力。 这不仅让数据传输更安全,也能显著提升网站的可信度。
本文链接:https://kinber.cn/post/5720.html 转载需授权!
推荐本站淘宝优惠价购买喜欢的宝贝: