Memcached is a free & open source, high-performance, distributed memory object caching system that if installed and used correctly will drastically speed up your WordPress powered site or blog.
What is Memcached?
In computing, Memcached is a general-purpose distributed memory caching system that was originally developed by Danga Interactive for LiveJournal, but is now used by many other sites. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached runs on Unix, Linux, Windows and MacOSX and is distributed under a permissive free software license.
Memcached’s APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
Memcached allows you to take memory from parts of your system where you have more than you need and make it accessible to areas where you have less than you need.
Memcached also allows you to make better use of your server memory.
How to install Memcached on CentOS
This is a step by step guide will help you to install Memcached from source on CentOS.
The first thing you will need to do is connect to your webserver. For this article I am connecting using SSH and WinSCP.
You can run the commands in this article by opening up the Console in WinSCP.
If you wish to install the package using yum instead, run yum install memcached.
The version of CentOS that I am using is CentOS release 5.7 (Final); to find what version you are using run the below query:
1 |
cat /etc/redhat-release |
cat /etc/redhat-release
1. We will be installing Memcached as the root user. First we need to make sure we are in the root directory.
1 |
cd /root |
cd /root
2. Now that we are in the root directory we need to create a temporary directory to hold the necessary packages and dependencies so that Memcached can be installed. In this article we will name our directory “memcacheinstall”. Run the below command:
1 |
mkdir memcacheinstall |
mkdir memcacheinstall
Before the Memcached package can be installed correctly we need to install LibEvent. To install LibEvent you will need to navigate to the official website and download the latest package. Once you have download the package place the LibEvent package in the “/root/memcacheinstall” temporary directory.
3. Once you have copied over the latest LibEvent package into the temporary directory run the below commands to continue the installation.
1 |
tar xzvf libevent-latestversion.tar.gz |
tar xzvf libevent-latestversion.tar.gz
4. Change the directory to the LibEvent directory
1 |
cd libevent-latestversion |
cd libevent-latestversion
5. Configure and make the LibEvent install.
1 |
./configure; make; make install |
./configure; make; make install
6. Now that we have that installed it is time for us to download Memcached and install it to the server. We need to change directories again. Run the below command:
1 |
cd /usr/local/src |
cd /usr/local/src
7. At the time of this article the latest package is version 1.4.13. To check the latest version visit the Memcached website. Run the below command to download the latest version:
1 |
curl -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz |
curl -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
8. Unzip and extract the package:
1 |
tar xzvf memcached-1.4.13.tar.gz |
tar xzvf memcached-1.4.13.tar.gz
9. Change the directory to our Memcached installation directory:
1 |
cd memcached-1.4.13 |
cd memcached-1.4.13
10. Configure and make install:
1 |
LDFLAGS='-Wl,--rpath /usr/local/lib' ./configure --prefix=/usr/local
|
LDFLAGS='-Wl,--rpath /usr/local/lib' ./configure --prefix=/usr/local
1 |
make && make install |
make && make install
11. Now that we have it installed, a good thing to do at this stage in to start Memcached. Run the below command to confirm that it will start:
1 |
memcached -u root -d |
memcached -u root -d
12. OK, so all is working great to this point. Lets now confirm that Memcached has started and is working. Run the below command:
1 |
ps aux | grep memcached |
ps aux | grep memcached
Stopping Memcached
To stop Memcached from running simply run the below command:
1 |
pkill memcached |
pkill memcached
Running Memcached as a PHP module
A great way to run Memcached is through the use of a PHP module. The reason was do this is that if we do not load it as a module in PHP, you basically cannot use Memcache with PHP.
Let us begin by log into WHM, go to Software -> Module Installers.
Select PHP PECL -> Manage.
Try to search for Memcached and then install the available package of Memcached module.
If there is no error, then you’re almost done.
If you encoutered an error when installing the module WHM do not fear. WHM sometimes displays an error in the installation process of a Memcached module for PHP? Let work around the problem by doing a symbolic link as below.
Add the below command:
1 |
ln -s /usr/lib/php/extensions/no-debug-non-zts-20090626/memcache.so /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so |
ln -s /usr/lib/php/extensions/no-debug-non-zts-20090626/memcache.so /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
1 |
chmod 0755 /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
|
chmod 0755 /usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
Finally, restart Apache.
Ensure that Memcached will start on server startup
To ensure that Memcached starts you will manually need to edit the file: /etc/rc.local
Open the file and add the below code:
1 |
memcached -d -u nobody& |
memcached -d -u nobody&
Save the file.
The command above to start Memcached and add to /etc/rc.local is very general. You can further customize the command to make Memcached works better for your server since each server has different specifications and customizations such as amount of memory (i.e., RAM), CPU etc.
Incorporate Memcached into WordPress through the W3 Total Cache plugin
Now that you have successfully installed Memcached and have it running it is time to incorporate it into your WordPress installation so that you can speed up your site and benefit from what the package has to offer.
For this article I am going to assume that you already have the W3 Total plugin installed and working on your WordPress site.
1. Navigate to the W3 Total Cache -> General page in your WordPress Dashboard.
2. Select “Memcached” from the available options in the dropdown list.
3. Save your settings.
4. Clear caches if necessary.
5. Browse your website and make sure it is working properly.
Conclusion
If all worked well then you should see an incredible improvement in the speed and loading time of your WordPress website or blog. For more information and the official Memcached documentation be sure to visit the Memcached website.
Disclaimer: Please carefully considering the risks of installing any new software onto your production servers. Also, you must do your own research prior to and in addition to following the tips in this article. Without understanding the risks and the technicality of installing something as detailed as Memcached onto your server, and without knowing why you even need something as Memcached, then you should think twice about following this tutorial. We will not be held responsible for any server errors or crashes.
Be sure to check out our other great WordPress Articles and Plugins



























