How to Install Memcached on CentOS 8

By 

Published on

3 min read

Install Memcached on CentOS 8

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

This article shows how to install and configure Memcached on CentOS 8.

Installing Memcached on CentOS

Memcached packages are included in the default CentOS 8 repositories. The installation is pretty easy, enter the following command as root or user with sudo privileges :

Terminal
sudo dnf install memcached libmemcached

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

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

Terminal
sudo systemctl enable memcached --now

To verify that memcached is running, type:

Terminal
sudo systemctl status memcached

The output should look something like this:

output
● memcached.service - memcached daemon
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-04-13 20:12:52 UTC; 2s ago
   ...

That’s it, you have installed Memcached on your CentOS 8 server and you can start using it.

Configuring Memcached

Memcached options can be configured in the /etc/sysconfig/memcached file. By default, Memcached is set to listen only on localhost.

If the client connecting to the server is also running on the same host, you should not make any changes.

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.

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

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.

The first 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:

Terminal
sudo nano /etc/sysconfig/memcached

In the OPTIONS parameter, add the server IP address -l 192.168.100.20. This instructs Memcached to bind to the specified interface only.

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

Save the file and restart the Memcached service for the changes to take effect:

Terminal
sudo systemctl restart memcached

Once the service is configured, the next step is to open the memcached port in your firewall.

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.

Terminal
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

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:

Terminal
sudo dnf install php-pecl-memcache

Python

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

Terminal
pip install pymemcache
Terminal
pip install python-memcached

Conclusion

We’ve shown you how to install Memcached on CentOS 8. For more information on this topic, consult Memcached Wiki .

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

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page