Last Updated: February 25, 2016
·
4.592K
· pix-art

Symfony session problems with Vagrant

I created a lamp stacked vagrant box with PHP 5.6 but my sessions didn't want to start. After some digging i noticed my session.save_path was linking too a folder that was "mounted" by vagrant. This caused PHP to fail on writing sessions and caused symfony to not be able to use sessions.

EDIT: This only seems to ocure on Mac

Step 1: Look for you session.save_path in phpinfo();

Picture

If the local value points to a folder that is "mounted" by vagrant this will cause issues. In my case /vagrant/ is my mount folder.

You can also use the following test case to reproduce your problem:

Create a test.php in your project:

<?php

session_save_path('/vagrant/sessions');
session_start();

file_put_contents('/vagrant/sessions/file.txt', 'TEST');

If you run this command line you'll see the file.txt is created successfully and your session create gives the following warning:

PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/vagrant/sessions) in Unknown on line 0

Step 2: Fixing the problem
Create a folder for your sessions to be written in:

mkdir -p /var/lib/php5/<folder>
chown vagrant:vagrant /var/lib/php5/<folder>

Add this line to your config to tell symfony to write you session in the default session path instead of the 1 you saw in Step 1.

framework:
    session:
        handler_id: session.handler.native_file
        save_path: /var/lib/php5/<folder>

Picture

Step 3: Enjoy

Documentation:
- http://symfony.com/doc/current/cookbook/session/sessions_directory.html
- https://ctors.net/2014/04/21/symfony_slow_in_vagrant

All my tips have been moved to my blog www.pix-art.be so come check it out!