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
sudo apt update
sudo apt install memcachedStep 2: Memcached configuration
For a basic setup, check these settings in the Memcached configuration file, /etc/memcached.conf:
-druns Memcached in daemon mode.-mis the maximum memory Memcached may use (default: 64 MB).-pis the port Memcached listens on (default: 11211).-lis the IP address to listen on.0.0.0.0listens on all interfaces, but keep127.0.0.1unless 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.
echo "stats" | nc localhost 11211Step 4: Install the Memcached PHP extension
sudo apt install php-memcachedStep 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.
phpenmod memcached
sudo service apache2 restartCheck 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
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.
Related articles
- enabling the PHP SOAP extension on Debian: the same install, enable and restart steps for another extension
- browser caching with Expires headers: another caching layer for faster WordPress pages

