Moving PHP’s session storage to tmpfs

Open php.ini

vim /etc/php5/fpm/php.ini

Add following line.

session.save_path = "/var/run/php5/sessions"

Save and exit.

Run following commands.

mkdir -p /var/run/php5/sessions/
chown -R www-data:www-data /var/run/php5/
cp -rp /var/lib/php5/* /var/run/php5/sessions/
service php5-fpm restart

Edit cron on Debian/Ubuntu

vim /etc/cron.d/php5

Find 2 occurrences of /var/lib/php5/ and replace them with /var/run/php5/sessions/

This cronjob keeps deleting old sessions from time to time. If you forget to do this, you will get disk full error.

That’s it!

16 responses to “Moving PHP’s session storage to tmpfs”

  1. I’m having a problem and I think it is related to this. What ownership and permissions does /var/run/php5/sessions/ need?

    • a chown -R www-data:www-data /var/run/php5/sessions followed by chmod -R 755 /var/run/php5/sessions fixed my problem but not sure its the proper solution?

      • Thanks for sharing your solution.

        I used cp -rp on live server. So I might have missed that issue. Anyway, updated article to take care of permission issue.

        • Ah, I don’t usually copy/paste very step, so I guess I overlooked the cp -rp and got into trouble when the session storage could not be written to 🙂

  2. Oh, btw. is /var/run/ automatically mounted on tmpfs? Is this a newer development? Is the whole /var folder on tmpfs? only run? What about /var/cache?

    Sorry to bug you with these questions but it seems you know more about these folders than me 🙂

      • hm, on Debian Wheezy it looks like this:

        /dev/md1 on / type ext3 (rw,usrjquota=quota.user,,jqfmt=vfsv0)
        tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
        proc on /proc type proc (rw,noexec,nosuid,nodev)
        sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
        udev on /dev type tmpfs (rw,mode=0755)
        tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
        devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
        /dev/md0 on /boot type ext3 (rw)
        tmpfs on /var/cache/nginx type tmpfs (rw,size=500M,mode=0755)
        tmpfs on /var/cache/ngx_pagespeed_cache type tmpfs (rw,size=500M,mode=0755)
        /var/run on /run type none (rw,bind)
        /var/lock on /run/lock type none (rw,bind)
        /dev/shm on /run/shm type none (rw,bind)

        so it doesn’t look like it. You see the two tmpfs mounts on /var/cache – I manually added those. Maybe I should add a similar one for the session cache?

          • I’m a bit confused. I tried that but ls -al /var/run simply lists the content of the folder, as does ls -al /var/cache/nginxwhich is definitely mounted on tmpfs.

            Am I doing something wrong or misreading the results?

            ls -al /var/cache/nginx/
            total 4
            drwxr-xr-x 18 www-data root 360 Jun 20 21:21 .
            drwxr-xr-x 19 root root 4096 Jul 5 20:26 ..
            drwx------ 222 www-data www-data 4440 Jul 6 07:04 0
            drwx------ 235 www-data www-data 4700 Jul 6 00:42 1
            drwx------ 223 www-data www-data 4460 Jul 5 19:24 2
            drwx------ 226 www-data www-data 4520 Jul 5 19:22 3
            drwx------ 229 www-data www-data 4580 Jul 6 09:11 4
            drwx------ 219 www-data www-data 4380 Jul 6 08:56 5
            drwx------ 227 www-data www-data 4540 Jul 5 16:43 6

    • Any suggested size? I know it depends on the number of sites and the number of users, etc. so jsut a thumb-suck size would be fine.

      • It depends on RAM you have. You can set it to 50MB I guess. Session data is just textual data I guess!

        This is one reason that I use a big common tmpfs dir and change application config.