LNMP中 Nginx 与 PHP-FPM 的协同机制
核心原理与角色分工
通信方式与选择
关键配置示例
server {
listen 80;
server_name localhost;
root /srv/www/htdocs;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000; # 与 PHP-FPM 的 listen 一致
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { root /srv/www/htdocs; }
}
listen = 127.0.0.1:9000listen = /var/run/php/php7.4-fpm.sock/srv/www/htdocs/info.php<?php
phpinfo();
?>
http://服务器IP/info.php 验证解析是否正常。常见故障排查与优化
SCRIPT_FILENAME 路径错误或文件不存在,需确保 $document_root$fastcgi_script_name 指向真实文件。systemctl status php-fpm nginx;查看 Nginx error.log 与 PHP-FPM error.log。ss -lntp | grep :9000 或 ls -l /var/run/php/php*.sock;核对 Nginx fastcgi_pass 与 PHP-FPM listen 一致。ls -l 核对 socket 文件 与 Nginx/PHP-FPM 用户 匹配;必要时调整 listen.owner/listen.group 与 Nginx worker 用户。