How to Install Memcached on Debian 9

Posted 

3 min read

Memcached is a free and open-source high-performance in-memory key-value data store. It is mostly used to speed up applications by caching various objects from the results of database calls.

In this tutorial, we will walk you through installing and configuring Memcached on Debian 9.

Prerequisites

You’ll need to be logged in as a user with sudo access to be able to install packages on your Debian system.

Installing Memcached

Memcached packages are included in the default Debian 9 repositories. The installation is very easy, just follow the steps below:

  1. Update the packages list:

    sudo apt update
  2. Install Memcached by typing:

    sudo apt install memcached libmemcached-tools

    The libmemcached-tools package contains provides several command line tools for managing the Memcached server.

  3. Once the installation is completed, the Memcached service will start automatically. To check the status of the service, enter the following command:

    sudo systemctl status memcached

    The output will look like this:

    ● memcached.service - memcached daemon
      Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
      Active: active (running) since Wed 2019-06-05 21:00:57 GMT; 52s ago
        Docs: man:memcached(1)
    Main PID: 1371 (memcached)
      CGroup: /system.slice/memcached.service
              └─1371 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1

That’s it, you have successfully installed Memcached on your Debian 9 server.

Configuring Memcached

Memcached can be configured by editing the /etc/memcached.conf file. The default configuration settings are sufficient for most users.

By default, the Memcached service listens on localhost only. If the client connecting to the server is also running on the same host you don’t need to change the default configuration file.

Remote Access

If it is improperly configured, Memcached can be used to perform a distributed denial-of-service (DDoS) attack. To allow remote connections to the Memcached server, you need to configure your firewall and allow access to the Memcached UDP port 11211 only from trusted clients.

In the following example we are assuming that you want to connect to the Memcached server over a private network. The server IP is 192.168.100.20 and the client’s IP address is 192.168.100.30

If you are using UFW, run the following command to allow assess from the remote client IP address:

sudo ufw allow from 192.168.100.30 to any port 11211

If you are running plain old iptables run:

iptables -I INPUT -s 192.168.100.30 --dport 2112112 -j ACCEPT

Once the firewall is configured the next step is to edit the Memcached settings and set the Memcached service to listen on the server’s private networking interface:

To do so, open the memcached.conf configuration file:

sudo nano /etc/memcached.conf

Locate the line that begins with -l 127.0.0.1 and replace 127.0.0.1 with the server IP address 192.168.100.20.

/etc/memcached.conf
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 192.168.100.20

Restart the Memcached service for the changes to become active:

sudo systemctl restart memcached

Now you can connect to the Memcached server from the remote location.

Connecting to Memcached

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

PHP

To use Memcached as a caching database for your PHP application such as WordPress, Drupal or Magento, you need to install the php-memcached extension:

sudo apt install php-memcached

Python

There are several Python libraries for interacting with memcache. You can install your preferred library using pip:

pip install pymemcache
pip install python-memcached

Conclusion

You have learned how to install Memcached on your Debian 9 server. For more information on this topic consult Memcached Wiki.