Installing and configuring Memcached

Step-by-step guide to high-performance caching.

Whether you're running a VPS, Web Hosting or a Dedicated Server, installing Memcached is a powerful way to boost your application's speed by storing frequently used data directly in RAM. It’s an essential tool for everything from small personal blogs to high-traffic Linux-based enterprise projects.

Fast Track: You can deploy Memcached with a single click during the server checkout process. Learn more in our One-Click-Apps documentation.

Think of Memcached as a massive hash table living on your server. It stores data with a defined lifespan (TTL), allowing for near-instant retrieval via unique keys. It is primarily used to cache web pages, database query results, and API objects that require low-latency access.

Installing Memcached

On Ubuntu / Debian

sudo apt update
sudo apt install memcached php-memcached -y

On CentOS / Rocky Linux / AlmaLinux

sudo yum install memcached php-pecl-memcached -y
# Or for newer versions:
sudo dnf install memcached php-pecl-memcached -y

Configuration

To fine-tune your instance, open the configuration file:

sudo nano /etc/memcached.conf

Recommended Settings:

# Default listening port
-p 11211

# Run as a specific user
-u memcached

# Maximum simultaneous connections
-c 1024

# Allocated memory for the cache (in megabytes)
-m 128

# Listen to localhost only (highly recommended for security)
-l 127.0.0.1

Save the file and restart the service to apply changes:

sudo systemctl restart memcached
sudo systemctl enable memcached

Verifying the setup

Check the service status:

sudo systemctl status memcached

You can also verify the connection and pull statistics directly:

echo "stats" | nc localhost 11211

Our products and services

Web HostingReliable hosting services for websites of any scale.
Order
VPSFlexible cloud infrastructure with full root access.
Order
Dedicated ServersBare metal servers for maximum performance.
Order

Enabling Memcached for PHP

Confirm that the PHP extension is active:

php -m | grep memcached

If it’s missing, install it using your package manager:

  • Ubuntu/Debian: sudo apt install php-memcached
  • CentOS/Rocky: sudo yum install php-pecl-memcached

After installation, restart your web server or PHP handler:

sudo systemctl restart php-fpm
# OR
sudo systemctl restart apache2

Best practices

  • Memory allocation: for small sites, 64–128 MB is usually plenty. For high-traffic applications, consider bumping the -m value to 512 MB or 1 GB.
  • Security first: never expose port 11211 to the public internet. Ensure Memcached is only accessible via localhost or a trusted private network.
  • Go local: for maximum security and slightly better performance on a single-server setup, consider using a Unix socket instead of a TCP port.

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us