Linux VPS

Install and secure Memcached on Ubuntu or AlmaLinux

Install the Memcached object cache and its PHP extension on Ubuntu or AlmaLinux, bind it to localhost, size its memory, connect WordPress or apps and verify it works.

IntermediateLinux VPS2 min read

Memcached is a simple, very fast in-memory key-value cache. Applications store the results of expensive database queries or calculations in it, so repeat requests skip the database. It is an alternative to Redis when you only need a volatile cache, without persistence or advanced data types.

Before you start

  • An Ubuntu 22.04/24.04 or AlmaLinux 8/9 VPS with sudo access.
  • PHP installed if a PHP application will use it.
  • Choosing between them? Redis adds persistence and data structures; see install Redis.

Install Memcached

Ubuntu:

apt update && apt -y install memcached libmemcached-tools

AlmaLinux:

dnf -y install memcached libmemcached

Enable and start it:

systemctl enable --now memcached

Secure and size it

Memcached has no authentication by default, so it must never be reachable from the internet. Open servers have been abused for huge amplification DDoS attacks.

  • Ubuntu, edit /etc/memcached.conf:
    -l 127.0.0.1
    -U 0
    -m 256
  • AlmaLinux, edit /etc/sysconfig/memcached:
    CACHESIZE="256"
    OPTIONS="-l 127.0.0.1 -U 0"

-l 127.0.0.1 listens only locally, -U 0 disables UDP, and -m/CACHESIZE sets the memory in MB. Restart with systemctl restart memcached. Keep port 11211 closed in the firewall.

Install the PHP extension

Ubuntu: apt -y install php-memcached (or php8.3-memcached for a specific version). AlmaLinux with Remi: dnf -y install php-pecl-memcached. Then restart PHP-FPM.

Connect your application

  • WordPress: use an object cache plugin that supports Memcached, such as LiteSpeed Cache (Object Cache tab, method Memcached, host localhost, port 11211).
  • Laravel: set CACHE_STORE=memcached in .env.
  • Custom PHP: $m = new Memcached(); $m->addServer('127.0.0.1', 11211);

Verify

memcstat --servers=127.0.0.1

Watch get_hits and get_misses grow as the site is used. Confirm it is local only with ss -lntup | grep 11211, which should show 127.0.0.1:11211.

Troubleshooting

  • Class Memcached not found: the PHP extension is not installed for the PHP version your site uses.
  • Low hit rate: the cache is too small and evicts items; raise the memory size.
  • Stale data after deploys: flush the cache with echo flush_all | nc -q1 127.0.0.1 11211.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.