By: Adrian Otto
This tutorial explains how to set up Client API’s.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
Use your Control Panel to start a Cloud Server. For an example, see this video demo.
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 email@example.com
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 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
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
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.
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:
To enable UDP, add this option for the UDP port number you want it to listen on:
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 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
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 thefor 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