Memached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load and session management. Lets focus on session management first and build up a caching daemon to store PHP sessions in a load balanced environment. In this post I will explain how you can easily install it and make it available in LAMP.
If you have a cluster of multiple instances(web servers) behind a load balancer , for example, Elastic Load Balancer (ELB) without sticky sessions, persistent PHP session storage will definitely be a matter of concern. You won’t be able to store sessions across multiple instances. In these situations, you usually save the user sessions in a fast storage ( RAM ) , where all your servers can access, and which is usually memcached. So lets start with the installation.
Activate the RPMForge custom repository http://dag.wieers.com/rpm/packages/rpmforge-release/
Download your specific RPM and install it:
rpm –install rpmforge-release-*.rpm
yum install –enablerepo=rpmforge memcached
Now memcached is installed.
You can test it:
memcached -d -m 2048 -l 10.0.0.40 -p 11211 -u nobody
This starts memcached up as a daemon, using 2GB of memory, and listening on IP 10.0.0.40, port 11211. The -m switch specifies the amount of memory in megabytes. The -l switch specifies the IP to listen on and finally the -p switch specifies the port to listen on.
Install PHP Memcache extension
tar -xvf memcache-2.2.5.tgz
Copy the file memcache.so to the default module directory.
In my case it was /usr/lib/php/modules.
# vi /etc/php.ini ( your php configuration file )
This should do !
If you check your phpinfo() you should see a MemCache section appear.
You can now fully use the MemCache functionality in your PHP.
Also make sure that you give the path of the memcached server in the php.ini of the client servers.