在 CentOS 上为 Nginx 配置 Let’s Encrypt SSL 证书(详细指南)
以下是针对 Nginx 的完整配置流程,使用 Certbot 工具自动化完成:
一、准备工作
系统要求:
- CentOS 7 或 8
- Nginx 已安装并运行
- 域名已解析到服务器 IP
安装必要组件:
1
2
3
4
5# 安装 EPEL 仓库
sudo yum install epel-release
# 安装 Certbot 和 Nginx 插件
sudo yum install certbot python3-certbot-nginx防火墙配置:
1
2
3sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
二、配置 Nginx 虚拟主机
创建基础配置文件:
1
sudo vim /etc/nginx/conf.d/yourdomain.conf
添加基本配置:
1
2
3
4
5
6
7
8
9server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ =404;
}
}测试并重启 Nginx:
1
2sudo nginx -t
sudo systemctl restart nginx
三、获取 Let’s Encrypt 证书
运行 Certbot:
1
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
交互式配置:
1
2
3
4- 输入邮箱(用于紧急通知)
- 同意服务条款(A)
- 是否分享邮箱(可选)
- 选择重定向选项:推荐选2(强制HTTPS)成功输出:
1
2
3Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/yourdomain.com/privkey.pem
四、验证 Certbot 自动配置
查看 Nginx 配置变化:
1
sudo vim /etc/nginx/conf.d/yourdomain.conf
Certbot 添加的 SSL 配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# ...其他配置...
}
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri; # HTTP重定向到HTTPS
}
五、手动配置 SSL(可选高级设置)
优化 SSL 配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23server {
listen 443 ssl http2;
server_name yourdomain.com;
# 证书路径
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# 安全协议设置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
# 会话设置
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# HSTS 增强安全
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# ...其他配置...
}重启 Nginx:
1
sudo nginx -t && sudo systemctl restart nginx
六、证书自动续订
测试续订功能:
1
sudo certbot renew --dry-run
设置定时任务:
1
2
3
4
5
6# 创建每日检查的定时任务
echo "0 0 * * * /usr/bin/certbot renew --quiet" | sudo tee -a /etc/crontab > /dev/null
# 或者使用 systemd 定时器
sudo systemctl enable certbot-renew.timer
sudo systemctl start certbot-renew.timer续订后自动重载 Nginx:
1
sudo vim /etc/letsencrypt/renewal-hooks/post/reload-nginx.sh
添加内容:
1
2
systemctl reload nginx设置权限:
1
sudo chmod +x /etc/letsencrypt/renewal-hooks/post/reload-nginx.sh
七、验证与测试
在线检测工具:
命令行验证:
1
2
3
4
5
6
7
8# 检查证书信息
sudo openssl x509 -in /etc/letsencrypt/live/yourdomain.com/cert.pem -text -noout
# 检查Nginx配置
sudo nginx -t
# 检查443端口监听
sudo netstat -tuln | grep 443
八、故障排除
常见错误:
Certbot 验证失败:
- 确保域名解析正确
- 检查防火墙是否开放80端口
1
sudo firewall-cmd --list-ports
Nginx 启动失败:
- 检查证书路径权限:
1
sudo chmod 755 /etc/letsencrypt/{live,archive}
- 检查证书路径权限:
混合内容警告:
- 确保网页中所有资源使用 HTTPS
日志检查:
1
2
3
4
5# Certbot 日志
sudo tail -f /var/log/letsencrypt/letsencrypt.log
# Nginx 错误日志
sudo tail -f /var/log/nginx/error.log
最终效果
1 | graph LR |
通过以上步骤,你的 Nginx 服务器将:
- 自动重定向 HTTP → HTTPS
- 使用 Let’s Encrypt 的免费 SSL 证书
- 每90天自动续订证书
- 启用最佳安全实践配置
建议每半年检查一次 SSL 配置,使用在线工具验证安全性设置是否保持最新。