Memcached is a high performance free and opensource in-memory key-value store used as a caching system. It’s mainly used for speeding up database-driven sites and web applications by caching data in RAM. In so doing, it significantly reduces the frequency that an eternal source of data is read.
Memcached is simple and easy to deploy and its API is widely available for a wide range of popular programming languages such as Python.
This guide walks you through the installation of Memcached on Debian 10, codenamed Debian Buster and Debian 9, codenamed Stretch.
On this page
Install Memcached on Debian
Memcached packages are already included in the Debian repository, and as such, we are going to install Memcached using the APT package manager.
But first, update system packages as shown:
$ sudo apt update
Thereafter, install Memcached by invoking the command:
$ sudo apt install memcached libmemcached-tools
The libmemcached-tools package is a C & C++ library that provides multiple command-line utilities that you can use for interacting and managing the Memcached server.
Once the installed, Memcached service will automatically start and you can verify this by running the command:
$ sudo systemctl status memcached
By default, Memcached listens on port 11211 and you can verify this using the netstat command as shown:
$ sudo netstat -pnltu
Configure Memcached on Debian
To configure Memcached, you need to configure the /etc/memcached.conf
file. For the most part, the default settings will work just fine for a majority of users.
Without any configuration, Memcached listens on the localhost only. If you are connecting to the Memcached server from the server itself, no configuration is needed.
To allow remote connections to the server, some additional configuration is required. We need to modify the firewall to allow access to UDP port 11211 which Memcached listens to by default.
Let’s assume that the Memcached server IP address is 10.128.0.46 and the client’s IP address is 10.128.0.45. To allows the client machine access to the Memcached server, run the command.
$ sudo ufw allow from 10.128.0.45 to any port 11211
Next, reload the firewall for the changes to persist.
$ sudo ufw reload
Thereafter, head over to the memcached.conf
configuration file.
$ sudo vim /etc/memcached.conf
Be sure to locate the line that starts with -l 127.0.0.1
.
Replace it with the server’s IP, which in this case is 10.128.0.46 as shown:
Now, restart Memcached for the changes to come into effect.
$ sudo systemctl restart memcached
Enable Memcached for PHP and Python Applications
If you intend to use Memcached as a caching database for PHP applications such as Drupal or WordPress, the php-memcached extension is required.
To install it, run the command:
$ sudo apt install php-memcached
For Python applications, install the following Python libraries using pip. If pip is not installed, you can install it using the command:
$ sudo apt install python3-pip
Then install the libraries as shown.
$ pip3 install pymemcache $ pip3 install python-memcached
Conclusion
We have come to the end of this guide. It’s our hope that you can now install Memcached on your Debian 10 instance without a hitch. Your feedback is welcome.