How to Install Memcached (Caching Server) on CentOS 7

Memcached is an open source distributed memory object caching program that allows us to improve and speed up the performance of dynamic web applications by caching data and objects in Memory.

Memcached is also used to cache entire database tables and queries to improve the performance of the database. It is the only caching system available freely and used by many big sites like YouTube, Facebook, Twitter, Reddit, Drupal, Zynga etc.

Memcached can commit to denial of service attacks if not correctly configured. In this article, we will explain how to install and secure your Memcached server on CentOS 7 Linux distribution. These given instructions also works on RHEL and Fedora Linux.

Installing Memcached in CentOS 7

First update your local software package index and then install Memcached from the official CentOS repositories using following yum commands.

# yum update
# yum install memcached
Install Memcached in CentOS 7

Install Memcached in CentOS 7


Next, we will install libmemcached – a client library that offers a couple of tools to manage your Memcached server.

# yum install libmemcached
Install Memcached Library in CentOS 7

Install Memcached Library in CentOS 7

Memcached should now be installed on your CentOS system as a service, along with the tools that required you to test its connectivity. Now we can proceed further to secure its configuration settings.

Securing Memcached Configuration Settings

To make assure that installed Memcached service is listening on the 127.0.0.1 local interface, we will alter the OPTIONS variable in the /etc/sysconfig/memcached configuration file.

# vi /etc/sysconfig/memcached

Search for the OPTIONS variable, and add the -l 127.0.0.1 -U 0 to OPTIONS variable. These configuration settings will protect our server from denial of service attacks.

/etc/sysconfig/memcached

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1 -U 0" 
Secure Memcached Configuration

Secure Memcached Configuration

Let’s discuss each of the above parameters in details.

  1. PORT : The port used by Memcached to run.
  2. USER : The start-up daemon for Memcached service.
  3. MAXCONN : The value used to set max simultaneous connections to 1024. For busy web servers you can increase to any number based on your requirements.
  4. CACHESIZE : Set cache size memory to 2048. For busy servers you can increase up to 4GB.
  5. OPTIONS : Set IP address of server, so that Apache or Nginx web servers can connect to it.

Restart and enable your Memcached service to apply your configuration changes.

# systemctl restart memcached
# systemctl enable memcached

Once started, you can confirm that your Memcached service is bound to the local interface and listening only on TCP connections using following netstat command.

# netstat -plunt
Verify Memcached Server

Verify Memcached Server

You can also check the stats of the server using memcached-tool as shown.

# memcached-tool 127.0.0.1 stats
Check Memcached Running Status

Check Memcached Running Status

Now make sure to allow access to Memcached server by opening a port 11211 on your firewall as shown.

# firewall-cmd --permanent --zone=public --add-port=11211/tcp

Install Memcached PHP extension

Now, install PHP extension to work with Memcached daemon.

# yum install php-pecl-memcache

Install Memcached Perl Library

Install perl library for Memcached.

# yum install perl-Cache-Memcached

Install Memcached Python Library

Install python library for Memcached.

# yum install python-memcached

Restart Web Server

Restart the Apache or Nginx service to reflect changes.

# systemctl restart httpd
# systemctl restart nginx

Cache MySQL Queries with Memcached

It isn’t an easy task for all, you need to use API’s to modify your PHP codes to enable MySQL caching. You can find the examples codes at Memcache with MySQL and PHP.

That’s It! In this article, we have expanded how to install and secure your Memcached server to local network interface. If you have faced any issues during installation, do ask for help in our comment section below.

Posted by Web Monkey