How to Install and Configure Memcached on RHEL 7
Memcached is a high-performance, distributed memory object caching system used to speed up dynamic web applications by reducing database load. It stores frequently requested data — such as database query results, session data, and rendered page fragments — in RAM, allowing subsequent requests to be served in microseconds instead of querying the database each time. On RHEL 7, Memcached is available directly from the base repositories, making installation straightforward. This guide walks through installing Memcached, tuning its configuration, verifying the service, installing the PHP extension, and integrating it with WordPress as a practical use case.
Prerequisites
- A running RHEL 7 server with root or sudo access
- Active Red Hat subscription or access to a configured yum repository
- Basic familiarity with the command line and systemctl
- PHP installed if you plan to use the PHP Memcached extension
- WordPress installed if following the WordPress integration section
Step 1: Install Memcached
Memcached is available in the default RHEL 7 repositories. Install it along with the libmemcached library, which is required by various client tools and PHP extensions:
sudo yum install -y memcached libmemcached
Confirm the installed version:
memcached --version
You should see output similar to:
memcached 1.4.15
Step 2: Configure Memcached
The primary configuration file for Memcached on RHEL 7 is /etc/sysconfig/memcached. This file controls the port, user, maximum connections, and the size of the memory cache. Open it in your editor:
sudo vi /etc/sysconfig/memcached
The default contents look like this. Review and adjust each variable to match your server’s available RAM and expected load:
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
Here is a description of each setting:
- PORT — The TCP/UDP port Memcached listens on. The default is 11211. Do not expose this port publicly.
- USER — The system user the daemon runs as. The dedicated
memcacheduser is created automatically during installation. - MAXCONN — The maximum number of simultaneous connections. For a busy web application, increase this to 2048 or higher depending on concurrency requirements.
- CACHESIZE — The amount of RAM in megabytes to allocate for the cache. A common starting point is 256 MB on a server with 2 GB or more of RAM.
- OPTIONS — Additional command-line flags passed to the memcached daemon. Use
-l 127.0.0.1here to explicitly bind to localhost only, preventing accidental network exposure.
A production-ready configuration example for a server with 4 GB RAM might look like this:
PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="512"
OPTIONS="-l 127.0.0.1"
Save the file and exit the editor.
Step 3: Enable and Start the Memcached Service
Use systemctl to enable Memcached at boot and start the service immediately:
sudo systemctl enable memcached
sudo systemctl start memcached
Check the service status to confirm it is running without errors:
sudo systemctl status memcached
Expected output:
● memcached.service - Memcached
Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2026-05-17 10:00:00 UTC; 5s ago
Main PID: 12345 (memcached)
CGroup: /system.slice/memcached.service
└─12345 /usr/bin/memcached -p 11211 -u memcached -m 512 -c 2048 -l 127.0.0.1
Step 4: Test Memcached with Telnet
Before integrating Memcached with any application, verify it responds correctly using the telnet client. Install telnet if it is not present:
sudo yum install -y telnet
Connect to the Memcached port on localhost:
telnet localhost 11211
Once connected, store a test key and retrieve it:
set testkey 0 60 5
hello
STORED
get testkey
VALUE testkey 0 5
hello
END
The set command stores the value hello under testkey with no flags, a 60-second expiry, and a value length of 5 bytes. The get command retrieves it. Type quit to close the connection. You can also retrieve server statistics at any time:
stats
END
Step 5: Install the PHP Memcached Extension
To use Memcached from PHP, you need the php-pecl-memcached package. Note the distinction: php-pecl-memcached uses the libmemcached library and supports binary protocol and SASL authentication, while the older php-pecl-memcache (no ‘d’) is a simpler legacy extension. Install the preferred extension:
sudo yum install -y php-pecl-memcached
If your PHP version is managed through a Software Collection or Remi repository, adjust the package name accordingly (e.g., php72-php-pecl-memcached). After installation, restart your web server to load the new extension:
sudo systemctl restart httpd
Verify the extension is loaded:
php -m | grep memcached
You should see memcached in the output. You can also check via a PHP info page or with:
php -r "echo class_exists('Memcached') ? 'OK' : 'NOT LOADED';"
Step 6: Configure WordPress to Use Memcached
WordPress supports Memcached as an object cache backend through a drop-in file. The most widely used solution is the W3 Total Cache plugin or the standalone object-cache.php drop-in from the memcached-object-cache project.
The simplest approach is to download the drop-in directly. Copy or download object-cache.php into your WordPress wp-content directory:
sudo curl -o /var/www/html/wp-content/object-cache.php
https://raw.githubusercontent.com/tollmanz/wordpress-memcached-backend/master/object-cache.php
Next, define the Memcached server address in wp-config.php. Add the following lines before the /* That's all, stop editing! */ comment:
global $memcached_servers;
$memcached_servers = array(
'default' => array(
'127.0.0.1:11211',
),
);
WordPress will now use Memcached for its object cache, storing transients, query results, and other cached data in RAM instead of the database. This can dramatically reduce the number of database queries per page load, especially on sites with heavy plugin use or large post counts.
Step 7: Firewall Configuration — Keep Memcached Private
Memcached has no built-in authentication by default. It is critical that the Memcached port is never accessible from the public internet. If you configured Memcached to bind to 127.0.0.1 in Step 2, the firewall is an additional layer of defence. Confirm the port is not open externally:
sudo firewall-cmd --list-all
Port 11211 should not appear in the list. If you need to allow Memcached access between two internal servers (for example, a web server reaching a dedicated Memcached server), use a rich rule scoped to the internal IP only:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="11211" accept'
sudo firewall-cmd --reload
Never open port 11211 to 0.0.0.0/0. Publicly exposed Memcached servers have been used in large-scale UDP reflection DDoS attacks.
Step 8: Basic Performance Monitoring
The memcached-tool utility provides a convenient way to inspect cache statistics. It is part of the Memcached source distribution but also available as a standalone script. Use the built-in stats interface via telnet or netcat:
echo "stats" | nc 127.0.0.1 11211
Key metrics to monitor:
- curr_items — Number of items currently stored in the cache.
- get_hits / get_misses — Cache hit ratio. A healthy ratio is typically above 90%.
- evictions — Number of items evicted to make room for new ones. High evictions indicate the cache size is too small.
- bytes — Current memory usage in bytes.
- limit_maxbytes — Configured maximum memory limit.
echo "stats" | nc 127.0.0.1 11211 | grep -E "curr_items|get_hits|get_misses|evictions|bytes"
If you see a high eviction count, increase CACHESIZE in /etc/sysconfig/memcached and restart the service.
Conclusion
You have successfully installed and configured Memcached on RHEL 7, including tuning its core parameters, validating the daemon with telnet, installing the PHP extension, and integrating it with WordPress. The most important operational rule to remember is to keep Memcached bound to localhost or a private network interface and never expose it to the public internet. With a well-sized cache and a healthy hit ratio, Memcached can reduce database queries by 80% or more on a typical WordPress site, resulting in significantly faster page load times and lower server CPU usage under traffic peaks.