User Tools

Site Tools


vps-hosting:install-memcached-on-centos-7-with-cpanel

Memcached explained

Difficulty
Medium
Medium

Memcached is a memory object caching system, which is used for speeding up a good majority of current web applications. Most applications that you can install as a web front or back-end, have support for memcaching to help distribute and relieve load from your server. Essentially, memcached helps make better use of your memory, regardless of which processes are using memory.

Installing memcached

Before you can install memcached, you need to install the binaries responsible for the process.

yum -y install libmemcached-devel memcached

Note: This may install several dependencies that are needed for memcached to work correctly.

Once the install is finished, you can start the memcached service:

systemctl start memcached

To have memcached start on reboot, and persist until manually turned off, you should enable the service in systemd by typing the following command:

systemctl enable memcached

Your ConfigServer Security and Firewall (CSF) will likely notify you of an unknown process once it's been running for awhile. Since we know what this process is, and dont need to be notified by the firewall in the future, you need to add an ignore line in the Firewall's process ignore file, like so:

echo "exe:/usr/bin/memcached" >> /etc/csf/csf.pignore

After this, restart CSF to register the changes:

csf -ra

Now you'll need to modify the default configuration file for memcached, which is located at /etc/sysconfig/memcached. You can modify this file with your favorite editor; I'll be using vim (nano is another popular commandline editor).

vi /etc/sysconfig/memcached

You should see the following:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

We need to make a few adjustments, notably to the CACHESIZE variable and the OPTIONS variable. On smaller VPS servers, CACHESIZE shouldn't exceed 1G. On larger servers (Dedicated, Cloud-3, MVPS-4, VPS-7, SSD-5), you can safely increase this value to 3G or so. Since I am using a VPS-5 for this example, I will be using 1G as the CACHESIZE.

Further, the OPTIONS variable should be changed to "-l 127.0.0.1". This will force memcached to only listen on localhost, and not all available interfaces – this is good security practice. This is the end result for my VPS:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="1G"
OPTIONS="-l 127.0.0.1"

After you save your file, type the following command to restart both memcached and load the new configuration file:

systemctl restart memcached

Installing PHP memcache modules

Now that memcached is installed, you begin installing the PHP modules associated with communicating with the memcached interface. Most CMS and other web applications use PHP by default, so it's a good idea to install the modules. I will be showing the installation instructions for installing memcached on PHP56, and PHP7* versions.

We'll be using PECL to install the memcached modules for our PHP installations. Keep in mind this is for EasyApache4 servers, and may not work as desired with EasyApache3. It is recommended that you upgrade your EasyApache3 installation to EasyApache4 before continuing.

For PHP56, the module is called "memcache", the following command will use the PHP Extension Community Library to install the module on the specified version of PHP:

scl enable ea-php56 'pecl install memcache'

During this module's install, just hit [Enter] when you're queried with the following:

Enable memcache session handler support? [yes] :

For PHP7*, the module is called memcached. Replace the 0 with 1 or 2 for versions PHP71 and PHP72:

scl enable ea-php70 'pecl install memcached'

During this module's install, you'll be queried with the following - instead of pressing [Enter], type what's displayed below and then hit [Enter]:

libmemcached directory [no] : no –disable-memcached-sasl

Memcached SASL is a security layer which adds authentication support to memcached's protocols - you don't need this, and it's not recommended in an environment where multiple domains are being served, and may be using memcached.

To confirm the modules are installed, type:

scl enable ea-php56 'php -m' | grep memcache
scl enable ea-php70 'php -m' | grep memcached

Once you confirm the modules are installed, you will need to restart apache with the following command, to ensure the modules are loaded:

systemctl restart httpd

vps-hosting/install-memcached-on-centos-7-with-cpanel.txt · Last modified: 2018/04/12 13:32 by Derrick B.