news 2026/4/16 18:18:13

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/4/16 12:25:23

基于Spring Boot的心理咨询预约微信小程序(源码+论文+部署+安装)

感兴趣的可以先收藏起来&#xff0c;还有在毕设选题&#xff0c;项目以及论文编写等相关问题都可以给我留言咨询&#xff0c;我会一一回复&#xff0c;希望可以帮到大家。一、程序背景随着现代社会生活节奏加快&#xff0c;人们心理压力日益增大&#xff0c;心理健康问题成为社…

作者头像 李华
网站建设 2026/4/16 12:58:53

花16800元买线索,不如花768元找老板

在B2B的销售与采购这个领域里面&#xff0c;正在上演着一个极其残酷的情况&#xff1a; 有非常多的企业&#xff0c;每一年都会花费上万元去订阅那些价格昂贵的拓客系统&#xff0c;像探迹、励销云这些都属于此类&#xff0c;然而他们拿到的却只是大量没有用处的名单。 这些名单…

作者头像 李华
网站建设 2026/4/16 16:24:10

创客匠人 AI 智能体重构知识变现生态:创始人 IP 的商业闭环搭建指南

当知识付费市场规模逼近 3000 亿元&#xff0c;用户却越来越 “挑剔”——52.4% 的受访青年认为知识付费 “宣传与实际不符”&#xff0c;54.3% 抱怨缺乏后续服务。这一矛盾背后&#xff0c;是传统知识变现模式的底层缺陷&#xff1a;流量驱动下的 “一锤子买卖”&#xff0c;难…

作者头像 李华
网站建设 2026/4/16 12:56:26

域名常见问题集(十四)——什么是域名投资组合

关于Dynadot Dynadot是通过ICANN认证的域名注册商&#xff0c;自2002年成立以来&#xff0c;服务于全球108个国家和地区的客户&#xff0c;为数以万计的客户提供简洁&#xff0c;优惠&#xff0c;安全的域名注册以及管理服务。 Dynadot平台操作教程索引&#xff08;包括域名邮…

作者头像 李华
网站建设 2026/4/16 9:18:43

Laravel的Migrations:添加interger,string,timestamp类型字段default(null) 的陷阱

引言 我在开发 Laravel 应用程序时&#xff0c; 使用 Laravel 的 Schema Builder 添加整数类型字段时&#xff0c;即使设置了 default(null)&#xff0c;数据库中仍然显示为 NOT NULL。这个看似简单的陷阱可能会导致数据插入失败、应用程序行为异常等问题 问题现象 假设我们需要…

作者头像 李华
网站建设 2026/4/16 2:41:44

新手小白如何从0搭建一个本地CTF靶场,一文详解!

从0搭建一个本地CTF靶场 我们平时大部分练习的CTF靶场都是别人的平台的&#xff0c;所以想着自己搭一个来玩玩&#xff0c;用的是CTFd框架&#xff0c;因为网上的教程也比较多&#xff0c;这次搭建也是比较顺利的&#xff0c;记录一下。 前期准备&#xff1a; centos7.x系统…

作者头像 李华