17

I switched servers recently, and now my home page won't work. It gives the following text:

Warning: session_start() [function.session-start]: open(/var/lib/php/session/sess_eqbchncji8kj22f0iqa9g3v7u2, O_RDWR) failed: Permission denied (13) in /var/www/vhosts/alt.alternativedc.com/httpdocs/index.php on line 6

Warning: Unknown: open(/var/lib/php/session/sess_eqbchncji8kj22f0iqa9g3v7u2, O_RDWR) failed: Permission denied (13) in Unknown on line 0

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

I assumed that this meant that the session folder was not writable, so I ran the following command after I ssh-ed into the server:

chmod o+rw /var/lib/php/session

That didn't seem to solve the problem. Not sure what to do now...

Daniel Kats
  • 5,141
  • 15
  • 65
  • 102
  • Does that particular session file exist? You may have to chmod all the session files in there as well. – Marc B Jul 08 '11 at 17:45
  • "didn't seem to solve the problem." What does that mean? – webbiedave Jul 08 '11 at 17:47
  • @webbiedave Means nothing changed - the errors persisted. @Marc B not sure I can do that. I think PHP creates a new file for each session ID. I cannot anticipate session IDs. not sure if I am right, though... – Daniel Kats Jul 08 '11 at 17:50
  • @BlackSheep: Marc B simply means to also run `chmod o+rw /var/lib/php/session/*` then try again. – webbiedave Jul 08 '11 at 17:52
  • @webbiedave Ran chmod o+rw /var/lib/php/session/*, got chmod: cannot access `/var/lib/php/session/*': No such file or directory – Daniel Kats Jul 08 '11 at 17:59

7 Answers7

27

Try changing your session save path in your php config file, /tmp is a good location.

php.ini

session.save_path = /tmp

http://www.php.net/manual/en/session.configuration.php#ini.session.save-path

Eddie
  • 12,898
  • 3
  • 25
  • 32
  • Works great on OSX 10.10 – smerten Sep 28 '15 at 10:35
  • 3
    "Warning: If you leave this set to a world-readable directory, such as /tmp (the default), other users on the server may be able to hijack sessions by getting the list of files in that directory." – jgraup Feb 02 '16 at 19:32
  • In my case I just needed to remove the "3;" from the default session.save_path = "3;/var/lib/php/sessions" – John Doe Mar 01 '18 at 20:59
19

Just had the same issue on CentOS:

chown -R apache:apache /var/lib/php/session

Making the httpd user the ower of the session directory should work, as well.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
7

You probably changed a parent folder's permissions recursively, most likely to your own user.
Go to your sessions folder:
cd ~;cd /var/lib/php/

If you find a sessions folder, just write these two commands in your terminal:
cd ~; to go home, then
sudo chown -R www-data:www-data /var/lib/php/session

Or, if your sessions folder is "sessions" instead of "session":
cd ~; to go home, then
sudo chown -R www-data:www-data /var/lib/php/sessions

This way your server will be able to write sessions into your project.
I'm Quite sure about this approach.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
daniel Warui
  • 188
  • 2
  • 4
2

both of tmp and /var/lib/session must be chmod 1777

and problem solved.

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Muharrem
  • 45
  • 1
  • 4
    Welcome to Stack Overflow. When you answer a question, please format your answer nicely (spelling, grammar, punctuation). In this case, also explain what chmod 1777 does, and better yet, explain it in more generic terms also, like "make sure that `/tmp` and `/var/lib/session` are writable. – Martijn de Milliano Apr 24 '13 at 20:20
  • 1777 permissions can be risky. https://unix.stackexchange.com/questions/64126/why-does-chmod-1777-and-chmod-3777-both-set-the-sticky-bit – Keith John Hutchison Sep 04 '18 at 01:27
2

Try changing the owner of the session directory to www-data. To do this run this command sudo chown -R www-data /var/lib/php/sessions. This works for me.

1

I tried all the solutions here but they didn't work, because php.ini was being overwritten by other configs.

To find the culprit I used this trick:

grep -lR 'php_value' /etc/

And there it was /etc/httpd/conf.d/php.conf messing it up. So I changed its value from php_value session.save_path "/var/lib/php/session" to php_value session.save_path "/tmp".

After restarting Apache (service httpd restart) it finally worked!

Community
  • 1
  • 1
carla
  • 1,970
  • 1
  • 31
  • 44
0

on ubuntu 12.04 /var/lib/php5 has 1733 permission I change in php.ini session.save_path to /tmp to correctly store sessions alternatively you can set parameter in your code by ini_set('session.save_path',path_where_apache_have_permission_777);

Giacomo
  • 79
  • 1
  • 2