news 2026/5/6 23:25:43

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/3 14:36:13

项目经理别瞎忙!3个能力+1个工具,项目延期从此是路人

项目延期、团队内耗、需求反复&#xff0c;是很多项目经理日常面临的“三座大山”。想要打破困局&#xff0c;不用靠“拼命加班”&#xff0c;关键是抓准核心能力&#xff0c;用对工具和方法&#xff0c;就能实现高效控场。 一、目标拆解能力&#xff1a;把“大目标”拆成“可落…

作者头像 李华
网站建设 2026/4/25 14:24:08

【每天学习一点算法 2026/01/20】汉明距离

每天学习一点算法 2026/01/20 题目&#xff1a;汉明距离 两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。 给你两个整数 x 和 y&#xff0c;计算并返回它们之间的汉明距离。 这个问题最容易想到的方法就是用 异或运算 然后统计结果二进制 1 的个数就…

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

多工厂协同模式下,MES管理系统如何平衡“集团管控”与“边缘自治”

许多企业在业务扩张初期&#xff0c;往往会被早期部署的“烟囱式”MES系统困住。当第二家、第三家工厂在异地拔地而起时&#xff0c;如果系统架构缺乏前瞻性&#xff0c;原有系统往往会因为无法支撑跨地域的数据吞吐和多工厂的业务差异&#xff0c;最终演变成多个互不相通的信息…

作者头像 李华
网站建设 2026/5/2 1:05:00

AI Agent:下一代人工智能的核心范式

AI Agent&#xff1a;下一代人工智能的核心范式 引言 AI Agent&#xff08;人工智能智能体&#xff09;是当前AI领域最热门的话题之一。它代表了从被动响应到主动思考、规划和执行的范式转变。本文将深入剖析AI Agent的核心概念、技术架构以及其广阔的应用前景。 什么是AI Agen…

作者头像 李华
网站建设 2026/5/6 21:58:01

计算机毕业设计springboot基于Springbootvue的教学辅助系统设计与实现 SpringBoot+Vue 智慧课堂协同学习平台的设计与实现 基于SpringBoot与Vue的混合式教学支

计算机毕业设计springboot基于Springbootvue的教学辅助系统设计与实现i7q40 &#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。高校课堂正从“教师单向讲授”向“学生主动建构”迁移…

作者头像 李华