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://hostrequest_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/

```

  1. **监控证书过期**

```bash

查看证书过期日期

certbot certificates | grep "Expiry Date"

```

  1. **使用强密码保护私钥**

```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. **邮箱通知**:证书过期前会发送邮件到指定邮箱,请确保邮箱可访问

参考资源


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

```bash

检查证书

certbot certificates

测试 HTTPS

curl -I https://your-domain.com/

检查续期定时器

systemctl status certbot-renewal.timer

```

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

相关推荐
每天吃饭的羊2 小时前
hash结构
开发语言·前端·javascript
吃吃喝喝小朋友2 小时前
JavaScript异步编程
前端·javascript
Trae1ounG3 小时前
Vue生命周期
前端·javascript·vue.js
程序员小李白3 小时前
js数据类型详细解析
前端·javascript·vue.js
weixin_462446233 小时前
Python用Flask后端解析Excel图表,Vue3+ECharts前端动态还原(附全套代码)
前端·python·flask·echats
满栀5853 小时前
jQuery 递归渲染多级树形菜单
前端·javascript·jquery
闲蛋小超人笑嘻嘻3 小时前
Flexbox 属性总结
前端·css
TOPGUS3 小时前
谷歌将移除部分搜索功能:面对AI时代的一次功能精简策略
前端·人工智能·搜索引擎·aigc·seo·数字营销