How to Install Memcached on Debian

Quick Summary

  • Install the memcached package with apt, review its settings in /etc/memcached.conf, then add the php-memcached extension and restart Apache.
  • Keep Memcached listening on 127.0.0.1 unless another server needs it, since a port open to the internet can be abused.
  • WordPress won't use Memcached on its own; it needs an object-cache.php drop-in in wp-content, which caching plugins can add.

Memcached is an in-memory key-value cache that PHP applications such as WordPress can use to keep data between requests. To install it on Debian, install the memcached package with apt, check its settings in /etc/memcached.conf, then add the php-memcached extension and restart Apache.

Log in to your VPS over SSH as a user with sudo rights before you start.

Step 1: Install Memcached on Debian

Shell
sudo apt update
sudo apt install memcached

Step 2: Memcached configuration

For a basic setup, check these settings in the Memcached configuration file, /etc/memcached.conf:

  • -d runs Memcached in daemon mode.
  • -m is the maximum memory Memcached may use (default: 64 MB).
  • -p is the port Memcached listens on (default: 11211).
  • -l is the IP address to listen on. 0.0.0.0 listens on all interfaces, but keep 127.0.0.1 unless another server needs to connect: a Memcached port that is open to the internet can be abused.

After changing the file, restart the service with sudo systemctl restart memcached.

Step 3: Test the Memcached setup (optional)

This sends the stats command to Memcached on port 11211 and prints its statistics. It uses netcat (nc); install it with apt if the command isn’t found.

PHP
echo "stats" | nc localhost 11211

Step 4: Install the Memcached PHP extension

Shell
sudo apt install php-memcached

Step 5: Restart Apache

phpenmod enables the extension (run it with sudo if you aren’t root), and the restart makes Apache load it. If you use Nginx with PHP-FPM, restart the PHP-FPM service instead.

Shell
phpenmod memcached
sudo service apache2 restart

Check that PHP loads the extension

Create an info.php file in your site’s root folder with the following code, open it in your browser and look for the memcached section. Delete the file afterwards, because it shows details about your server.

PHP
<?php
  phpinfo();
?>

Memcached is now running, but WordPress doesn’t use it on its own. To make it WordPress’s persistent object cache, you also need an object-cache.php drop-in in wp-content, which caching plugins with Memcached support can add for you.

Main article:
How to Install Memcached on Debian

If you’d rather have caching set up and tuned for you, see my WordPress SEO and performance services.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More