在项目目录中保存 php 会话时出错 [英] error when saving php sessions in project's directory

查看:28
本文介绍了在项目目录中保存 php 会话时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目配置文件中:

会话:handler_id: session.handler.native_filesave_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"

"%kernel.root_dir%/../var/sessions 属于 wwww-data(是的,php-fpm 作为 www-数据).

当项目运行时,会在该目录中创建一个()会话文件,但显然 php/symfony 尽管能够正确创建它,但实际上在读取/写入它时遇到了麻烦.这是刚刚创建的空会话文件(此请求后没有其他进程运行):

请求终止并引发异常:

<块引用><块引用>

会话数据文件不是由您的 uid 创建的.

我发现了这个答案(还有这个 一个),并通过这样做(将我的会话的 save_path 设置为 "/var/lib/php/sessions" 我能够继续(我可以看到我的会话文件不仅被创建,而且它的内容实际上就位),但我试图理解为什么它无法将会话文件保存在自定义位置.

在我的 PHP 配置中,我看到我将 session.save_path 设置为 /var/lib/php/session,但我希望我能够在执行时间覆盖它.

真正让我烦恼的是会话文件被创建了(所以文件权限应该没问题),但没有写入.

我在 nginx + php-fpm (7.0) 之上运行 Symfony 3.2.2.

为什么会这样?是否有任何其他设置我应该使用以便 php 能够覆盖此配置并将其会话文件写入其他地方?

从跟踪中我看到:

  • NativeSessionStorage ->start() 被调用,并执行session_start(返回true)
  • 然后SessionHandlerProxy ->read($sessionId)被调用,调用$this->handler->read($sessionId)时抛出异常;.

解决方案

我能够重现完全相同的错误,创建会话文件但抛出会话数据文件不是由您的 uid 创建.",但我不是确定这是不是你的情况.

无论如何,我的猜测是您正在使用 nfs 服务器来保存您的代码,但是您在本地开发和运行 nginx.您将 nfs 挂载到服务器目录以将您的代码同步到服务器(我们在我以前的工作中使用过相同的方法).

因此,您将 nfs 服务器目录挂载到本地目录.发生的情况是 php 使用用户 www-data 创建您的会话文件,但该用户在服务器上的 uid 与您的本地机器不同.所以文件被创建,你看到正确的所有者,但它的 uid 是不同的.

检查:

链接

链接

这只是我最好的猜测.如果是这样,请告诉我,也许我可以帮助进行 nfs 用户映射.希望有帮助.

On my project's configuration file I have:

session:
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"

"%kernel.root_dir%/../var/sessions belongs to wwww-data (and yes, php-fpm is running as www-data).

When the project runs, an (empty) session file is created in that dir, but apparently php/symfony have trouble actually reading/writing to it, despite being able to properly create it. Here's the empty session file, just created (no other process ran after this request):

The request dies raising an exception:

Session data file is not created by your uid.

I found this answer (and this one), and by doing that (setting my session's save_path to "/var/lib/php/sessions" I'm able to continue (and I can see that my session files get not only created, but that its contents are actually in place), but I'm trying to understand why it fails to save the session files on the custom location.

In my PHP configuration I see that I have session.save_path set to /var/lib/php/session, but I'd expect I'd be able to override that on execution time.

What really peeves me is that the session file get created (so file permissions should be fine), but not written into.

I'm running Symfony 3.2.2 on top of nginx + php-fpm (7.0).

Why is this happening? Is there any other setting I should use so php is able override this configuration and write its sessions files elsewhere?

From the trace I see that:

  • NativeSessionStorage ->start() is called, and executes session_start (which returns true)
  • Then SessionHandlerProxy ->read($sessionId) is called, and throws the exception when calling $this->handler->read($sessionId);.

解决方案

I was able to reproduce the same exact error, creating the session file but throwing "Session data file is not created by your uid.", but I am not sure if this is your case.

Anyway, my guess is that you are using an nfs server to keep your code, but you develop and run nginx locally. You made a nfs mount to the server directory to sync your code to the server(we had the same in one of my previous employments).

So you mount the nfs server directory to a local directory. What happens is that php creates your session file with user www-data, but this user has a different uid on the server than your local machine. So file is created, you see the correct owner, but its uid is different.

Check:

link

link

This is just my best guess. If this is it, let me know, maybe I could help with nfs user mapping. Hope it helps.

这篇关于在项目目录中保存 php 会话时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆