转自:https://blog.csdn.net/zhangxianhau/article/details/155909098
# Let's Encrypt免费证书与HTTPS配置完全指南
## 前言
2025年了,网站还不上HTTPS?
- 浏览器会标记为"不安全"
- SEO排名受影响
- 无法使用HTTP/2、HTTP/3
- 用户数据传输有风险
以前SSL证书很贵,现在有了Let's Encrypt,完全免费,自动续期,没有理由不用。
## 一、Let's Encrypt简介
### 1.1 什么是Let's Encrypt
Let's Encrypt是一个免费、自动化、开放的证书颁发机构(CA),由非营利组织ISRG运营。
**特点:**
- 完全免费
- 自动化颁发和续期
- 被所有主流浏览器信任
- 单域名/泛域名都支持
### 1.2 证书类型
| 类型 | 说明 | 验证方式 |
|------|------|----------|
| 单域名 | 只对一个域名有效 | HTTP/DNS |
| 多域名(SAN) | 多个域名共用一个证书 | HTTP/DNS |
| 泛域名 | *.example.com | 仅DNS |
### 1.3 验证方式
**HTTP-01验证:**
- 在网站目录放置特定文件
- Let's Encrypt访问验证
- 需要80端口可访问
**DNS-01验证:**
- 添加特定的DNS TXT记录
- Let's Encrypt查询验证
- 适合泛域名、无法开放80端口的场景
## 二、Certbot安装与使用
### 2.1 安装Certbot
```bash
# Ubuntu/Debian
apt update
apt install certbot
# CentOS/RHEL
yum install epel-release
yum install certbot
# 或使用snap(推荐,版本更新)
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot
```
### 2.2 获取证书(HTTP验证)
**独立模式(没有Web服务器时):**
```bash
# 需要80端口空闲
certbot certonly --standalone -d example.com -d www.example.com
```
**Webroot模式(已有Web服务器):**
```bash
# 指定网站根目录
certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com
```
**Nginx插件模式(自动配置):**
```bash
# 安装插件
apt install python3-certbot-nginx
# 自动获取证书并配置Nginx
certbot --nginx -d example.com -d www.example.com
```
### 2.3 获取证书(DNS验证)
```bash
# 泛域名证书
certbot certonly --manual --preferred-challenges dns -d "*.example.com" -d example.com
```
执行后会提示添加DNS TXT记录:
```
Please deploy a DNS TXT record under the name:
_acme-challenge.example.com
with the following value:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
添加记录后等待DNS生效,然后继续。
### 2.4 证书文件说明
证书存放在 `/etc/letsencrypt/live/example.com/`:
| 文件 | 说明 | 用途 |
|------|------|------|
| cert.pem | 域名证书 | - |
| chain.pem | 中间证书链 | - |
| fullchain.pem | 完整证书链 | Nginx ssl_certificate |
| privkey.pem | 私钥 | Nginx ssl_certificate_key |
## 三、Nginx HTTPS配置
### 3.1 基础配置
```nginx
server {
listen 80;
server_name example.com www.example.com;
# HTTP重定向到HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
# SSL证书
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# SSL配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
# HSTS(可选,强制HTTPS)
add_header Strict-Transport-Security "max-age=31536000" always;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
```
### 3.2 安全加固配置
```nginx
# /etc/nginx/conf.d/ssl.conf
# SSL会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# 现代加密套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# DH参数(可选,增强安全性)
# openssl dhparam -out /etc/nginx/dhparam.pem 2048
# ssl_dhparam /etc/nginx/dhparam.pem;
```
### 3.3 测试配置
```bash
# 测试Nginx配置
nginx -t
# 重载配置
nginx -s reload
# 测试SSL
curl -I https://example.com
```
## 四、自动续期配置
### 4.1 证书有效期
Let's Encrypt证书有效期是90天,建议提前30天续期。
### 4.2 手动续期
```bash
# 续期所有证书
certbot renew
# 测试续期(不真正执行)
certbot renew --dry-run
```
### 4.3 自动续期
**方法1:Cron定时任务**
```bash
# crontab -e
0 3 * * * certbot renew --quiet --post-hook "nginx -s reload"
```
**方法2:Systemd Timer(推荐)**
Certbot安装后通常自带timer:
```bash
# 查看timer状态
systemctl status certbot.timer
# 启用timer
systemctl enable certbot.timer
systemctl start certbot.timer
# 查看下次执行时间
systemctl list-timers | grep certbot
```
### 4.4 续期钩子
```bash
# 续期成功后执行的命令
certbot renew --post-hook "systemctl reload nginx"
# 或在配置文件中设置
# /etc/letsencrypt/renewal/example.com.conf
[renewalparams]
post_hook = systemctl reload nginx
```
浙公网安备 33010602011771号