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!


  1. ovidiu Avatar

    any measurable effect of doing this?

    1. Rahul Bansal Avatar

      I did not measured so cannot count. I am trying to move as much temporary data to tmpfs as possible.

  2. ovidiu Avatar

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

    1. ovidiu Avatar

      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?

      1. Rahul Bansal Avatar

        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.

      2. ovidiu Avatar

        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 🙂

      3. Rahul Bansal Avatar

        No. No. I added cp -rp after your comment. Earlier it was cp -r only. 🙂

  3. ovidiu Avatar

    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 🙂

    1. Rahul Bansal Avatar

      You can find that always using mount command.

      /var/run is symlink to /run and /run is mounted on tmpfs. Atleast in Ubuntu 12.04.

      1. ovidiu Avatar

        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?

      2. Rahul Bansal Avatar

        You can do either way.

        Or just run ls -l /var/run to check if its a real dir or symlink.

      3. ovidiu Avatar

        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

  4. Rahul Bansal Avatar

    Your /var/runis not a symbolic link. You can add another tmpfs mount point.

    1. ovidiu Avatar

      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.

      1. Rahul Bansal Avatar

        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.

  5. Rahul Bansal Avatar


    Please check “Edit cron on Debian/Ubuntu” section. Without it, you may run into errors/other issues.

Book a strategy call
with our experts

No obligation. Limited slots.

Contact Form Business Enquiry

"*" indicates required fields

Please attach any RFP, project specification, or document that you would like to share.
Drop files here or
Max. file size: 5 GB.
    This field is for validation purposes and should be left unchanged.