Install and Configure Memcached on Ubuntu 20.04

In this article, we want to teach you Install and Configure Memcached on Ubuntu 20.04.

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.

How To Install and Configure Memcached on Ubuntu 20.04

Before you start to set up Memcached on Ubuntu 20.04, 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 Ubuntu 20.04.

Set up Memcached on Ubuntu 20.04

First, update your local package index with the following command:

sudo apt update

The Memcached packages are available in the default Ubuntu 20.04 repository. To install it run the following command:

sudo apt install memcached libmemcached-tools

When your installation is completed, Memcached will start automatically on your server. To verify your installation run the following command:

sudo systemctl status memcached

In your output you should see:

Output
memcached.service - memcached daemon
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor pre>
Active: active (running) since Wed 2021-11-17 14:14:44 CET; 2min 12s ago
Docs: man:memcached(1)
Main PID: 7297 (memcached)
Tasks: 10 (limit: 2282)
Memory: 1.8M
CGroup: /system.slice/memcached.service
└─7297 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 >

At this point, when you have your service is active and running on your server you can start using it.

Let’s see how to configure Memcached on Ubuntu 20.04.

How To Configure Memcached on Ubuntu 20.04

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

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 on Ubuntu 20.04 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/memcached.conf

Then, find the -l line and set it to bind to the specific interface only:

-l 192.168.100.20

When you are finished, save and close the file.

To apply the changes, restart Memcached on Ubuntu 20.04:
sudo systemctl restart memcached

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

To do this, run the following command:

#sudo ufw allow 11211
#sudo ufw allow from 192.168.100.20 to any port 11211

Now reload the firewall to apply the new rules:

sudo ufw reload

How To connect to the Memcached

For connecting to the Memcached server on Ubuntu 20.04 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 apt install php-memcached

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 Ubuntu 20.04. You can easily connect to it with a language-specific client.

Hope you enjoy it.

May this article about the Install and Configure Memcached on AlmaLinux 8 be useful for you.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular