Networking

How to install and enable memcached on Ubuntu and CentOS

In order to improve your Apache or Nginx server performance, you might need to aid it with the help of memcached. Learn how to install this caching system on Ubuntu and CentOS.

memcachedhero.jpg
Image: Jack Wallen

Memcached is a caching system that temporarily stores information that would normally be used by a database, in memory. With this in place, in-memory transactions become incredibly fast without stressing the backend database server.

Out of the box, memcached isn't installed or enabled. If you have a database-driven website that would benefit from this kind of caching system, you will definitely want to add memcached to either your Apache or Nginx server.

I'll walk you through the process of enabling this system on Ubuntu 16.04 and CentOS 7. I assume you already have your LAMP (or LEMP) server up and running. With that said, let's get busy.

SEE: Equipment reassignment checklist (Tech Pro Research)

Installing memcached on Ubuntu 16.04

The install on Ubuntu 16.04 will be handled completely through the command line. Before you begin the installation of memcached, you'll want to update apt with the following command:

sudo apt-get update

Once the update completes, issue the following command to install memcached and all of its necessary dependencies:

sudo apt-get install php-memcached memcached

That one command will install everything you need. The installed software comes pre-configured, so there's not much more you need to do except restart Apache with the command sudo service apache2 restart.

Installing memcached on CentOS 7

As with Ubuntu, the installation of memcached on CentOS 7 will be handled completely from the command line. To install, open a terminal window and issue the following commands:

yum clean all
​yum -y update
​yum -y install memcached php-memcached

Unlike Ubuntu, the memcached install on CentOS 7 won't set the service up to run at boot. To do this, issue the following commands:

systemctl start memcached
​systemctl enable memcached
​systemctl status memcached

The status command should show memcached running (Figure A).

Figure A

Figure D

Memcache is now running on CentOS 7.

Testing memcached

These tests can be used on Ubuntu and CentOS platforms. You can test to make sure memcached is loaded by creating the file /var/www/html/info.php with the following contents:

<?
​phpphpinfo();
​?>

Save that file and then point a browser to http://IP_OF_SERVER/info.php (where IP_OF_SERVER is the actual IP address of your server). You should see memcached listed in its own section (Figure B).

Figure B

Figure A

Memcached successfully installed and ready.

An even better test is to ensure that memcached is working as it should. Create the new file /var/www/html/cache_test.php with the following contents:

<?php
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);


$result = $mem->get("blah");


if ($result) {
    echo $result;
} else {
    echo "No matching key found yet. Let's start adding that now!";
    $mem->set("blah", "I am data!  I am held in memcached!") or die("Couldn't save anything to memcached...");
}
?>

Save and close that file. Now, point your browser to http://IP_OF_SERVER/cache_test.php (IP_OF_SERVER is the actual IP address of your server). The test should respond that there is no matching key found (Figure C).

Figure C

Figure B

No matching key found for memcached.

No problem—hit refresh on your browser and that message will change (Figure D).

Figure D

Figure C

We now see memcached at work.

As you can see, memcached has stored data in memory.

Ready for integration

Your memcached-enabled server is ready for integration—how you integrate memcached into your server apps will depend on what platforms (e.g., Drupal, WordPress, etc.) you're serving up. No matter what your software is, your server is now aided by the power of memcached.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox