Install and Configure Memcached on AlmaLinux 8

In this article, we want to teach you how to Install and Configure Memcached on AlmaLinux 8.

Memcached is a free and open-source high-performance memory caching system. It’s typically used to cache database data, API calls, or page rendering chunks in RAM to increase the application performance.

It can store data as little as a number, or as big as a finished HTML page.

How To Install and Configure Memcached on AlmaLinux 8

To set up Memcached on AlmaLinux 8, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do these, you can follow our article about the Initial Server Setup with AlmaLinux 8.

Now let’s see how to set up Memcached o your server.

How To Set up Memcached on AlmaLinux 8

Memcached packages are available in the default AlmaLinux repository. First, update your local package index with the following command:

sudo dnf update

Now you can install Memcached on AlmaLinux 8 with the command below:

sudo dnf install memcached libmemcached

This will provide several command-line tools for managing the Memcached server.

Now you need to start and enable the Memcached on AlmaLinux 8 with the following command:

sudo systemctl enable memcached --now

Then, verify that your service is active and running:

sudo systemctl status memcached

In your output you will see:

Output
memcached.service - memcached daemon
Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor p>
Active: active (running) since Tue 2021-11-16 02:14:20 EST; 45s ago
Main PID: 116191 (memcached)
Tasks: 10 (limit: 11409)
Memory: 4.0M
CGroup: /system.slice/memcached.service
└─116191 /usr/bin/memcached -p 11211 -u memcached -m 64 -c 1024 -l 1>

It means that you have Memcached active and running on your AlmaLinux 8.

Let’s see how to configure it.

How To Configure Memcached on Almalinux 8

The Memcached configuration file is located at the /etc/sysconfig/memcached.

By default, it is set to listen only on localhost.

You don’t need to make any changes if the client connecting to the server is also running on the same host.

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

If you want to connect to the Memcached server over a private network, you need to edit the Memcached configuration file and set it to listen on the server’s private networking interface.

First, open the configuration file with your favorite text editor, here we use vi:

sudo vi /etc/sysconfig/memcached

Then, find the OPTIONS parameter and set it to bind to the specific interface only:

OPTIONS="-l 192.168.100.20"

When you are finished, save and close the file.

To apply the changes, restart Memcached on AlmaLinux 8:
sudo systemctl restart memcached

Now you need to open the Memcached port on your firewall. We assumed that you enabled firewalld from the requirements.

To do this, run the following command:

sudo firewall-cmd --add-port=11211/tcp --zone=public --permanent

Now reload the firewall to apply the new rules:

sudo firewall-cmd --reload

How To connect to the Memcached

For connecting to the Memcached server you need to use a language-specific client.

To use Memcached as a caching database for your PHP application, you need to install the Memcached PHP extensions with the following command:

sudo dnf install php-pecl-memcache

If you are running a Python application, you can install your preferred library with the pip command:

pip install pymemcache
pip install python-memcached

Conclusion

At this point, you learn to Set up and Configure Memcached on AlmaLinux 8. You can easily connect to it with a language-specific client.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!