没有不值得去解决的问题,也没有不值得去学习的技术!

基于 LNMP 2.1 部署后,接口响应 500 ,原因在于 .user.ini 的配置 open_basedir 有误

查看 Nginx 的 error log。报错:PHP message: PHP Warning: require(): open_basedir restriction in effect. File(/home/wwwroot/qdx/src/vendor/autoload.php) is not within the allowed path(s)

1、接口响应 500。如图1

接口响应 500
图1

2、查看 Nginx 的 error log。报错:PHP message: PHP Warning: require(): open_basedir restriction in effect. File(/home/wwwroot/object/src/vendor/autoload.php) is not within the allowed path(s):如图2

查看 Nginx 的 error log。报错:PHP message: PHP Warning:  require(): open_basedir restriction in effect. File(/home/wwwroot/object/src/vendor/autoload.php) is not within the allowed path(s)
图2
Plaintext

2025/03/11 10:46:31 [error] 323148#0: *3 FastCGI sent in stderr: "PHP message: PHP Warning:  require(): open_basedir restriction in effect. File(/home/wwwroot/object/src/vendor/autoload.php) is not within the allowed path(s): (/home/wwwroot/object/src/api/web/:/tmp/:/proc/) in /home/wwwroot/object/src/api/web/index.php on line 6PHP message: PHP Warning:  require(/home/wwwroot/object/src/vendor/autoload.php): failed to open stream: Operation not permitted in /home/wwwroot/object/src/api/web/index.php on line 6PHP message: PHP Fatal error:  require(): Failed opening required '/home/wwwroot/object/src/api/web/../../vendor/autoload.php' (include_path='.:/usr/local/php/lib/php') in /home/wwwroot/object/src/api/web/index.php on line 6" while reading response header from upstream, client: 101.204.100.38, server: objectapi.ikhw.cn, request: "POST /unregistered-user/get-id HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "objectapi.ikhw.cn"



3、参考:LNMP添加、删除虚拟主机及伪静态使用教程 防跨目录设置  查看 .user.ini 中的内容

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z object]# cat .user.ini
open_basedir=/home/wwwroot/object:/tmp/:/proc/


4、编辑 .user.ini。.user.ini文件无法直接修改,如要修或删除需要先执行:chattr -i /home/wwwroot/object/.user.ini

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z object]# cat .user.ini
open_basedir=/home/wwwroot/object/src:/tmp/:/proc/


5、仍然报错。最终决定删除掉 .user.ini。参考:LNMP下防跨站、跨目录安全设置,仅支持PHP 5.3.3以上版本 直接在 php.ini 中编辑

Plaintext

2025/03/11 10:53:59 [error] 323149#0: *5 FastCGI sent in stderr: "PHP message: PHP Warning:  require(): open_basedir restriction in effect. File(/home/wwwroot/object/src/vendor/autoload.php) is not within the allowed path(s): (/home/wwwroot/object/src/api/web/:/tmp/:/proc/) in /home/wwwroot/object/src/api/web/index.php on line 6PHP message: PHP Warning:  require(/home/wwwroot/object/src/vendor/autoload.php): failed to open stream: Operation not permitted in /home/wwwroot/object/src/api/web/index.php on line 6PHP message: PHP Fatal error:  require(): Failed opening required '/home/wwwroot/object/src/api/web/../../vendor/autoload.php' (include_path='.:/usr/local/php/lib/php') in /home/wwwroot/object/src/api/web/index.php on line 6" while reading response header from upstream, client: 101.204.100.38, server: objectapi.ikhw.cn, request: "POST /unregistered-user/get-id HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "objectapi.ikhw.cn"

Plaintext

2025/03/11 10:53:59 [error] 323149#0: *5 FastCGI sent in stderr: "PHP message: PHP Warning:  require(): open_basedir restriction in effect. File(/home/wwwroot/object/src/vendor/autoload.php) is not within the allowed path(s): (/home/wwwroot/object/src/api/web/:/tmp/:/proc/) in /home/wwwroot/object/src/api/web/index.php on line 6PHP message: PHP Warning:  require(/home/wwwroot/object/src/vendor/autoload.php): failed to open stream: Operation not permitted in /home/wwwroot/object/src/api/web/index.php on line 6PHP message: PHP Fatal error:  require(): Failed opening required '/home/wwwroot/object/src/api/web/../../vendor/autoload.php' (include_path='.:/usr/local/php/lib/php') in /home/wwwroot/object/src/api/web/index.php on line 6" while reading response header from upstream, client: 101.204.100.38, server: objectapi.ikhw.cn, request: "POST /unregistered-user/get-id HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "objectapi.ikhw.cn"

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系