How to Install memcached in Ubuntu & CentOS

Last updated on August 9th, 2024 at 06:05 am

Memcached is a powerful caching system that improves website speed and performance. It can be used with PHP, Apache, NGINX and other popular platforms. Here are the steps to install memcached in Apache for Ubuntu, CentOS. You can also use them to install memcached in WordPress, Drupal, Magento and other applications and CMS systems.

What is memcached

Memcached is a free, open-source memory-based caching system used by websites and apps to cache & retrieve data in key-store format. They are often employed by websites to reduce database queries by caching frequent and recent query results. They can be used to cache all sorts of data such as images, files and also full web pages. Memcached can be deployed as a single standalone instance as well as distributed caching system. It supports both Linux as well as Windows systems.

How Memcached Works

Memcached uses a client-server architecture. The server stores an array of key-value combinations that are created by the client and accessed using the keys. These arrays are stored in the server’s RAM. The clients connect to servers using TCP/UDP protocol via one of the many client libraries available. It computes the hash value of a key to determine the server to be used, in case it is a distributed system. Then it reads or write the value using its key.

If the RAM space allocated for Memcached on a server becomes full, it will discard the old key-value pairs to store new ones. So Memcached is a temporary cache and not a permanent one unlike Apache/NGINX caching systems that provide persistent caches. If you want persistent storage, you need to use MemcacheDB or CouchDB that also use Memcached algorithms but deliver a permanent storage for key-value pairs.

How to Install memcached in Ubuntu & CentOS

We will look at how to install memcached in Ubuntu as well as CentOS. The following steps will also enable memcached after installation.

1. Installing memcached in Ubuntu

You can install memcached on Ubuntu/Debian systems using APT package manager. Open terminal and run the following command to update packages

$ sudo apt-get update

Install memcached and its dependencies

$ sudo apt-get install php-memcached memcached

If you also want to install command line tool to manage memcached run the following command.

$ sudo apt-get install libmemcached-tools

You can check its status with the following command.

$ sudo systemctl status memcached

Restart Apache web server to apply changes.

$ sudo service restart apache2

Bonus Read : How to Install mod_session in Ubuntu

2. Installing memcached in CentOS

You can install memcached on CentOS/RHEL/Fedora systems using YUM package manager. Open terminal and run the following commands to install memcached and its dependencies.

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

This will install memcached but not enable it to run on startup. Run the following commands to enable it.

systemctl start memcached ​
systemctl enable memcached ​
systemctl status memcached

You can check memcached status with the following command.

$ sudo systemctl status memcached

Restart Apache web server to apply changes.

sudo systemctl restart httpd.service

Bonus Read : How to Disable mod_security in Apache

3. Verify memcached

To ensure that memcached is installed and enabled, create a test file /var/www/html/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"; 
   $mem->set("blah", "Data from memcached") 
    or die("Couldn't save anything to memcached..."); 
} 
?>

Save and close the file. Open browser and go to http://IP_OF_SERVER/test.php

Replace IP_OF_SERVER with your server’s IP or domain name.

First time, you will see the message “No matching key found yet” since your memcached is empty and populated only on your first request. Refresh your page on browser. This time, you will see the message “Data from memcached” indicating that the response was sent from memcached instead of web server.

4. Configure Memcached

The configuration file of memcached is located at /etc/memcached.conf and the default configuration is more than enough for most users. By default, memcached installation is configured for local client and server systems. Sometimes, you may want to allow remote access to your memcached server. For this purpose, open the memcached configuration file.

$ sudo vi /etc/memcached.conf

Look for the following line.

-l 127.0.0.1

Let us say you want to allow remote access from IP address 54.43.32.21. Then replace 127.0.0.1 with this IP address.

-l 54.43.32.21

Also, update firewall rules to allow access from 54.43.32.21 IP address.

Also, in order to increase the cache size, look for -m option and increase it as per your requirement. -m option specifies the amount of RAM to be used for memcached cache. Its minimum value is 64Mb. Here is an example to increase it to 1Gb.

-m 1024

Save and close the file. Restart Memcached server.

$ sudo systemctl restart memcached

Conclusion

Caching systems are very useful in speeding up your website/app and reducing server load. Memcached is a popular, scalable and free caching system used by famous organizations such as YouTube, Facebook, Twitter, etc. It can be setup as a standalone or a distributed caching system in Linux as well as Windows systems. It works well with all major web servers such as Apache and NGINX and plays well with all popular programming languages such as PHP, Python, Perl, etc. Hopefully, the above article will help you install and configure memcached in Apache for Ubuntu as well as CentOS.