• Call: +1 (858) 429-9131

How to configure Memcached on AWS EC2: A Starter’s Guide

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:

[bash]
wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-*.rpm
rpm –install rpmforge-release-*.rpm
yum install –enablerepo=rpmforge memcached
[/bash]

Now memcached is installed.

You can test it:

[bash]
memcached -d -m 2048 -l 10.0.0.40 -p 11211 -u nobody
[/bash]

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

[bash]
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar -xvf memcache-2.2.5.tgz
cd memcache-2.2.5
phpize
./configure –enable-memcache
make
make install
[/bash]

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 )

Add

[bash]
extension=memcache.so
[/bash]

This should do !

Now restart your web server

[bash]/etc/init.d/httpd restart[/bash]

If you check your phpinfo() you should see a MemCache section appear.
You can now fully use the MemCache functionality in your PHP.

Now set the session.save_handler as memcache

[bash]session.save_handler = memcache[/bash]

Then set the session.save_ path. Mention all the server ip’s that you want to share the sessions using memcached.

[bash]session.save_path = “tcp://127.0.0.1:11211 , tcp://192.168.1.1:11211” [/bash]

Also make sure that you give the path of the memcached server in the php.ini of the client servers.


Associated Links

  • Cross-platform software
  • Network management
  • Routing
  • Hypertext Transfer Protocol
  • Session
  • LAMP
  • Resin
  • Web server

  • 4 Comments

    1. Dennis says:

      very well written article. Thank you. it helped me

    2. Vinod Krishna says:

      Thanks Dennis !.

    3. Nguyen Thanh Binh says:

      Thanks your article.

    Leave a Reply