news 2026/5/1 10:38:03

Let‘s Encrypt HTTPS 证书配置指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Let‘s Encrypt HTTPS 证书配置指南

# Let's Encrypt HTTPS 证书配置指南

本指南用于在 Amazon Linux 2023 系统上使用 Let's Encrypt 免费证书为 Nginx 配置 HTTPS。

## 前置条件

- 系统:Amazon Linux 2023
- Web 服务器:Nginx
- 域名已正确解析到服务器 IP
- 防火墙已开放 80 和 443 端口

## 配置步骤

### 1. 检查系统环境

```bash
# 检查操作系统版本
cat /etc/os-release

# 检查 Nginx 是否已安装
nginx -v

# 检查域名 DNS 解析
nslookup your-domain.com
```

### 2. 安装所需软件

```bash
# 更新软件包
dnf update -y

# 安装 Nginx(如果未安装)
dnf install -y nginx

# 启动并启用 Nginx
systemctl start nginx
systemctl enable nginx

# 安装 Certbot
dnf install -y certbot

# 安装 Certbot Nginx 插件
dnf install -y python3-certbot-nginx
```

### 3. 确保 Nginx 配置正确

```bash
# 检查 Nginx 配置文件语法
nginx -t

# 如果配置文件存在,确认 HTTP 80 端口配置正确
# 编辑 Nginx 配置文件(根据实际情况修改)
vi /etc/nginx/conf.d/your-site.conf
```

**Nginx HTTP 配置示例:**

```nginx
server {
listen 80;
server_name your-domain.com;

location / {
root /var/www/your-site;
index index.html;
try_files $uri $uri/ /index.html;
}
}
```

```bash
# 重新加载 Nginx 配置
systemctl reload nginx
```

### 4. 获取 Let's Encrypt 证书

```bash
# 方法一:使用 Certbot 自动配置(推荐)
# 将 your-domain.com 替换为您的实际域名
certbot --nginx -d your-domain.com --non-interactive --agree-tos --email admin@yourdomain.com --redirect

# 方法二:如果需要多个域名
certbot --nginx -d your-domain.com -d www.your-domain.com --non-interactive --agree-tos --email admin@yourdomain.com --redirect

# 方法三:仅获取证书不自动配置(手动配置)
certbot certonly --nginx -d your-domain.com --non-interactive --agree-tos --email admin@yourdomain.com
```

**参数说明:**
- `--nginx`: 使用 Nginx 插件
- `-d`: 指定域名
- `--non-interactive`: 非交互模式
- `--agree-tos`: 同意服务条款
- `--email`: 证书过期提醒邮箱
- `--redirect`: 自动将 HTTP 重定向到 HTTPS

### 5. 手动配置 Nginx(如果方法一失败)

如果自动配置失败,请手动配置:

```bash
# 备份原配置文件
cp /etc/nginx/conf.d/your-site.conf /etc/nginx/conf.d/your-site.conf.backup

# 编辑配置文件
vi /etc/nginx/conf.d/your-site.conf
```

**HTTPS 配置示例:**

```nginx
server {
listen 80;
server_name your-domain.com;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
server_name your-domain.com;

# SSL 证书路径
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

# SSL 配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

# 您的网站配置
location / {
root /var/www/your-site;
index index.html;
try_files $uri $uri/ /index.html;
}
}
```

```bash
# 测试并重新加载 Nginx
nginx -t
systemctl reload nginx
```

### 6. 配置证书自动续期

#### 方法一:使用 systemd timer(推荐)

```bash
# 创建续期服务文件
cat > /etc/systemd/system/certbot-renewal.service << 'EOF'
[Unit]
Description=Let's Encrypt SSL Certificate Renewal
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx"
EOF

# 创建定时器文件
cat > /etc/systemd/system/certbot-renewal.timer << 'EOF'
[Unit]
Description=Daily Let's Encrypt SSL Certificate Renewal Timer

[Timer]
OnCalendar=daily
RandomizedDelaySec=1h
Persistent=true

[Install]
WantedBy=timers.target
EOF

# 重新加载 systemd 配置
systemctl daemon-reload

# 启用并启动定时器
systemctl enable certbot-renewal.timer --now

# 检查定时器状态
systemctl status certbot-renewal.timer
systemctl list-timers certbot-renewal.timer
```

#### 方法二:使用 crontab(备选)

```bash
# 编辑 crontab
crontab -e

# 添加以下内容(每天凌晨 3 点检查续期)
0 3 * * * certbot renew --quiet --deploy-hook 'systemctl reload nginx'
```

### 7. 验证配置

```bash
# 1. 检查证书信息
certbot certificates

# 2. 测试 HTTPS 访问
curl -I https://your-domain.com/

# 3. 测试自动重定向
curl -I http://your-domain.com/

# 4. 测试证书续期(不会实际续期)
certbot renew --dry-run

# 5. 查看 Nginx 错误日志
tail -f /var/log/nginx/error.log

# 6. 查看 Certbot 日志
tail -f /var/log/letsencrypt/letsencrypt.log
```

## 常用管理命令

### 证书管理

```bash
# 查看已安装的证书
certbot certificates

# 手动续期所有证书
certbot renew

# 续期特定证书
certbot renew --cert-name your-domain.com

# 撤销证书
certbot revoke --cert-path /etc/letsencrypt/live/your-domain.com/cert.pem

# 删除证书
certbot delete --cert-name your-domain.com
```

### Nginx 管理

```bash
# 检查配置文件语法
nginx -t

# 重新加载配置(不中断服务)
systemctl reload nginx

# 重启服务
systemctl restart nginx

# 查看状态
systemctl status nginx
```

## 故障排查

### 1. 证书获取失败

**问题:DNS 解析失败**
```bash
# 检查域名是否解析到正确 IP
nslookup your-domain.com
dig your-domain.com +short

# 确保防火墙开放 80 和 443 端口
# 对于 Amazon Linux 2023 / Amazon Linux 2
sudo iptables -L -n | grep -E ':(80|443)'

# 对于使用 Security Groups 的 EC2 实例
# 请在 AWS 控制台确认入站规则包含 80 和 443 端口
```

**问题:端口被占用**
```bash
# 检查 80 和 443 端口占用情况
netstat -tlnp | grep -E ':(80|443)'
ss -tlnp | grep -E ':(80|443)'
```

### 2. 自动续期失败

**问题:定时器未运行**
```bash
# 检查定时器状态
systemctl status certbot-renewal.timer

# 手动启动定时器
systemctl start certbot-renewal.timer

# 查看定时器日志
journalctl -u certbot-renewal.timer
journalctl -u certbot-renewal.service
```

**问题:证书续期测试失败**
```bash
# 运行详细测试
certbot renew --dry-run --force-renewal

# 查看详细日志
certbot renew --dry-run --force-renewal -v
```

### 3. HTTPS 无法访问

**问题:证书链不完整**
```bash
# 检查证书文件
ls -la /etc/letsencrypt/live/your-domain.com/

# 应该包含以下文件:
# cert.pem chain.pem fullchain.pem privkey.pem README
```

**问题:Nginx 配置错误**
```bash
# 检查 Nginx 错误日志
tail -100 /var/log/nginx/error.log

# 测试配置文件
nginx -t

# 如果配置正确,重新加载
systemctl reload nginx
```

## 证书路径说明

Let's Encrypt 证书文件位于 `/etc/letsencrypt/live/your-domain.com/` 目录:

- **fullchain.pem**: 完整证书链(服务器证书 + 中间证书)
- **privkey.pem**: 私钥文件
- **chain.pem**: 中间证书
- **cert.pem**: 服务器证书

**重要**:Nginx 配置中应使用 `/etc/letsencrypt/live/your-domain.com/fullchain.pem` 和 `/etc/letsencrypt/live/your-domain.com/privkey.pem`,而不是直接链接到 archive 目录,因为 live 目录包含符号链接,会在证书更新时自动指向最新证书。

## 安全建议

1. **定期备份证书**
```bash
# 备份 Let's Encrypt 目录
tar -czf /backup/letsencrypt-$(date +%Y%m%d).tar.gz /etc/letsencrypt/
```

2. **监控证书过期**
```bash
# 查看证书过期日期
certbot certificates | grep "Expiry Date"
```

3. **使用强密码保护私钥**
```bash
# 设置私钥文件权限(仅 root 可读)
chmod 600 /etc/letsencrypt/live/your-domain.com/privkey.pem
```

## 多域名证书

如果您需要为多个域名配置证书:

```bash
# 为多个域名获取单个证书
certbot --nginx -d domain1.com -d www.domain1.com -d domain2.com -d www.domain2.com --non-interactive --agree-tos --email admin@yourdomain.com --redirect

# 为不同域名分别获取证书
certbot --nginx -d domain1.com --non-interactive --agree-tos --email admin@domain1.com --redirect
certbot --nginx -d domain2.com --non-interactive --agree-tos --email admin@domain2.com --redirect
```

## 注意事项

1. **域名 DNS 解析**:确保域名已正确解析到服务器 IP,否则无法获取证书
2. **防火墙设置**:确保 80 和 443 端口对外开放
3. **证书有效期**:Let's Encrypt 证书有效期为 90 天,系统会自动续期
4. **续期限制**:Let's Encrypt 有速率限制,同一域名每周最多获取 5 个证书
5. **邮箱通知**:证书过期前会发送邮件到指定邮箱,请确保邮箱可访问

## 参考资源

- Let's Encrypt 官网: https://letsencrypt.org/
- Certbot 文档: https://certbot.eff.org/docs/
- Nginx SSL 配置: https://nginx.org/en/docs/http/configuring_https_servers.html

---

**配置完成后,请使用以下命令验证:**

```bash
# 检查证书
certbot certificates

# 测试 HTTPS
curl -I https://your-domain.com/

# 检查续期定时器
systemctl status certbot-renewal.timer
```

如果所有检查都通过,说明 HTTPS 证书配置成功!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/1 7:57:52

[特殊字符]_Web框架性能终极对决:谁才是真正的速度王者[20260120170511]

作为一名拥有10年开发经验的全栈工程师&#xff0c;我经历过无数Web框架的兴衰更替。从早期的jQuery时代到现在的Rust高性能框架&#xff0c;我见证了Web开发技术的飞速发展。今天我要分享一个让我震惊的性能对比测试&#xff0c;这个测试结果彻底改变了我对Web框架性能的认知。…

作者头像 李华
网站建设 2026/5/1 9:54:44

告别走马观花!红松小课助推银发文旅向高品质“学游”升级

随着“活到老学到老”理念深入人心&#xff0c;银发群体的文旅需求正从“景观打卡”向“学游一体”深度转型。以退休生活兴趣社区红松为例&#xff0c;其凭借线上兴趣学习线下实景游学的创新模式&#xff0c;精准契合老年群体需求&#xff0c;不仅让万千老年学员实现了“游中求…

作者头像 李华
网站建设 2026/5/1 8:41:39

滴滴 wsgsig

声明 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由 此产生的一切后果均与作者无关&#xff01; 部分python代码 url "https:…

作者头像 李华
网站建设 2026/4/25 8:59:11

太香了!JNPF 国际化功能实操指南,高效管理多语言翻译

做国际化系统时&#xff0c;还在为多语言翻译维护头疼&#xff1f; 服务端内置翻译和客户端业务翻译分散管理、新增语言要逐处修改、批量导入容易出错&#xff1f; JNPF 国际化功能提供了一站式解决方案 —— 支持服务端与客户端翻译分类管理&#xff0c;可灵活新增、编辑、批…

作者头像 李华
网站建设 2026/4/29 12:35:52

如何将联系人从 Android 传输到 PC

失去联系人可能会带来很大的不便。无论您是要升级手机、备份数据&#xff0c;还是只是需要访问计算机上的联系人列表&#xff0c;了解如何将联系人从 Android 传输到 PC 总是很有用的。按照本指南&#xff0c;获得 6 个实用方法&#xff0c;然后轻松转移您的联系人。第 1 部分&…

作者头像 李华
网站建设 2026/5/1 9:56:34

想跳槽的心,已经达到了顶峰!

这几年&#xff0c;IT行业里冲动离职的人越来越少了&#xff0c;大家都变得更理智——先骑着驴&#xff0c;慢慢找马。如果你也在职悄悄看机会&#xff0c;其实心里都清楚&#xff1a;早晚得动&#xff0c;但总想着“等过完年再说吧”。可时间从来不等人。等着等着&#xff0c;…

作者头像 李华