Support: 1-800-961-4454
Sales Chat
1-800-961-2888

Setting up memcached on Cloud Servers

4

By: Adrian Otto

This tutorial explains how to set up memcached for RHEL5 or CentOS5 on Cloud Servers. Although the example is provided for PHP, you can access a memcached server from practically any language using one of the memcached Client API’s.

Step 1: Start up a Cloud Server

Use your Control Panel to start a Cloud Server. For an example, see this video demo.

Step 2: Log Into your Cloud Server using SSH

You may connect as root if your server configuration allows it, or connect as a user and use ‘su’ to get a root shell. I use my Terminal from OSX or PuTTY on Windows.

$ ssh root@12.34.56.7
root@12.34.56.7's password:
[root@mcd-demo ~]#

Step 3: Set up rpmforge

The rpmforge package allows you to download and install third party packages from the DAG RPM archive using the ‘yum’ utility on any RHEL5 or CentOS5 system. First visit this URL and determine the current rpmforge package for your system. Note that all Cloud Servers are 64 bit.

http://dag.wieers.com/rpm/packages/rpmforge-release

Once you have determined the current release for your system, use the ‘rpm’ tool to install it from the correct URL. Example:

# wget http://dag.wieers.com/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
# rpm -ivh rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm

Step 4: Install memcached

Install memcached from rpmforge using yum, set it to run at system boot, and start it up.

# yum -y install memcached
# chkconfig memcached on
# service memcached start

Step 5: Configure memcached

You can skip this skip if you want to use a default installation. Otherwise, you can edit /etc/sysconfig/memcached on your server. You will find these settings by default:

PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

If you plan on using memcached locally on this same server, you can restrict it to only listen on a local UNIX socket and disable networking.

OPTIONS="-s /var/run/memcached/memcached.sock"

Make sure that if you do use a socket file that the directory is created and owned by the user specified by the USER setting.

# mkdir -p /var/run/memcached
# chown nobody /var/run/memcached

If you plan on using memcached over the network, you may want to install it on a non standard port number of your choice.

PORT="22222"

You can also tune the slab size so that it’s suitable for the size of the data you plan on storing in memcached. The default is 48 bytes. This example sets it to 512 bytes:

OPTIONS="-n 512"

To enable UDP, add this option for the UDP port number you want it to listen on:

OPTIONS="-U 22222"

If you want it to use more memory, you can change this setting to the number of megabytes it should use:

CACHESIZE="64"

On a dedicated 1GB Cloud Server, I set mine to 512MB. This allows a lot of memory for connection handling, and the OS, and guarantees that I won’t exhaust the memory and start running from swap:

CACHESIZE="512"

If you have a busy site and expect more than 1024 concurrent connections, you can safely set that maximum much higher:

MAXCONN="20480"

So here is an example settings file that has a bunch of options changed from the default:

PORT="22222"
USER="nobody"
MAXCONN="20480"
CACHESIZE="512"
OPTIONS="-U 22222 -n 512 -l 10.1.2.3"

Note: The above example says it should only listen on the private IP address 10.1.2.3. This will help avoid unauthorized access to your memcached for setups where you access it from your other Cloud Servers. If you want to limit it only to local clients on the same server, then use “-l 127.0.0.1″ instead.

For more information, consult the man page:

# man memcached

Now save the file and restart the memcached daemon:

# service memcached restart

Step 6: Set up a test application

Now let’s set up a simple PHP application that uses memcached to show that it works. First let’s set up an Apache+PHP environment:

# yum -y install httpd httpd-devel php php-pecl-memcache
# service httpd start
# chkconfig iptables off
# service iptables stop

Now, here you have the option of using the easier installation and using the Memcache class in your PHP script, or use a more complex installation to use the superior Memcached class in your PHP script. This procedure requires that you upgrade to version 5.2.0 or newer of PHP. For this tutorial we will show you the easier way to set up the older Memcache class. In this example, I just download the test script from this blog.

# wget -O /var/www/html/test.php

Now browse to your server (use the IP address if you have not set up and DNS for it yet):

Example:

You will notice that if you visit it multiple times within 5 seconds that you only see the cached result and if you wait 5+ seconds between visits to the page that you will see it insert and fetch new data from the cache.

Look at the source code for the example.

Completion

Congratulations! You’ve set up memcached on a Cloud Server! Now it’s time to begin using it in your application to add speed and scalability to your application and start saving money!

About the Author

This is a post written and contributed by Adrian Otto.

Adrian serves as a Principal Architect for Rackspace, focusing on cloud services. He cares deeply about the future of cloud technology, and important projects like OpenStack. He also is a key contributor for and serves on the editing team for OASIS CAMP, a draft standard for application lifecycle management. He comes from an engineering and software development background, and has made a successful career as a serial entrepreneur.


More
4 Comments

[…] Setting up memcached on Cloud Servers […]

avatar Rackspace Cloud Computing & Hosting |  memcached: More Cache = Less Cash! on August 3, 2009 | Reply

# chkconfig iptables off
# service iptables stop

??? You must be kidding. It is like saying: Welcome hackers, you can walk on this red carpet!

avatar Chris on September 28, 2011 | Reply

I think the idea behind that was to quickly setup Apache/PHP for the example. Instead of confusing the user with how to setup Apache/PHP and adding rules to iptables so that it’s accessible from the outside, they kept to the article and showcased memcached.

avatar AcidRaZor on March 11, 2012

Hello,
I have an issue with implementing the memcache in a distributed environment. I have installed two instance of memcache at ports 11211 and 11212 on the server 10.78.44.140. I have a separate server 10.78.65.9 and when i telnet from this server to 11211 port, I am always getting the connection refused error. But when i telnet to 11212 port i could easily connect.

Any insight into what actually is the problem will be very much helpful and a lifesaver. Can you please shed some light on this issue.

Thanks!

avatar Abhilash on March 2, 2012 | Reply

Leave a New Comment

(Required)


Racker Powered
©2014 Rackspace, US Inc.