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.
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.
nano -w /etc/php5/apache2/php.ini
nano -w /etc/php.ini
Search for this two variables:
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
- warning: session_start(): Cannot find save handler memcache - session startup failed moodle
- Cannot find save handler memcache - session