We've covered how to install Memcached before on Debianand on Mac OS X. But server software can vary significantly between sites, and this guide can be used to set up Memcached on Red Hat Enterprise Linux (RHEL) or CentOS, which are architecturally the same.
The easiest way to install Memcached is through a package manager such as yum or apt. However, Memcached is not available from the default collection of packages, so the first thing we need to do is add a new RPM (Red Hat Package Manager) server so that we can install Memcached through yum.
One of the best 3rd-party RPM servers is provided by Dag Wieers, which will provide us with up-to-date packages that are not provided by Red Hat directly. The one tricky part of setting up an RPM server is making sure you get the repository that matches your server version and architecture (32-bit or 64-bit). So we need to collect that information first.
Then get the server architecture information. This is a typical response for a 32-bit machine:
Or if you have a 64-bit machine you will probably get something like this:
The server I was using when I wrote this was a 32-bit machine running CentOS version 5.x. So my particular server was:
To install a new RPM server, we can just use the rpmcommand. Note that you must find the RPM server string that matches your architecture and software. Do not use the URL unless you have a 32-bit machine running CentOS 5.x, instead get the server that's appropriate from http://dag.wieers.com/rpm/FAQ.php#B2.
Afterwards you can confirm memcached is up and running by calling it.
And finally restart Apache so that it will pick up the new extension:
Running phpinfo() on your webserver should now confirm that memcache is installed:
Just having memcache installed will not do anything by itself, we need to actually start up some instances of it for our web server to connect to, and we need memcached to automatically start up when the server restarts.
For this we need to install a new script at/etc/init.d/memcached. For this I usually use a custom script that's a bit crude, since it assumes that memcached is being used exclusively for our web server. However, most of the time this is true and it works just fine.
So simply load this script into /etc/init.d. Then set the permissions on it to make it executable:
Then register the script to start up with the server:
Now you can start up memcached as a service.
And you can confirm that memcached has fired up several instances by checking ps.
And that's it! You may need to change the /etc/init.d/memcached file to match your needs depending on what you're using Memcached for. If you're using Memcached with Drupal, you can follow the instructions for changing your settings.php file by following the instructions provided with the Memcache module. Also make sure youconfigure your Firewall to prevent access to Memcache from external URLs.