How to Install Memcached on CentOS 7

Posted 

3 min read

Memcached is a free and open-source high-performance in-memory key-value data store. It is mainly used as a caching system to speed up applications by caching various objects from the results of database calls.

In this tutorial, we will explain how to install and configure Memcached on CentOS 7.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Installing Memcached

Memcached packages are included in the default CentOS 7 repositories. The installation is pretty easy, just type the following command:

sudo yum install memcached libmemcached

The libmemcached package contains provides several command line tools for managing the Memcached server.

Once the installation is completed, start and enable the Memcached service

sudo systemctl start memcached
sudo systemctl enable memcached

That's it, at this point you have Memcached installed and running on your CentOS 7 server.

Configuring Memcached

Memcached can be configured by editing the /etc/sysconfig/memcached file. By default, Memcached is set to listen on all interfaces. In the following sections, we will show you how to configure the service for local and remote access.

When improperly configured Memcached can be used to perform a distributed denial-of-service (DDoS) attack.

Local Access Only

If the client connecting to the server is also running on the same host it is recommended to set the Memcached service to listen to localhost only.

To do so, open the memcached configuration file with your text editor:

sudo nano /etc/sysconfig/memcached

In the OPTIONS parameter add -l 127.0.0.1. This instruct Memcached to bind to the specified interface only.

/etc/sysconfig/memcached
OPTIONS="-l 127.0.0.1"

Restart the Memcached service for the changes to take effect:

sudo systemctl restart memcached

Remote Access

If the application that will connect to Memcached is hosted on a remote server, you need to configure your firewall and allow access to the Memcached port 11211 only from the client IP address.

The following example assumes that you want to connect to the Memcached server over a private network. The Memcached server IP is 192.168.100.20 and the client's IP address is 192.168.100.30

CentOS comes with a firewall configuration tool FirewallD. The commands below will create a new zone named memcached, open the port 11211 and allow access only from the client IP address.

sudo firewall-cmd --new-zone=memcached --permanent
sudo firewall-cmd --zone=memcached --add-port=11211/udp --permanent
sudo firewall-cmd --zone=memcached --add-port=11211/tcp --permanent
sudo firewall-cmd --zone=memcached --add-source=192.168.100.30/32 --permanent
sudo firewall-cmd --reload

Once your firewall is configured the next step is to edit the Memcached configuration and set the service to listen on the server's private networking interface:

Open the memcached configuration file:

sudo nano /etc/sysconfig/memcached

In the OPTIONS parameter add the server IP address -l 192.168.100.20:

/etc/sysconfig/memcached
OPTIONS="-l 192.168.100.20"

Save the file and restart the Memcached service:

sudo systemctl restart memcached

Connecting to Memcached

To connect to the Memcached server you need to use a language-specific client.

PHP

To use Memcached as a caching database for your PHP application such as WordPress, Drupal or Magento, you need to install the php-pecl-memcached extension:

sudo apt install php-pecl-memcache

Python

There are several Python libraries for interacting with memcache. You can install your preferred library using pip:

pip install pymemcache
pip install python-memcached

Conclusion

You have learned how to install Memcached on your CentOS 7 server. For more information on this topic consult Memcached Wiki.