配置Nginx 1.30+PHP 8.3启用SSL需两步:先验证并启用with-http_ssl_module模块,再正确配置HTTPS server块与fastcgi_pass指向PHP-FPM socket;同时设置80端口301跳转,并通过phpinfo()验证PHP执行而非下载。

配置 Nginx 1.30 + PHP 8.3 并启用 SSL,核心是两件事:确保 HTTPS 能正常响应,同时 .php 文件能被正确执行(而非下载)。这两者相互独立又紧密关联——SSL 配置错,打不开加密连接;PHP-FPM 链路断,页面直接返回源码或 502 错误。
确认 Nginx 已支持 SSL 模块
Nginx 1.30 默认不带 http_ssl_module,需编译时启用。运行以下命令验证:
nginx -V 2>&1 | grep -o with-http_ssl_module
若无输出,说明未启用。需重新编译安装:
立即学习“PHP免费学习笔记(深入)”;
- 进入 Nginx 源码目录,执行:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module - 接着
make(不要make install),停掉旧服务后替换objs/nginx到安装目录的 bin 目录 - 重启后再次运行
nginx -V确认模块存在
准备并放置 SSL 证书文件
推荐使用 Let’s Encrypt 免费证书(90 天有效期),用 Certbot 一键获取:
certbot certonly --standalone -d yourdomain.com
证书默认存于 /etc/letsencrypt/live/yourdomain.com/,关键文件有:
-
fullchain.pem→ 对应 Nginx 的ssl_certificate -
privkey.pem→ 对应ssl_certificate_key
建议复制到统一路径并设权限:
sudo mkdir -p /etc/nginx/ssl
sudo cp /etc/letsencrypt/live/yourdomain.com/fullchain.pem /etc/nginx/ssl/
sudo cp /etc/letsencrypt/live/yourdomain.com/privkey.pem /etc/nginx/ssl/
sudo chmod 600 /etc/nginx/ssl/privkey.pem
编写 HTTPS + PHP 8.3 共存的 server 块
在站点配置文件(如 /etc/nginx/conf.d/yourdomain.conf)中写入:
server {
listen 443 ssl http2;
server_name yourdomain.com;
root /var/www/html;
index index.php index.html;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
注意三点:
-
fastcgi_pass必须与 PHP-FPM 实际监听方式一致(Unix socket 或 127.0.0.1:9000) -
root和$document_root在 location 内要能正确拼出 PHP 文件绝对路径 - 务必包含
include fastcgi_params;,否则 SCRIPT_FILENAME 可能缺失
强制跳转 HTTPS 并验证 PHP 执行
添加一个仅监听 80 端口的 server 块做重定向:
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
最后检查并重载:
sudo nginx -t && sudo systemctl reload nginx
验证 PHP 是否生效:
- 在
/var/www/html/下新建info.php,内容为<?php phpinfo(); ?> - 浏览器访问
https://yourdomain.com/info.php,看到 PHP 8.3 信息页即成功 - 若提示下载或 502,重点查 PHP-FPM 进程状态、socket 权限、fastcgi_pass 地址三者是否匹配



















