Memcached is an open-source and distributed memory object caching system that holds the most frequently queried data in memory. This will reduce data load time as well as provide ease of access to the database. You can use Memcached to speed up dynamic web applications by alleviating database load. It is simple, easy to deploy and can be integrated with several programming languages including PHP, Python and more.

In this tutorial, I will show you how to install Memcached on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A root password is configured on the server.

 

Installing Memcached on Debian 11

By default, Memcached is included in the Debian 11 default repository. You can install it by running the following command:

apt-get install memcached libmemcached-tools -y

Once the Memcached is installed, start the Memcached service and enable it to start at system reboot:

systemctl start memcached
systemctl enable memcached

You can also check the status of the Memcached using the following command:

systemctl status memcached

You should get the following output:

? memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-11-01 10:12:25 UTC; 14s ago
       Docs: man:memcached(1)
   Main PID: 28398 (memcached)
      Tasks: 10 (limit: 4679)
     Memory: 3.1M
        CPU: 28ms
     CGroup: /system.slice/memcached.service
             ??28398 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Nov 01 10:12:25 debian11 systemd[1]: Started memcached daemon.

By default, Memcached listens on port 11211. You can check it using the following command:

ss -antpl | grep memcached

You will get the following output:

LISTEN 0      1024       127.0.0.1:11211      0.0.0.0:*    users:(("memcached",pid=28398,fd=26))

Configure Memcached

Memcached default configuration file is located at /etc/memcached.conf. You can edit it with the following command:

nano /etc/memcached.conf

You can change some default options as per your requirements:

-l 127.0.0.1
-U 0
-p 11211
-u memcache
-m 2000

Save and close the file then restart the Memcached service to apply the changes:

Save and close the file then restart the Memcached service to apply the changes:

systemctl restart memcached

Enable Memcached for PHP and Python Applications

In order to use Memcached with a PHP-based application, you will need to install the Memcached PHP library to your server. You can install it using the following command:

apt-get install php-memcached -y

If you want to enable Python and Perl support, run the following command:

apt-get install python3-pymemcache libcache-memcached-libmemcached-perl -y

Once all the libraries are installed, you can proceed to the next step.

 

Verify Memcached

Memcached is now installed and configured. Now, create a simple PHP script to verify the Memcached. First, install the Apache and PHP with the following command:

apt-get install apache2 php libapache2-mod-php -y

Once all the packages are installed, create a info.php file using the following command:

nano /var/www/html/info.php

Add the following lines:

<?php // Show all information, defaults to INFO_ALL phpinfo(); ?>

Save and close the file then open your web browser and access the URL http://your-server-ip/info.php. You should see that Memcached is enabled on the test page.

Enable Memcached on the firewall

If the Memached server is remote. Enable the port on the firewall:

sudo ufw allow 11211/tcp

Access Memcached CLI Interface

Memcached provides a command-line interface to interact with Memcached directly using the command line.

First, connect to Memcached using the following command:

telnet localhost 11211

Once you are connected, you will get the following output:

Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Now, check the status of the Memcached using the following command:

stats

You should see some important information such as uptime, number of items in the cache, and the number of client connections to the instance in the following output:

STAT pid 28398
STAT uptime 222
STAT time 1635761765
[...]

Conclusion

Congratulations! you have successfully installed Memcached on Debian 11. You can now integrate Memcached with PHP or Python-based applications and increase your website speed.