// SSD VPS

How to Install and Configure Memcached on a Linux VPS: Complete Guide — CLIQHOST

October 15, 2026 · by Alex M.

How to Install and Configure Memcached on a Linux VPS: Complete Guide — CLIQHOST

The performance of a web application depends heavily on how quickly it can access frequently requested data. Every database query consumes resources and adds latency. Memcached solves exactly this problem: it temporarily stores query results in RAM, allowing subsequent requests to be served in microseconds rather than milliseconds.

In this guide, you'll learn how to install, configure, and optimize Memcached on a SSD VPS running Ubuntu 22.04, including integration with PHP and WordPress.


What Is Memcached and Why Use It?

Memcached is an open-source, distributed in-memory caching system. It acts as an intermediate layer between your application and the database:

  • Drastic reduction in SQL queries — frequently used data is served directly from RAM
  • Extremely low latency — response times under 1 ms for cached data
  • Horizontal scalability — multiple Memcached instances can form a cluster
  • Broad compatibility — native support in PHP, Python, Ruby, Node.js, Java

If you're running WordPress, WooCommerce, Drupal, or any high-traffic application on a NVMe VPS, Memcached can dramatically reduce server load.


Prerequisites

Before you start, make sure you have:

  • A Linux VPS running Ubuntu 20.04 or 22.04 (guide tested on Ubuntu 22.04 LTS)
  • Root access or a user with sudo privileges
  • PHP 7.4+ installed (if integrating with PHP applications)
  • An active SSH connection

If you don't have a server yet, you can quickly order a SSD VPS from CLIQHOST with Ubuntu pre-installed.


Step 1: Update the System and Install Memcached

Before any installation, update system packages:

sudo apt update && sudo apt upgrade -y

Install Memcached and the development library needed for the PHP extension:

sudo apt install memcached libmemcached-tools -y

Check if the service started automatically:

sudo systemctl status memcached

You should see active (running). If not, start it manually:

sudo systemctl start memcached
sudo systemctl enable memcached

Step 2: Configuring the Main Memcached File

The main configuration file is located at /etc/memcached.conf. Open it with your preferred editor:

sudo nano /etc/memcached.conf

Here are the most important directives:

# Memory allocated (in MB)
-m 256

# Listening port (default 11211)
-p 11211

# User under which the process runs
-u memcache

# Listening address — IMPORTANT: localhost only for security
-l 127.0.0.1

# Maximum simultaneous connections
-c 1024

# Maximum size of a single cached object (in MB)
-I 5m

Practical Memory Allocation Recommendations

VPS RAM Memory for Memcached
1 GB 128–256 MB
2 GB 256–512 MB
4 GB 512 MB – 1 GB
8 GB+ 1–2 GB

On a NVMe VPS with dedicated resources, you can allocate more memory without impacting other services.

After making changes, restart Memcached:

sudo systemctl restart memcached

Step 3: Securing Your Memcached Instance

Memcached has no native authentication in standard versions, so network-level security is essential.

3.1 — Ensure It Listens Only on Localhost

Verify that the -l directive in /etc/memcached.conf is set to 127.0.0.1 (not 0.0.0.0).

3.2 — Block Port 11211 with UFW

sudo ufw deny 11211
sudo ufw allow OpenSSH
sudo ufw enable

3.3 — Verify Open Ports

sudo ss -tlnp | grep memcached

Correct output:

LISTEN  0  1024  127.0.0.1:11211  0.0.0.0:*

If Memcached's port is publicly exposed, your server can be exploited for UDP amplification DDoS attacks. CLIQHOST's server management service includes security audits for exactly these kinds of misconfigurations.


Step 4: Installing the PHP Extension for Memcached

To connect PHP applications to Memcached, you need the php-memcached extension:

sudo apt install php-memcached -y

For a specific PHP version (e.g., 8.1):

sudo apt install php8.1-memcached -y

Restart your web server:

# For Nginx + PHP-FPM
sudo systemctl restart php8.1-fpm
sudo systemctl restart nginx

# For Apache
sudo systemctl restart apache2

Verify the extension is active:

php -m | grep memcached

This should return: memcached


Step 5: Testing Memcached from the Command Line

The memcstat tool from the libmemcached-tools package lets you check server status:

memcstat --servers=127.0.0.1:11211

Alternatively, use telnet for a quick test:

telnet 127.0.0.1 11211

In the telnet console, type:

set test_key 0 60 5
hello
get test_key

Expected response:

STORED
VALUE test_key 0 5
hello
END

Memcached is working correctly! Exit telnet with quit.


Step 6: Integrating Memcached with WordPress

WordPress can use Memcached as an Object Cache backend, eliminating repeated queries to the MySQL/MariaDB database.

6.1 — Installing the Memcached Object Cache Plugin

Option 1: Dedicated "Memcached Object Cache" Plugin

Download the object-cache.php file from WordPress.org and place it in /wp-content/object-cache.php:

cd /var/www/html/wp-content/
wget https://raw.githubusercontent.com/tollmanz/wordpress-memcached-backend/master/object-cache.php

Add to wp-config.php before /* That's all, stop editing! */:

$memcached_servers = array(
    'default' => array(
        '127.0.0.1:11211'
    )
);

Option 2: W3 Total Cache

From the WordPress admin panel:

  1. Go to Plugins → Add New and search for W3 Total Cache
  2. Install and activate the plugin
  3. Go to Performance → General Settings
  4. Under Object Cache, select Memcached from the dropdown
  5. Save and test

If you're running WordPress on optimized WordPress hosting or a VPS, this configuration significantly reduces TTFB (Time To First Byte).


Step 7: Monitoring Memcached Statistics

Memcached exposes detailed statistics accessible in real time.

Statistics via Telnet

telnet 127.0.0.1 11211
stats

The most important metrics:

Metric Description
curr_items Number of objects stored in cache
get_hits Requests served from cache
get_misses Requests not found in cache (accessed from DB)
bytes Memory used (bytes)
limit_maxbytes Configured maximum memory limit
evictions Objects evicted from cache due to memory limits

Calculating Hit Rate

hit_rate = get_hits / (get_hits + get_misses) * 100

A hit rate above 90% indicates an effective configuration. If evictions keep rising, increase the -m value in your config.

Monitoring with phpMemcachedAdmin

You can install the phpMemcachedAdmin web interface for a graphical view:

cd /var/www/html
git clone https://github.com/elijaa/phpmemcachedadmin.git

Always configure restricted access (HTTP Basic Auth) before exposing it publicly.


Step 8: Advanced Optimizations

8.1 — Enabling the Binary Protocol

The Memcached binary protocol is more efficient than ASCII. Enable it by adding to /etc/memcached.conf:

-B binary

⚠️ Make sure your PHP client (the php-memcached extension) supports binary protocol — the php-memcache extension (without "d") supports only ASCII.

8.2 — Increasing Thread Count

On a VPS with multiple CPU cores, you can increase the number of worker threads:

-t 4

8.3 — Adjusting Slab Size

If you're storing large objects (complete HTML pages), adjust slab settings:

-f 1.25
-n 128

On a dedicated-resource VPS, these optimizations make a noticeable difference under heavy traffic scenarios.


Memcached vs. Redis: Which One to Choose?

A frequent question: when should you use Memcached vs. Redis?

Criteria Memcached Redis
Data types Strings only String, list, hash, set, sorted set
Disk persistence No Yes (optional)
Native cluster Yes (client-side) Yes (Redis Cluster)
Memory footprint Smaller Slightly larger
Best for Simple cache, sessions Complex cache, pub/sub, queues

For simple WordPress Object Cache, Memcached is sufficient and easier to configure. For more complex applications (message queues, pub/sub, persistent data), Redis is the better choice.


Common Troubleshooting

Memcached Won't Start

sudo journalctl -u memcached -n 50

Common cause: port 11211 already in use. Check:

sudo ss -tlnp | grep 11211

PHP Extension Not Loading

php --ini | grep memcached

Verify that the extension's .ini file exists in PHP's conf.d directory.

Low Hit Rate

  • Increase allocated memory
  • Check the TTL (time-to-live) of cached objects
  • Ensure cache keys are built consistently

Conclusion

Memcached is a simple, robust, and highly effective solution for speeding up PHP-based web applications. Installation on a Linux VPS takes under 10 minutes, and the performance gain can be substantial — especially for high-traffic sites with large databases.

If you want a solid foundation for your projects, choose a SSD VPS or a NVMe VPS from CLIQHOST, with guaranteed resources, technical support, and the option of full server management. Need help with the setup? Contact the CLIQHOST team — we're ready to assist.

SHARE
// what clients say

What Our Clients Say

Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.

★★★★★

"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."

AM
Andrei M.
eCommerce owner · Chișinău
★★★★★

"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."

EV
Elena V.
Web agency · Bălți
★★★★★

"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."

DC
Dmitri C.
SaaS founder · Chișinău