My website won't save PHP sessions properly, I originally posted on it in SO, but it's increasingly looking like a server issue. So, I figured I would re-phrase the question here.

Why would this PHP server config fail to save and retrieve sessions?

One of the key symptoms seems to be that PHP is not creating any files in /var/lib/php/session. Here are the current permissions form the server:

$ ls -al /var/lib/php/session/
total 28
drwxrwx--- 2 root apache 24576 Jul 20 13:30 .
drwxr-xr-x 3 root root    4096 Jul 20 13:30 ..

Here are some test URLs. This first link creates a session array and prints the output, the second just prints session output, but unfortunately it's blank.




I see you are using nginx and port 7080 for apache.

If ever your architecture includes multiple apache servers and some kind of load balancing, then you must do something for sessions to be shared accross multiple apache server (memcached, clever load balancing, nfs, mysql, ... ).

Edit #1

IMHO, as you have only one webserver, there is no reason for now to try using memcached for storing sessions.

At some point, you managed to configure php to use files to store sessions but session files were not effectively written /var/lib/php/session

That is why you loose session when you jump from one page to another.

/var/lib/php/session permissions are usually fine and sessions work out the box in most linux and bsd distro.

To check that permissions are ok do the following :

Step 1 : Check that php is configured to write to /var/lib/php/session using your url that calls phpinfo(). You should get something like :

Registered save handlers => files user
session.save_path => /var/lib/php/session

step 2 : Check under which user apache is running both with ps and by looking at config files.

You should obtain apache , www , www-data or http.

step 3 :

Check that apache user has write access to session path. This can be done by typing ls -ld /var;ls -ld /var/lib;ls -ld /var/php;ls -ld var/lib/php/session

or by effectively creating a file with apache user

su - www -s /bin/bash or su -m www


touch /var/lib/php/session/test

If permission is not the problem, we'll can try other stuff

  • I installed memcached. Didn't realise there was further config for sessions to work. Not sure if it's load balancing either. – jnthnclrk Aug 2 '12 at 12:39
  • Do you use multiple server? Is memcached supposed to share sessions between all your servers? Does session work if you use files to store them and only one web server? – user130370 Aug 2 '12 at 13:26
  • Pretty sure it's only 1 server. I'll add some test pages to the question above now. Not sure how to use files to store sessions. – jnthnclrk Aug 2 '12 at 13:28
  • You seem to use files for session saving: session.save_handler files session.save_path /var/lib/php/session; Are there any file created in /var/lib/php/session while you use your test page? – user130370 Aug 2 '12 at 13:43
  • # ls /var/lib/php/session is empty... – jnthnclrk Aug 2 '12 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.