Ubuntu LNMP环境如何配置SSL证书
导读:Ubuntu LNMP 环境配置 SSL 证书 一 准备与前提 已部署好 Nginx + PHP-FPM + MySQL,域名已解析到服务器,且可通过 HTTP 80 访问。 开放防火墙端口:建议允许 Nginx Full(包含 80/4...
Ubuntu LNMP 环境配置 SSL 证书
一 准备与前提
- 已部署好 Nginx + PHP-FPM + MySQL,域名已解析到服务器,且可通过 HTTP 80 访问。
- 开放防火墙端口:建议允许 Nginx Full(包含 80/443),或分别放行 80 与 443。
- 具备 sudo 权限,并能编辑站点配置与重载服务。
二 使用 Certbot 自动获取并部署 Let’s Encrypt 证书
- 安装 Certbot 与 Nginx 插件:
- sudo apt update
- sudo apt install certbot python3-certbot-nginx
- 获取并自动配置证书(同时处理 example.com 与 www.example.com):
- sudo certbot --nginx -d example.com -d www.example.com
- 自动续期与验证:
- 启用并启动定时续期:sudo systemctl enable --now certbot.timer
- 测试续期:sudo certbot renew --dry-run
- 防火墙放行(如使用 UFW):sudo ufw allow ‘Nginx Full’。
三 手动配置 Nginx 启用 HTTPS(可选)
- 典型站点配置示例(HTTP 自动跳转 HTTPS + 强加密套件):
- server { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri; }
- server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
安全与性能
ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256; ssl_session_cache shared:SSL:10m;站点根与 PHP
root /var/www/example.com; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # 按实际 PHP 版本调整 } location ~ /.ht { deny all; } }
- 检查并重载 Nginx:
- sudo nginx -t & & sudo systemctl reload nginx。
四 验证与常见问题
- 访问测试:浏览器打开 https://example.com,确认证书有效、链完整、锁标正常。
- 命令行验证:
- 证书链与握手:openssl s_client -connect example.com:443 -servername example.com
- 续期演练:sudo certbot renew --dry-run
- 常见问题排查:
- 端口与防火墙:确认 443 已放行(UFW 可用 sudo ufw allow ‘Nginx Full’)。
- 配置生效:修改后先执行 sudo nginx -t,再 sudo systemctl reload nginx。
- 权限与路径:证书路径通常为 /etc/letsencrypt/live/example.com/,确保 Nginx 可读取。
- PHP 版本匹配:fastcgi_pass 中的 php-fpm.sock 路径需与已安装版本一致(如 php8.1-fpm.sock)。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Ubuntu LNMP环境如何配置SSL证书
本文地址: https://pptw.com/jishu/763943.html
