You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Apache迁移至Nginx后配置PHP include_path的问题求助

解决Nginx中PHP include_path不生效的问题

问题根源

你在Nginx虚拟主机里重复定义fastcgi_param PHP_VALUE,这会导致后面的配置覆盖前面的,最终只有最后一行的/usr/share/php/被设置到include_path里,这和你错误日志里显示的include_path='/usr/share/php'完全对应。

另外你的location ~ \.(php|html|htm)$块里的try_files $uri =404/有语法错误,=404后面不需要加斜杠。

正确配置方案

把所有include路径合并到一个fastcgi_param PHP_VALUE指令里,用冒号分隔,和Apache的配置格式一致:

location ~ \.(php|html|htm)$ {
    try_files $uri =404;
    include snippets/fastcgi-php.conf;
    include /etc/nginx/fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass   unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param SERVER_NAME $host;
    # 合并所有include路径,和Apache配置对齐
    fastcgi_param  PHP_VALUE  "include_path=/sites/web-test1/vendor/webtoolkit/src:/sites/web-test1/private:/usr/share/php";
}

额外验证步骤

  1. 修改配置后,重启Nginx和PHP-FPM:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
  1. 可以在PHP文件里添加<?php echo get_include_path(); ?>,访问页面确认include_path是否正确加载。
  2. 确保目标目录权限正确,让www-data用户有读取权限:
sudo chown -R www-data:www-data /sites/web-test1/vendor /sites/web-test1/private
sudo chmod -R 755 /sites/web-test1/vendor /sites/web-test1/private

为什么之前的配置无效?

Nginx中重复定义同一个fastcgi_param时,最后一个定义会覆盖所有前面的,所以你之前的四次设置最终只有/usr/share/php/生效,这就是PHP找不到includes/emailpriv.inc.php的原因——该文件所在的目录不在生效的include_path里。

内容的提问来源于stack exchange,提问作者Canadian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 12:15:35