How To Install Memcached on Debian 10

Memcached is an open-source high performance general-purpose distributed key-value. Memcached mostly used to cache dynamic web applications by cache the database query. In simple words, memcached serve the static version of the dynamic website, it’s API available for most of popular languages.

This how-to tutorial will cover how to install memcached on Debian 10, but it’s applicable to Ubuntu, Linux Mint and any Debian derivatives.

Update and Upgrade Pakcages

sudo apt update; sudo apt upgrade -y

Install memcached package

sudo apt install memcached -y

Check memcached status

sudo systemctl status memcached

memcached status services

memcached using port 11211 by default, to verify that port already opened use ss
check open port debian
since memcached bind to it’s safe from outside attacker by default.

Memcache Configuration File

To configure memcached, we need to change the configuration file on /etc/memcached.conf, for beginner I recommended to enable verbose mode by remove # in front of -vv
memcache enable verbose mode
a lot easier to troubleshooting when verbose mode used.

To allow memcached used by another server on local network (ex EC2, GCP, Linode, DigitalOcean), first, check your private IP using ip addr
ip address list ip network

my private IP address is, I’ll use that IP for memchaced listen to.

sudo sed -i 's/' /etc/memcached.conf

to allow other server to access memcached, whitelist the IP using iptables (ex from IP, change the private IP to public IP if you use public IP.

sudo iptables -A INPUT -s -p tcp --dport 11211 -j ACCEPT

Testing Memcached

Before we create a script to testing Memcached from PHP, first install Memcached extention for PHP

sudo apt install php-memcached -y

Let’s create simple PHP application memcached.php

// memcached.php
$memcached = new Memcached();
$memcached->addServer("", 11211);
$timestamp = date('Y-m-d H:i:s');
$getDate = $memcached->get("timestamp");
if ($getDate) {
    echo "Cached: " . $getDate . "\n";
} else {
    echo "Not cached yet: " . $timestamp . "\n";
    $memcached->set("timestamp", $timestamp);

that script will asking for timestamp value, if the value not found it’ll set the value to memcached server, then next time script asking for timestime it’s served by memcached.
run the memcached.php script 5 times using bash loop

for a in {1..5}; do php memcached.php ; done

php memcached testing

Leave a Reply