How To Setup Memcached on cPanel-WHM

  WHM

Introduction

Memcached is a high-performance distributed memory caching system designed to accelerate dynamic web applications by reducing database load. It stores frequently accessed data in memory, resulting in faster response times and improved website performance.

This guide explains how to install and configure Memcached on a cPanel/WHM server running CentOS 6/7 or RHEL 6/7.

Important Note

Memcached packages in cPanel EasyApache 4 (EA4) are available through the Experimental Repository. Since experimental packages may not be suitable for production shared-hosting environments, proceed with caution.

Additionally, the Memcache and Memcached PHP extensions provided by cPanel are only available for PHP 5.4, 5.5, 5.6, 7.0, and 7.1. These extensions are not available through the EA4 experimental repository for PHP 7.2 and newer versions.

Prerequisites

Before proceeding, ensure you have:

  • A cPanel/WHM server running CentOS 6/7 or RHEL 6/7
  • Root SSH access to the server
  • EasyApache 4 installed

Step 1: Enable the EasyApache 4 Experimental Repository

Memcached packages are distributed through the EA4 Experimental Repository.

Install the repository using:

yum install ea4-experimental -y

Step 2: Install the Memcached Service

Install the Memcached daemon package:

yum install ea-memcached -y

Verify the installation:

rpm -qa | grep memcached

Step 3: Install Memcache and Memcached PHP Extensions

Install the required PHP extensions for the supported PHP versions:

yum install -y \
ea-php54-php-memcache ea-php54-php-memcached \
ea-php55-php-memcache ea-php55-php-memcached \
ea-php56-php-memcache ea-php56-php-memcached \
ea-php70-php-memcache ea-php70-php-memcached \
ea-php71-php-memcache ea-php71-php-memcached

To verify installation:

yum list installed | grep memcache

Step 4: Configure Memcached

The Memcached configuration file is located at:

/etc/sysconfig/memcached

Open the file:

nano /etc/sysconfig/memcached

Modify the configuration according to your server requirements:

PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="256"
OPTIONS="-l 127.0.0.1,::1"

Configuration Parameters

ParameterDescription
PORTMemcached listening port
USERService user account
MAXCONNMaximum simultaneous client connections
CACHESIZEMemory allocation (MB) used for caching
OPTIONSRestricts access to localhost only

Recommended Values

Server SizeCACHESIZE
Small VPS64 MB
Medium VPS128 MB
Large VPS256 MB
Dedicated Server512 MB+

Default Values

MAXCONN="1024"
CACHESIZE="64"

Save the file and exit.

Step 5: Configure PHP Extensions

Edit both memcache.ini and memcached.ini files for the PHP versions installed on your server.

PHP 7.1

nano /opt/cpanel/ea-php71/root/etc/php.d/memcache.ini
nano /opt/cpanel/ea-php71/root/etc/php.d/memcached.ini

PHP 7.0

nano /opt/cpanel/ea-php70/root/etc/php.d/memcache.ini
nano /opt/cpanel/ea-php70/root/etc/php.d/memcached.ini

PHP 5.6

nano /opt/cpanel/ea-php56/root/etc/php.d/memcache.ini
nano /opt/cpanel/ea-php56/root/etc/php.d/memcached.ini

PHP 5.5

nano /opt/cpanel/ea-php55/root/etc/php.d/memcache.ini
nano /opt/cpanel/ea-php55/root/etc/php.d/memcached.ini

PHP 5.4

nano /opt/cpanel/ea-php54/root/etc/php.d/memcache.ini
nano /opt/cpanel/ea-php54/root/etc/php.d/memcached.ini

Uncomment or add the following settings:

session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"

These settings allow PHP session data to be stored in Memcached instead of local disk storage, improving session performance.

Step 6: Start and Enable Memcached

Restart the Memcached service:

service memcached restart

Enable the service to start automatically after reboot:

chkconfig memcached on

Restart Apache:

service httpd restart

Step 7: Verify Memcached Status

Check whether Memcached is running:

service memcached status

Verify that the service is listening on port 11211:

netstat -plunt | grep 11211

Expected output:

tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN

Verify PHP extension loading:

php -m | grep memcache

Expected output:

memcache
memcached

Troubleshooting

Memcached Service Fails to Start

Check logs:

journalctl -u memcached

or

tail -f /var/log/messages

PHP Extension Not Loading

Verify the extension files exist:

php -i | grep memcache

Restart Apache after making configuration changes:

service httpd restart

Connection Refused

Verify that Memcached is listening:

netstat -plunt | grep 11211

Check firewall settings if remote access is required.

Conclusion

Memcached is an effective caching solution that can significantly improve website performance by reducing database queries and accelerating PHP session handling. By integrating Memcached with cPanel/WHM and configuring the appropriate PHP extensions, administrators can optimize application performance and reduce server load.

For production environments, always monitor memory utilization and adjust the CACHESIZE and MAXCONN parameters based on your server’s available resources and application requirements.

LEAVE A COMMENT