I am running Ubuntu 12.04 in the Vagrant VM on the OSX host machine.
Here the piece of Vagrant config to mount a folder:
config.vm.synced_folder "/var/projects", "/projects", type: "nfs"
I found out that PHP cannot properly create a session on the NFS shared directory.
Here is the test.php script:
session_save_path('/projects/sessions');
session_start();
file_put_contents('/projects/sessions/file.txt', 'TEST');
Here is the output of the execution:
$ php test.php
PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/project/sessions) in Unknown on line 0
Here is what happens after execution:
$ ls -l
total 8
-rw------- 1 502 dialout 0 Jul 21 10:13 sess_0p6bt4g3o0sofi3b7p6016jtg7
-rw-rw-r-- 1 502 dialout 4 Jul 21 10:13 file.txt
-rw-rw-r-- 1 502 dialout 164 Jul 21 10:12 test.php
As you can see, the session file is empty and is create with the strange permissions. Thus, the txt file is created without errors.
I've tried with PHP 5.4 and 5.5
Any other suggestions to try?
Thank you in advance
I've run into this problem as well :(
Unfortunately I haven't been able to figure out why this is happening. It has something to do with how PHP writes session data in combination with a NFS synced folder in a vagrant guest. (All other write operations function normally, it's specific to session writes.)
I eventually worked around the problem by storing sessions outside of the synced folders. In my case I simply used /tmp. For a vagrant guest the security risk for doing this is acceptable IMHO, because it only runs on your local machine. I wouldn't recommend this on real (production) servers though, but there you won't have this issue ;)
The owner (502) / group (dialout) of files & directories within the synced folders is normal for a vagrant guest on a OS X host.
502 is the uid of your user on the OS X host. That user has its gid set to 20 (staff), which is normal for OS X users. When mounting synced folders through NFS, the same uid & gid will show up in the vagrant guest. There the uid 502 isn't mapped to a user, which is why 502 will show when you do a ls -l. The gid 20 is mapped to the group dialout, so that's why that will show.
I'm mentioning this because you can find a lot of resources on the internet pointing to this "strange" user / group info as being the source of write problems, but it isn't. This is not "strange" at all (but perfectly normal for NFS synced folders on OS X hosts) and will not cause write problems. The fact that you can write a file with file_put_contents() proves that.
By default PHP will use the value files for the session.save_handler directive, which results in sessions being stored on a filesystem.
Certain extensions (like Sqlite, Memcached, Redis, etc) will register different handlers for you to use. You can enable them by setting session.save_handler to sqlite, memcached, redis, etc. Consult the proper documentation on how to configure them.
Or you can use a custom handler by setting session.save_handler to user. Then you can implement anything you like. This is a common way to store sessions in a MySQL database. These docs should get you started:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With