How to Install Memcached on Debian 11 / Debian 10
Memcached, the high-performance, distributed memory object caching system, is extremely useful in speeding up dynamic web applications by alleviating database load.
Memcached helps in minimizing the number of calls to database systems by caching data in memory. As a result, the subsequent requests for the same data can be served faster and thus improve web server performance.
Here, we will see how to install Memcached on Debian 11 / Debian 10.
Install Memcached on Debian
The Memcached package is available in the Debian OS repository. So, you do not need to set up external repositories to get the Memcached package.
First, update the repository index.
sudo apt update
Then, install the Memcached using the below command.
sudo apt install -y memcached
Once the installation is completed, the Memcached service will be up and running automatically. You can use the ps command or systemctl command to verify the status of the service.
sudo systemctl status memcached
ps -ef | grep -wi memcached
memcache 11047 1 0 03:40 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
You can manage the Memcached service with the help of its configuration file /etc/memcached.conf. There are four important configurations by which you can bring up the production-ready Memcached instance.
The -m sets the maximum memory you can use for object storage. The default memory size is 64 MB. However, setting a 256 MB cache will be recommended.
The -p sets the listening port for Memcached. The default port is 11211.
The -u sets the Memcached service to run as a specific user.
The -l sets the listening address for Memcached. By default, the Memcached listens on localhost only. So, you may need to change it to a network interface IP address to allow remote connections.
You can also specify multiple addresses separated by comma or by using -l multiple times. Here, I will configure Memcached to listen on 192.168.0.10.
Configuring the Memcached to listen on the IP address comes with security RISK because it allows anyone to access and may lead to a DDoS attack. So, you must configure UFW (Firewall) to allow connections only from the trusted sources.
Restart the Memcached process.
sudo systemctl restart memcached
Set up Firewall For Memcached
As I said earlier, to secure the Memcached service, you will need to configure the firewall to allow incoming connections from a trusted network to the Memcached port.
The below rule will allow connections from the 192.168.0.0/24 subnet (IP address ranging from 192.168.0.1 to 192.168.0.254).
sudo ufw allow from 192.168.0.0/24 to any port 11211 sudo ufw reload
There are many Memcached clients are available for programming languages like PHP, Python, Java, etc. However, to use Memcached as a caching system for your PHP applications such as WordPress, or Joomla, you will need to install the Memcached to extension for PHP.
sudo apt install -y php-memcached
Once you installed the PHP extension, restart your webserver to take the effect of the new extension.