How to Install Memcached on CentOS 7

Published on

3 min read

Install Memcached on CentOS 7

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.


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 provides several command line tools for managing the Memcached server.

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

sudo systemctl start memcachedsudo 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 This instructs Memcached to bind to the specified interface only.


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 and the client’s IP address is

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 --permanentsudo firewall-cmd --zone=memcached --add-port=11211/udp --permanentsudo firewall-cmd --zone=memcached --add-port=11211/tcp --permanentsudo firewall-cmd --zone=memcached --add-source= --permanentsudo 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


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.


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 yum install php-pecl-memcache


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

pip install pymemcache
pip install python-memcached


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

If you have any questions or feedback, feel free to comment below.