How to Optimize Drupal 8 Caching with Memcached on cPanel Hosting

Introduction: Memcached & Improved Web Server Speeds

This Tutorial will show how to improve Drupal 8 performance on shared hosting using the Memcached framework on PHP 7 & third-party contributed modules. Memcached is used to keep frequently accessed MySQL database values in RAM on a web server.

The performance gains for Drupal 8 websites running Memcached will depend on the amount of web traffic, the configuration of the website, & the amount of available RAM.

Memcached can be implemented with HTML, JavaScript, & CSS caching for Drupal 8 websites for better page load speeds, including the use of a CDN, Varnish Cache, & NGINX.

Step One:
Install Memcached on the Web Server

To begin, determine whether the project is running on shared Linux hosting with cPanel or alternatively a VPS or dedicated server with custom configuration. If the Drupal 8 runs on shared web hosting, the administrator will not be able to install PHP packages with Sudo.

Most cPanel web hosting companies have Memcached installed & enabled by default on their web servers. Admins need to login to cPanel & manage the PHP settings.

How to Optimize Drupal 8 Caching with Memcached on cPanel Hosting

For web hosts that permit the selection of PHP version in cPanel, choose the latest version (7.x). Make sure that both the memcache & memcached extensions are enabled & save the settings. Note: This applies to CentOS, RHEL, & CloudLinux.

Some web hosting companies, like SiteGround, have transitioned away from allowing shared account users to manage these settings in cPanel due to security & inefficiency.

In SiteGround SuperCacher:

How to Optimize Drupal 8 Caching with Memcached on cPanel Hosting

Navigate to the Memcached tab & enable the service on the hosting plan. After saving the settings, the values are provided for the IP Address & Memcached port number.

Note: The port settings will be required to enable Memcached to work correctly in Drupal 8. If not provided, contact your host for the IP Address & Memcached port.

Otherwise, if you are configuring a VPS or dedicated server, Memcached can be installed using Sudo commands:

sudo apt-get update

sudo apt install memcached
sudo apt install php-memcached

After installing Memcached, restart the Apache web server extensions:

service memcached restart
service php7.0-fpm restart

This completes the installation on VPS or dedicated web servers running CentOS, RHEL, Ubuntu, Fedora, Gentoo, SUSE, Slackware, etc. for LAMP Stack configuration.

Step Two:
Install the Memcache Module for Drupal 8

Next it is required to install the Memcache module for Drupal 8 which provides an API that integrates with PECL memcache & the memcached libraries in PHP 5.x/7.x.

Required Files – Download:

The Memcache module provides backends for Caching ( & Locking ( including a Drupal 8 administration section for browser access.

How to Optimize Drupal 8 Caching with Memcached on cPanel Hosting

To Install: Navigate to /admin/modules/install & upload the module. Enable Memcache & Memcache Admin modules & save the settings.

If your web server does not have PECL Memcache installed by default, run:

sudo pecl install memcache

Navigate to: /admin/config/system/memcache where the values for manual settings can be configured to view debugging information & statistics on each page:

How to Optimize Drupal 8 Caching with Memcached on cPanel Hosting

Leave this setting unchecked unless required by systems testing or debugging code.

Step Three:
Installing with Drush & Drush Commands

Drush can also be used to install the Memcache module on Drupal 8 using CLI. With this installation, admins will be able to flush the Memcache storage using Drush.

drush en memcache

After installing Memcache, rebuild the Drush cache:

drush cc drush

After clearing the Drush cache, two new drush commands are available:

  memcache-flush (mcf)  Flush all Memcached objects in a bin.
  memcache-stats (mcs)  Retrieve statisticsfrom Memcached.

For more information about these commands, use the “drush help” command:

  drush help mcf

  drush help mcs

If Memcache support moves to Drupal core in the future, like APC, these commands will become part of the default Drush toolset, enabling admins to clear cache via CLI.

Step Four:
Advanced Configuration Options

In order to complete the installation of Memcached support, code changes to the Drupal 8 settings.php file & the Apache php.ini file must be made by an administrator.

For Drupal 8 open the settings.php file at: sites/default & add the lines:

$settings['memcache']['servers'] = ['' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = '';
$settings['cache']['default'] = 'cache.backend.memcache';
$settings['cache']['bins']['render'] = 'cache.backend.memcache';

Note: If using SiteGround or another cPanel web hosting plan, locate the port number for Memcached on your web server & use it to replace the “11211” port value above.

For websites running Memcached with multiple servers in clusters, add the following additional code to the settings.php file with the routing information.

Multiple Memcache Backends:

  $settings['memcache']['servers'] = [
    '' => 'default', // Default host and port
    '' => 'default', // Default host with port 11212
    '' => 'default', // Default port, different IP
    '' => 'default', // Default port with hostname
    'unix:///path/to/socket' => 'default', 'Unix socket'

Multiple Servers, Bins and Clusters:

  $settings['memcache'] = [
    'servers' = [
      'server1:port' => 'default',
      'server2:port' => 'default',
      'server3:port' => 'cluster1',
      'serverN:port' => 'clusterN',
      'unix:///path/to/socket' => 'clusterS',
    'bins' => [
      'default' => 'default',
      'bin1' => 'cluster1',
      'binN' => 'clusterN',
      'binX' => 'cluster1',
      'binS' => 'clusterS',

This will complete the Memcached installation on Drupal 8 unless more advanced configuration of the cache is required. Test the settings in administration for errors.

Step Five:
Lock, Key Hash Algo, & Memcache Prefix Options

There are additional settings that can be used to configure Memcached for high traffic websites or where web server resources are being constrained for CPU performance.

To Add a Memcache Prefix:

In order for multiple Drupal 8 installations to share Memcached server resources,

create a unique prefix for each installation in the memcache config in settings.php:

$settings['memcache']['key_prefix'] = 'something_unique';

To Add a Key Hash Algorithm:

$settings['memcache']['key_hash_algorithm'] = 'sha1';

Memcache locks need to be enabled through the services.yml file. Add the following code to replaces the default lock backend with a memcache implementation:

class: DrupalCoreLockLockBackendInterface
factory: memcache.lock.factory:get

Some Drupal 8 admins recommend turning stampede protection for Memcached off to avoid potential time-out errors in request queues, which can be used for debugging.

Conclusion: Memcached & Drupal 8 Server Performance

High traffic social networks & e-commerce communities cannot manage web traffic without making use of Memcached for MySQL database queries, variables, & stores.

Build a Drupal 8 caching strategy that includes PHP, MySQL, HTML, CSS, & JavaScript caching across multiple layers with CDN support resolution for the fastest page load speeds on production websites & to optimize web server hardware usage.

Check out these top 3 Drupal hosting services:

Was this article helpful?