Optimizing Caching with Memcached on cPanel Hosting for Drupal 8

Optimizing Caching with Memcached on cPanel Hosting for Drupal 8

Memcached is a popular memory object caching system. Since it’s an open-source software and free, you should use Memcached for your Drupal 8 website to speed up your website.

In this article-cum-guide, we explain how to improve the performance of your Drupal 8 website on shared hosting plans.

You can implement Memcached with HTML, CSS, or JavaScript caching.

Step-by-Step Process

Here are the steps you need to follow:
  • Install Memcached
Before you can start using it, you need to install Memcached. Most web hosting companies that offer cPanel also offer Memcached by default. It might also be enabled on the web server. To confirm, login to your cPanel account and then into PHP Selector.

If your web hosting company allows to change the current PHP version, then set your PHP to the latest version. Then, from the extensions available, check both Memcache and Memcached. Save the changes you’ve made.

You should do the same if you’re using RHEL, CentOS, or CloudLinux.

For shared hosting accounts, the option to change such settings in cPanel might not be available because of security reasons. So you’d have to contact your hosting provide for that.

For those on VPS or dedicated servers, they can install Memcached with Sudo commands, which are:

Code:
sudo apt-get update
Code:
sudo apt install memcached

sudo apt install php-memcached
Once finished, you need to restart the extensions of the Apache web server by executing this command:

Code:
service memcached restart

service php7.0-fpm restart
  • Install the Memcache Module
The next step is to install the necessary modules for Drupal 8. These modules offer the API endpoints that then integrates with PECL memcache and memcached libraries.

So go ahead and download the Memcache module from Durpal modules library. It will provide the backend necessary for Caching and Locking. The files relating to Caching and Locking are memcache.inc and memcache-lock.inc respectively.

To install the Memcache module, follow this path - /admin/modules/install. Then, upload the downloaded modules.

Once done, enable two modules, which are:
  • Memcache
  • Memcache Admin
Save the settings and exit.

In case your web server doesn’t come with PECL Memcache, execute this command to install it as well:

sudo pecl install memcache

Go to this directory - /admin/config/system/memcache

Change the settings and configurations as needed. You’ll also find statistics and debugging data. Lastly, you must leave the “Show memcache statistics at the bottom of each page”.
  • Install Memcache Module Using Drush and Drush Commands
For those using Drush, they can install the Memcache Module with Drush CLI itself.

To get started, execute this command in Drush:

Code:
drush en memcache
Then, rebuild the Drush cache:

Code:
drush cc drush
You’ll see two new commands after clearing the cache, which are:

Code:
memcache-flush (mcf) Flush all Memcached objects in a bin.

memcache-stats (mcs) Retrieve statisticsfrom Memcached.
If you need more information on these commands, you can use:

Code:
drush help mcf

drush help mcs
  • Set Advanced Configuration Options
To conclude the installation of Memcached support, the admins must edit the codes in settings.php and php.ini files of Drupal 8 and Apache respectively.

Open the settings.php file from the location sites/default and add in the following code:

Code:
$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = '';
$settings['cache']['default'] = 'cache.backend.memcache';
$settings['cache']['bins']['render'] = 'cache.backend.memcache';
If you’re on any other web hosting plan powered by cPanel, you need to find out Memcached’s port number and use that to replace the port value “11211” mentioned in the code above.

If you’ve got websites that are running Memcached with more than one server in clusters, then open the settings.php file and add in the following code that contain routing information:

Multiple Memcache Backends:

Code:
$settings['memcache']['servers'] = [
'127.0.0.1:11211' => 'default', // Default host and port
'127.0.0.1:11212' => 'default', // Default host with port 11212
'127.0.0.2:11211' => 'default', // Default port, different IP
'server1.com:11211' => 'default', // Default port with hostname
'unix:///path/to/socket' => 'default', 'Unix socket'
];
Multiple Servers, Bins and Clusters:

Code:
$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 conclude the installation of Memcached. Unless you need advanced configurations for your Drupal 8 website, this should be it. You may, however, test the settings in administration for debugging.
  • Lock, Key Hash Algo, & Memcache Prefix Options
There are a few other options you can apply to further configure Memcached. You may want to use them if you administer a high traffic website or the resources in your web servers are being constrained.

Here are a few options:
  • Add Memcache Prefix
If there are multiple Drupal 8 properties sharing a single Memcached server resources, you may want to create a unique prefix for each property. You can do that by opening the settings.php file and adding in the following:

Code:
$settings['memcache']['key_prefix'] = 'something_unique';
Add a Key Hash Algorithm

Code:
$settings['memcache']['key_hash_algorithm'] = 'sha1';
You may also want to enable Memcache locks. For this, open the services.yml file and replace the default lock backend with the following code:

Code:
lock:
class: DrupalCoreLockLockBackendInterface
factory: memcache.lock.factory:get
After you’re done, save the changes and reload things like Apache and Drupal 8. Now you have optimized Caching with Memcached for your Drupal 8 website. For more information, get in touch with your hosting support team.
Author
kumkumsharma
Views
103
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top