Setting up memcached on Cloud Servers

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@
root@'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.

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

# wget
# 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:


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.


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:


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:


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


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

OPTIONS="-U 22222 -n 512 -l"

Note: The above example says it should only listen on the private IP address 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” 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):


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.


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!

Previous articleSetting up memcached on Cloud Sites
Next articleMosso Powers Popular Site for 2008 US Presidential Race
Adrian serves as a Distinguished 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.


  1. # chkconfig iptables off
    # service iptables stop

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

    • 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.

  2. 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 I have a separate server 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.


  3. /var/run on Centos 7 boxes is transient, and so is emptied when the box is rebooted. The /var/run/memcached directory won’t be there on startup, and so memcache will start, but won’t write a PID file, which can cause problems with monitoring and even systemd’s idea of ‘status’.

    The best I could come up with was to modify the systemd service unit to include two ExecStartPre lines:

    ExecStartPre=/bin/mkdir -p -m 0755 /var/run/memcached
    ExecStartPre=/bin/chown memcached:memcached /var/run/memcached