How To Install and Use Memcached on Ubuntu 14.04

When your website grows to million of page views per month, one of the things be in stress is your database. So you want to help your database from stress using some backend caching mechanism.

One way of dealing with this is leveraging a memory object caching system, like Memcached. Memcache is a caching mechanism that works by temporarily storing the information in memory which would usually retrieve from the database. The first request will be from the database but the next results onwards will be serving from the cache as crazy fast.

So we can start installing the Memcached. There are two Memcache libraries available now. 1) Memcache and Memcached with a trailing d at the end. Both are almost same but Memcached has become more stable and more features by time. Of course, they have written by two different authors.

Install Memcached and the Components

We’re going to install Memcached as well as a MySQL database backend and PHP to handle the interaction.

sudo apt-get update
sudo apt-get install mysql-server php5-mysql php5 php5-memcached Memcached

If you don’t already have MySQL installed, the installation will prompt you to select and confirm an administrator’s password.

This should install and configure everything you need to run Memcached.

How to check if Memcached installed or not

Its very simple to check whether the Memcached installed or not by creating an info.php

sudo nano /var/www/html/info.php

Paste the below code in the file

<?php phpinfo(); ?>

I forget to say you have to restart the apache server.

sudo service apache2 restart

Now you can visit the file address in your server

http://domainname.com/info.php

 

You can search for Memcached text, if you see something like below , you are done with Memcached installation.

Memcached in phpinfo

We can also check whether the Memcached service is running by typing:

ps aux | grep memcached 
root@lamp-512mb-lon1-01:~# ps aux | grep memcached
memcache 2552 0.0 0.5 325396 2536 ? Sl 12:49 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
root 2633 0.0 0.4 11988 2268 pts/0 S+ 12:51 0:00 grep --color=auto memcached
root@lamp-512mb-lon1-01:~#

You can query the service for status by typing:

echo "stats settings" | nc localhost 11211

If you ever need to stop, start, or restart the Memcached service, this can be done by typing something like this:

sudo service memcached restart

Testing memecached with sample script

Create a file called mem_test.php

sudo nano /var/www/html/mem_test.php

paste the below code in that file in order to test the memcache

<?php
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);
$result = $mem->get("mycache");
if ($result) {
echo $result;
} else {
echo "No matching key found. I am adding!";
$mem->set("mycache", "Hey I am a memcached guy from server!") or die("Sorry!Couldn't save anything memcached...");
}
?>

 

Call the file in your browser.

http://yourdomain.com/mem_test.php
On the first load, you will see something like this.

memcahe test script

On the next page load, you will see something like this as it is serving from the cache.
memecache test

As you can see the Memcached service serving cached data. That’s it.

Editorial Staff
 

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.