How to Install Memcached on Debian 10

One of the uses of a cache is to speed up common queries on the data store. So, if you have many web applications, it will be noticed in the performance of your server. For example, applications can store results in Memcached and thus process more and better data. That’s why we have prepared this post for you to learn how to install Memcached on Debian 10.

Introducing to Memcached

According to the Memcached website:

Free & open-source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is used to cache data or objects in RAM, thus reducing the need to access an external data source (such as a database or an API).

The direct consequences of a Memcached implementation are the optimization of web applications and therefore faster load times. That is why it is a very popular tool for sites with high traffic.

Install Memcached on Debian 10

Memcached is available in the official Debian 10 repositories so installation is not complicated.

So, open a terminal from the system menu or connect via SSH and update Debian

sudo apt update
sudo apt upgrade

After that, you can install the memcached libmemcached-tools packages using APT

sudo apt install memcached libmemcached-tools

Memcached is a system service so you can start, enable or stop it with the command systemctl.

sudo systemctl start memcached
sudo systemctl enable memcached

Also, you can check the status of the service:

sudo systemctl status memcached

Sample Output:

● memcached.service - memcached daemon
   Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-04-01 00:22:05 CEST; 7s ago
     Docs: man:memcached(1)
 Main PID: 1627 (memcached)
    Tasks: 10 (limit: 2296)
   Memory: 3.4M
   CGroup: /system.slice/memcached.service
           └─1627 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Apr 01 00:22:05 atechtown systemd[1]: Started memcached daemon.
Apr 01 00:22:05 atechtown systemd[1]: /lib/systemd/system/memcached.service:13: PIDFile= references path below legacy directory /var/run/, updating /var/run/memcached/

This way, Memcached is installed and working correctly. However, for it to be used by applications it is necessary to modify the configuration file and set the host.

Thus the Memcached configuration file /etc/memcached.conf is set to /etc/memcached.conf.

Before editing the file it is a good idea to make a backup of it.

sudo nano /etc/memcached.conf /etc/memcached.conf.bak
sudo nano /etc/memcached.conf

Locate the line:

-l 127.0.0.1

And replace it with the following:

-l [your-server_IP_address]

Save the changes and close the editor.

To apply the changes you need to restart the Memcached service.

sudo systemctl restart memcached

And you can check the status of the service to see if it is running

sudo systemctl status memcached

With this memcached will be working properly.

Check if Memcached with PHP

One way to check if Memcached is running is with PHP. To do this, install Apache or nginx and make sure it is working. In this case, I will opt for Apache as it is the fastest and easiest to configure.

So, to install Apache and PHP along with the Memcached plugin, run:

sudo apt install apache2 php libapache2-mod-php php-memcached php-cli

To make Memcached and PHP work together with Apache, it is necessary to restart the services

sudo systemctl restart apache2
sudo systemctl restart memcached

To test, create a phpinfo file

sudo nano /var/www/html/phpinfo.php

And add the following content

<?php
phpinfo();
?>

Save the changes and close the editor. Now if you open it with a web browser, you will see the Memcached section.

Memcached working on Debian 10
Memcached working on Debian 10

So, this ensures that web applications made on this server will be configured to use Memcached.

Conclusion

When the performance of a web system becomes critical and the response time can be affected by the numerous simultaneous visits, it is necessary to use professional tools to help us with the task. In this case, Memcached is one of them and the best of all is that it is so efficient that even its installation is simple.