How to install Memcached on CentOS 8

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

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

Installing Memcached on CentOS

Memcached packages are included in the standard CentOS 8 repositories. Installation is quite simple, enter the following command as root or as a 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 complete, enable and start the Memcached service by typing:

sudo systemctl enable memcached --now

To make sure memcached is running, enter:

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-16 14:34:22 UTC; 1s ago
   ...

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

Memcached setup

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

If the client connecting to the server is also running on the same host, then you shouldn’t make any changes.

Remote access

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

If configured incorrectly, 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 IP address 192.168.20.20 of the Memcached server is there, and the IP address of the client is 192.168.20.30.

The first step is to edit the Memcached configuration and configure the service to listen on the server’s private network interface:

Open the memcached configuration file:

sudo nano /etc/sysconfig/memcached

In the OPTIONS parameter, add the server IP address -l 192.168.20.20. Memcached needs this to bind only to the specified interface.

/ etc / sysconfig / memcached

OPTIONS="-l 192.168.20.20"

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

sudo systemctl restart memcached

After configuring the service, the next step is to open the memcached port in the firewall.

CentOS comes with the FirewallD firewall configuration tool. The commands below will create a new zone named memcached, open port 11211, and only allow access from the client’s 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.20.30/32 --permanent
sudo firewall-cmd --reload

Memcached connection

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

PHP

To use Memcached as a caching database for your PHP application like 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 memcached. You can install your preferred library using pip:

pip install pymemcache
pip install python-memcached

Conclusion

We showed you how to install Memcached on CentOS 8. For more information on this topic, refer to the Memcached Wiki.

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

Sidebar