is one of those really good things that will help you to reduce load and gain more app performance / speed. Today I’m going to show you how to store sessions in Memcached instead of storing on the filesystem (default on all 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.

Installing Memcached on / 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  reload (centos)

If you use php-fpm:

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

Comments

comments

Bài viết liên quan