2026年11月14日

2026年11月14日

Install and Configure Memcached on Ubuntu/RHEL for PHP and WordPress

This article is a localized edition of the Japanese original. Read the Japanese original

Memcached is a cache server that stores data in memory as key-value pairs. It is used to keep things such as database query results around temporarily so that the same data does not have to be rebuilt again and again. Because it stores data only in memory, its contents are lost when it restarts. Do not use it to store data you cannot afford to lose.

This article explains installation and basic configuration on Ubuntu and RHEL-family systems (AlmaLinux and others), how to verify it, and what to watch for when using it from PHP and WordPress (as of November 2026; checked with memcached 1.6.24 on Ubuntu 24.04 and memcached 1.6.9 on AlmaLinux 9).

Step 1: Install and start it

Ubuntu / Debian

sudo apt update
# memcached itself plus the tools for checking it (memcstat and others)
sudo apt install -y memcached libmemcached-tools

sudo systemctl enable --now memcached
systemctl status memcached

libmemcached-dev contains development files used when you build things like PHP extensions yourself. You do not need it just to run the server.

RHEL / AlmaLinux / Rocky Linux

sudo dnf install -y memcached
sudo systemctl enable --now memcached
systemctl status memcached

Step 2: Check and change the configuration

Where the configuration file lives and how it is written differ by distribution.

Ubuntu / Debian: /etc/memcached.conf

These are the main settings right after the package is installed.

Setting Meaning
-m 64 Upper limit of memory used to store items (MB)
-p 11211 TCP port to listen on
-u memcache User to run as
-l 127.0.0.1 and -l ::1 Addresses to listen on (accepts connections only from the machine itself)
logfile /var/log/memcached.log Where logs are written
# -c 1024 Maximum simultaneous connections (left commented out = the default of 1024)

For example, to raise the memory limit to 256 MB, edit the -m 64 line and restart.

sudo nano /etc/memcached.conf
# Change -m 64 to -m 256 and save

sudo systemctl restart memcached

RHEL family: /etc/sysconfig/memcached

On RHEL-family systems, settings are made with variables.

# /etc/sysconfig/memcached (AlmaLinux 9 defaults)
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1"

CACHESIZE is the memory limit (MB) and MAXCONN is the maximum number of simultaneous connections. After changing them, apply the change with sudo systemctl restart memcached.

Deciding how much memory to give it

-m (CACHESIZE) is an upper limit; when it is reached, the oldest data is evicted. If the same server also runs the web server, PHP and the database, decide within what remains after subtracting the memory they use. Making it so large that the whole server runs short of memory makes things slower, not faster.

Step 3: Do not expose it to the outside

By default, Memcached has no authentication. Anyone who can reach port 11211 can read and write to it.

  • Keep the listen address at 127.0.0.1 (and ::1): As in the default settings above, it then accepts connections only from the same server.
  • If other servers need to use it: Set -l to an internal network address, and use a firewall to allow connections only from your web servers. Do not open port 11211 to the internet.
  • Leave UDP disabled: In memcached 1.6, the UDP port is disabled by default (-U 0). A configuration that uses UDP can be abused for amplification attacks if it is open to the outside.

Step 4: Verify that it works

# Show statistics with memcstat from libmemcached-tools (this is its name on Debian / Ubuntu)
memcstat --servers=127.0.0.1

# Or send the stats command with nc
printf "stats\nquit\n" | nc 127.0.0.1 11211 | grep -E "version|curr_items|get_hits|limit_maxbytes"

The memstat command mentioned in older articles is named memcstat in the Debian / Ubuntu packages. limit_maxbytes is the memory limit set with -m (in bytes), and get_hits is the number of times a value was served from the cache.

On Ubuntu, logs go to /var/log/memcached.log. However, detailed output appears only when you enable -v (or -vv) in the configuration file. If it fails to start, check journalctl -u memcached first.

Step 5: Use it from PHP

PHP has two extensions with similar names.

Extension Class name Ubuntu 24.04 package
memcached (uses libmemcached) Memcached php8.3-memcached (php-memcached)
memcache (a separate, older extension) Memcache php-memcache

Check which one the plugin or library you use requires before you install it. This example uses the memcached extension.

sudo apt install -y php8.3-memcached
# If you use PHP-FPM
sudo systemctl restart php8.3-fpm
# With Apache mod_php, run sudo systemctl restart apache2 instead

Use the following script to confirm that reading and writing work.

<?php
// memcached-test.php: check that PHP can write to and read from Memcached
$m = new Memcached();
$m->addServer( '127.0.0.1', 11211 );

$m->set( 'hello', 'world', 60 ); // expires in 60 seconds
var_dump( $m->get( 'hello' ) );   // success if it prints string(5) "world"
var_dump( $m->getResultMessage() );
php memcached-test.php

When you are done checking, do not leave this file somewhere it can be reached over the web.

Notes for using it with WordPress

When wp-content/object-cache.php (the object cache drop-in) exists, WordPress stores its cache externally through that file. Installing Memcached alone does not make WordPress use it, so you need a plugin that provides the drop-in.

  • W3 Total Cache: Its plugin description lists Memcached as one of its caching methods.
  • Memcached Object Cache (memcached on wordpress.org): A drop-in that uses the memcache extension (PECL memcache). As of November 2026 it was last updated in 2022 and has not been tested with recent WordPress versions, so verify it in a test environment before using it. Its description page tells you to set WP_CACHE_KEY_SALT in wp-config.php to a value that is different for each installation.

If several sites share one Memcached instance, give each site its own key prefix (salt) so their keys do not collide. Today, object caches that use Redis have a wider choice of supporting plugins, so for a comparison see also where transients and the object cache are stored.

Related articles

Questions about WordPress? The publisher of this site is based in Japan and replies in Japanese. Contact (Japanese)

お気軽にご相談ください

お見積りへ お問い合わせへ
▲