ScaleScaleScaleScale

Tips / Nginx


How to store PHP sessions in Memcached

memcached

Memcached is one of those really good things that will help you to reduce server load and gain more app performance / speed. Today I’m going to show you how to store PHP sessions in Memcached instead of storing on the filesystem (default on all PHP installations).

Why should I store my PHP sessions in Memcached?

Because RAM memory is always faster than file storage based on hard drives (even if you use SSD drives).  If you store sessions in memory instead of files your web applications will reduce server load and get a better performance. Sessions will be stored in memory, but there is a tricky difference, if you store your sessions at the filesystem, those can be there forever. However, when you store sessions in Memcached when your VPS/Dedicated server is turned off or gets restarted sessions will be deleted, keep that in mind.

Requirements:

Most shared hosting providers don’t offer memcached to their users, so, in order to start with this tutorial, you’ll need root access to the server, which you can do with a VPS or Dedicated server. In my opinion this are two of the best VPS/Dedicated providers you will find:

  • A Small Orange
  • A2 Hosting

Let’s begin now.

Installing Memcached on Ubuntu / CentOS

Check out this two guides, which are basic pre requisites in order to continue with this tutorial

I will assume that you alredy have PHP installed, so, in order to store php sessions in Memcached, all you need to do is modify a simple variable at your php.ini file.

Ubuntu users

nano -w /etc/php5/apache2/php.ini

CentOS users

nano -w /etc/php.ini

Search for this two variables:

session.save_handler
session.save_path

You must modify/set this variables as you see below:

session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

If you use apache reload the service to apply the changes:

sudo service apache2 reload (ubuntu)
service httpd reload (centos)

If you use php-fpm:

sudo service php5-fpm reload (ubuntu)
service php-fpm reload (centos)

Popular search terms:

  • php session memcached
  • memcached session save path
  • session php fpm
  • Cannot find save handler memcache - session
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Dongpt

    My configuration on Ubuntu 14.0 + php 5.5.9
    php.ini

    session.save_handler = memcached
    session.save_path = "127.0.0.1:11211"

    Pecl

    Installed packages, channel pecl.php.net:
    =========================================
    Package Version State
    memcache 2.2.7 stable

    But got this error when trying to load the page:

    Warning (2): session_start(): Cannot find save handler 'memcached' - session startup failed [CORE/Cake/Model/Datasource/CakeSession.php, line 615]

    This system is new installation of ubuntu 14.04. The same code is working on 12.04 and I still can’t find the root cause. Kindly advise.

    Thanks!

    • Dongpt

      It ends up that after add the PHP session configuration for memcache then PHP-FPM can’t start up. And there is nothing in php-fpm.log. 🙁

  • admin

    It seems like memcached (the system daemon) is not running / installed, ensure it’s running first:

    netstat -tap | grep memcached
    fuser -v 11211/tcp

    If it’s already running try this:

    session.save_path=”tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15″

    If that doesn’t work, try using PHP memcache module:

    apt-get install php5-memcache
    

    Then ensure memcache PHP module is loaded in your PHP configuration:

    php -i | grep memcache
    

    If not, ensure /etc/php5/conf.d/memcache.ini contains this line:
    extension=memcache.so

    It should show memcache module.

    service php-fpm restart
    

    Then at php.ini set:

    session.save_handler = memcache
    
  • Dongpt

    My first trying:
    root@ubuntu:/etc/php5/fpm# ps -efa | grep memcache
    memcache 15791 1 0 Aug22 ? 00:00:08 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -M
    root 19316 18769 0 01:25 pts/0 00:00:00 grep --color=auto memcache
    root@ubuntu:/etc/php5/fpm# netstat -tap | grep memcached
    tcp 0 0 localhost:11211 *:* LISTEN 15791/memcached
    root@ubuntu:/etc/php5/fpm# fuser -v 11211/tcp
    USER PID ACCESS COMMAND
    11211/tcp: memcache 15791 F.... memcached
    root@ubuntu:/etc/php5/fpm# pwd
    /etc/php5/fpm
    root@ubuntu:/etc/php5/fpm# grep "session.save_handler" php.ini | grep -v ";"
    session.save_handler = memcached
    root@ubuntu:/etc/php5/fpm# grep "session.save_path" php.ini | grep -v ";"
    session.save_path="tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
    root@ubuntu:/etc/php5/fpm# service php5-fpm status
    php5-fpm stop/waiting
    root@ubuntu:/etc/php5/fpm# service php5-fpm start
    php5-fpm start/running, process 19355
    root@ubuntu:/etc/php5/fpm# service php5-fpm status
    php5-fpm start/running, process 19411
    root@ubuntu:/etc/php5/fpm# service php5-fpm status
    php5-fpm stop/waiting
    root@ubuntu:/etc/php5/fpm#

    Seems it doesn’t work. Then the 2nd trying:
    root@ubuntu:/etc/php5/fpm# dpkg --list | grep php5-memcache
    ii php5-memcache 3.0.8-4build1 amd64 memcache extension module for PHP5
    root@ubuntu:/etc/php5/fpm# php -i | grep memcache
    /etc/php5/cli/conf.d/20-memcache.ini,
    memcache
    memcache support => enabled
    memcache.allow_failover => 1 => 1
    memcache.chunk_size => 8192 => 8192
    memcache.default_port => 11211 => 11211
    memcache.default_timeout_ms => 1000 => 1000
    memcache.hash_function => crc32 => crc32
    memcache.hash_strategy => standard => standard
    memcache.max_failover_attempts => 20 => 20
    Registered save handlers => files user memcache
    root@ubuntu:/etc/php5/fpm# cat /etc/php5/conf.d/memcache.ini
    ; uncomment the next line to enable the module
    extension=memcache.so

    [memcache]
    memcache.dbpath="/var/lib/memcache"
    memcache.maxreclevel=0
    memcache.maxfiles=0
    memcache.archivememlim=0
    memcache.maxfilesize=0
    memcache.maxratio=0

    root@ubuntu:/etc/php5/fpm# vi php.ini
    root@ubuntu:/etc/php5/fpm# grep "session.save_handler" php.ini | grep -v ";"
    session.save_handler = memcache
    root@ubuntu:/etc/php5/fpm# service php5-fpm status
    php5-fpm stop/waiting
    root@ubuntu:/etc/php5/fpm# service php5-fpm start
    php5-fpm start/running, process 19510
    root@ubuntu:/etc/php5/fpm# service php5-fpm status
    php5-fpm start/running, process 19590
    root@ubuntu:/etc/php5/fpm# service php5-fpm status
    php5-fpm stop/waiting
    root@ubuntu:/etc/php5/fpm#

    🙁

  • admin

    Memcached & php-memcache are ok. Weird… then it must be something related to the session.save_handler and/or session.save_path.