How to Install Memcached on CentOS 8

Posted 

3 min read

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 :

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:

sudo systemctl enable memcached --now

To verify that memcached is running, type:

sudo systemctl status memcached

The output should look something like this:

● 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:

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/memcached
OPTIONS="-l 192.168.100.20"

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

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.

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=192.168.100.30/32 --permanentsudo 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:

sudo dnf install php-pecl-memcache

Python

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

pip install pymemcache
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.