This tutorial will guide you on how to setup and configure memcached on cPanel-WHM.
Setting up memcached and memcache PHP extensions on cPanel-WHM is very easy.
Simply add EasyApache4 (EA4) Experimental Repository in order to install memcached. Unfortunately, both memcached and memcache RPMs are still in an experimental repo on cPanel-WHM.
So it might not be suitable for a shared hosting environment. Additionally, the memcached and memcache php extensions in cPanel-WHM are only available for PHP 5.4, PHP 5.5., PHP 5.6, PHP 7.0, and PHP 7.1.
So, both memcached and memcache since 2016 are not yet available for cPanel-WHM PHP 7.2, PHP 7.3 and upcoming PHP 7.4.
However, if you use cPanel-WHM on your own cloud server or dedicated server, you might be interested in trying to use memcached as a database caching for your site.
Simply follow this tutorial to install and setup memcached database cache for cPanel-WHM. This guide should work well on both RHEL 6/7 and CentOS 6/7.
1. Let’s Add EasyApache4 Experimental Repository
First of all, we need to add EA4 experimental repo to our CentOS 7 system. We’ve tested this many times on CentOS 7 system and that’s no problem.
$ yum install ea4-experimental -y
2. Install Memcached Daemon
To install memcached daemon on your system simply run the following command:
$ yum install ea-memcached -y
3. Install Memcached and Memcache PHP Extensions
Now, let’s install Memcached and Memcache PHP Extensions for PHP 5.4 – PHP 7.1.
Since PHP 5.4 – 7.0 has reached its end of life (eol), so we strongly recommend you to use memcached and memcache with PHP 7.1.
$ yum install 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 -y
4. Configuring Memcached
We can customize memcached configuration in orde to get best performance for your cPanel-WHM server.
CACHESIZE
is a maximum amount of memory allocation will be consumed by memcached for object storage. While MAXCONN
is the maximum number of simultaneous connections allowed by memcached.
For more details, you can read our previous Memcached tutorial here.
$ nano /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="256"
OPTIONS="-l 127.0.0.1,::1"
NOTE:
* Default Value for MAXCONN is 1024 and CACHESIZE 64
5. Edit .ini Configuration File for Memcache and Memcached
Finally, we must edit both memcache.ini and memcached.ini in order to enable this PHP extensions. Please note we should uncomment session.save_handler
and session.save_path
.
session.save_handler = "memcache"
session.save_path = "tcp://localhost:11211"
Depending on the PHP version you are using, here are the locations of memcache.ini and memcached.ini files. You can use a nano editor or browse via WinSCP to edit this files.
$ nano /opt/cpanel/ea-php71/root/etc/php.d/memcache.ini
$ nano /opt/cpanel/ea-php71/root/etc/php.d/memcached.ini
$ nano /opt/cpanel/ea-php70/root/etc/php.d/memcache.ini
$ nano /opt/cpanel/ea-php70/root/etc/php.d/memcached.ini
$ nano /opt/cpanel/ea-php56/root/etc/php.d/memcache.ini
$ nano /opt/cpanel/ea-php56/root/etc/php.d/memcached.ini
$ nano /opt/cpanel/ea-php55/root/etc/php.d/memcache.ini
$ nano /opt/cpanel/ea-php55/root/etc/php.d/memcached.ini
$ nano /opt/cpanel/ea-php54/root/etc/php.d/memcache.ini
$ nano /opt/cpanel/ea-php54/root/etc/php.d/memcached.ini
6. Restart Services
$ service memcached restart
$ chkconfig memcached on
$ service httpd restart
$ service nginx restart
if you currently use LiteSpeed Web Server on cPanel-WHM, run the following command to restart the LSWS service.
$ /usr/local/lsws/bin/lswsctrl restart
We are a bunch of people who are still continue to learn Linux servers. Only high passion keeps pushing us to learn everything.