白嫖Let's Encrypt SSL 证书指南

匿名作者
2026-01-11 02:4586

适用环境:Ubuntu / Debian / CentOS 目标域名www.downly.cn

本文档介绍如何使用 Let's Encrypt (Certbot) 申请免费 SSL 证书,并配置 Nginx 及自动续期。本方案采用 Standalone 模式,稳定性高且通用性强。


1. 前提条件

⚠️ 必须先完成域名解析! Certbot 需要验证您拥有该域名的控制权。如果域名未解析到当前服务器 IP,验证将失败。

bash
# 验证 DNS 解析 (应返回服务器 IP,如 123.45.67.123) nslookup www.downly.cn

2. 安装 Certbot

根据您的操作系统执行对应的安装命令(二选一):

Ubuntu / Debian

Bash

bash
apt-get update apt-get install certbot -y

CentOS 7 / 8+

Bash

bash
# CentOS 7 yum install epel-release certbot -y # CentOS 8+ dnf install epel-release certbot -y

3. 申请证书

我们使用 standalone 模式,它需要暂时占用 80 端口来完成验证。

bash
# 1. 停止 Nginx (释放 80 端口) systemctl stop nginx # 2. 申请证书 certbot certonly --standalone -d www.downly.cn # 3. 启动 Nginx systemctl start nginx

成功提示:申请成功后,证书文件将保存在 /etc/letsencrypt/live/www.downly.cn/ 目录下。


4. 配置 Nginx

编辑 Nginx 配置文件:

Bash

bash
vim /etc/nginx/conf.d/www.downly.cn.conf

写入以下配置:

nginx
server { listen 80; listen [::]:80; server_name www.downly.cn; return 301 https://www.downly.cn$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name www.downly.cn; ssl_certificate /etc/letsencrypt/live/www.downly.cn/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.downly.cn/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; location / { proxy_pass http://12.213.230.14:50001; proxy_set_header Domain $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

验证并生效:

bash
systemctl reload nginx

5. 配置自动续期

Let's Encrypt 证书有效期为 90 天。我们需要配置 Crontab 定时任务,建议 每 2 个月 自动续期一次。

  1. 编辑定时任务:

    bash
    crontab -e
  2. 添加以下内容(系统通用):

    bash
    # 每 2 个月的 15 号凌晨 2:15 自动续期 # --pre-hook: 续期前停止 Nginx 以释放端口 # --post-hook: 续期完成后重启 Nginx 15 2 15 */2 * certbot renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

附录:常用操作

证书文件位置

文件用途Nginx 配置项
fullchain.pem完整证书链ssl_certificate
privkey.pem私钥 (严禁泄露)ssl_certificate_key

验证命令

bash
# 模拟续期测试(检查配置是否正确) certbot renew --dry-run # 查看证书有效期 certbot certificates

申请多域名

如果需要在一张证书中包含多个域名:

bash
certbot certonly --standalone -d www.downly.cn -d downly.cn

相关推荐

评论 (0)

暂无评论,快来发表第一条评论吧!

AI 技术峰会

2025 AI 技术峰会

AI 实战课程

AI 实战课程

热门工具

AI 助手

智能对话,提升效率

智能图像处理

一键美化,智能修图

AI 翻译

多语言实时翻译