Introduction: Using Cloudflare CDN with Drupal 8 Sites
This Tutorial will guide web publishers to set-up website caching with the Cloudflare Content Delivery Network (CDN) service using cPanel for DNS management with the Cloudflare administration tools. Site owners will also need to install the Purge module for cache management.
Cloudflare offers a free CDN service that is common on shared Linux Hosting plans running cPanel. The company also has a paid service as well as the Railgun app that synchronizes websites with dynamic content to keep fresh content in the cache files.
Using the Cloudflare CDN service with Drupal 8 can speed up page load speeds considerably, taking pressure off of web servers under high traffic & saving resources. Administrators will not need to manage the DNS requirements in the settings.php file.
Step One: Enable Cloudflare for the Drupal 8 Site in cPanel
In cPanel, click on the Cloudflare icon and navigate to the administration section with a list of all domains. Enable Cloudflare on the Drupal 8 domain to be cached by CDN.
After Cloudflare CDN has been enabled for the domain, the domain is listed with a link to settings. You can choose either the free version or paid upgrade at $11.95 per month.
Note: Anyone can register with Cloudflare CDN for a free account at: sign-up.
Configuration: Review the default configuration settings for the domain & make the following changes. It may be required to experiment for the best values for a domain.
Recommended Cloudflare CDN Settings - cPanel:
- Cloudflare Caching Level:Basic
- Web Application Firewall:On
- Cloudflare Security Settings:Medium
- Auto Minify: Off
- SSL Support:Full
- Image Optimization:Basic
Save the settings under either a free or plus Cloudflare CDN account in cPanel & then proceed to configure the Drupal 8 website to install support for the caching framework.
Step Two: Install Module Support on the Drupal 8 Website
In order to install the Cloudflare module for Drupal 8, there are 4 additional modules that must be installed as prerequisites, one of which is only available on GitHub.
Required Downloads - Cloudflare Modules:
- Cloudflare: (download files)
- Chaos Tools: (download files)
- Purge: (download files)
- URLS Queuer: (download files)
- Cloudflare PHP SDK: (download files)
Download and install the files listed above at admin/modules/install or using FTP, Git, Drush, etc. The Cloudflare PHP SDK requires Composer to install with the command:
Note: If Composer is not installed on your web server, see this manual at drupal.org.
Cloudflare Module - Configuration Settings:
There are two modules in the Cloudflare suite to be enabled:
- [x] CloudFlare - CloudFlare CDN integration.
- [x] CloudFlare Form Tester - Adds replacement services used for testing CloudFlare forms.
These modules provide the Cloudflare API integration but require the Purge module.
Purge Module - Configuration Settings:
The Purge module works with the Cloudflare CDN administration settings to standardize the cache files according to the scheduled refresh cycle or new content.
Configuration: For the Purge module, install & enable the following options:
- [x] Purge - Provides a generic external cache invalidation API and queue service.
- [x] Purge Drush - Administrative Drush commands for Purge.
- [x] Purge Tokens - Adds tokens support for external cache invalidation.
- [x] Purge UI - Administrative interface for Purge.
- [x] Cron processor - Processes the queue every time cron runs, recommended for most configurations.
- [x] Late runtime processor - Process the queue on every request, this is only recommended on high latency configurations.
- [x] Core tags queuer - Queues every tag that Drupal invalidates internally.
- [x] URLs queuer - Queues URLs or paths to your Purge queue utilizing a traffic database.
- [x] CloudFlare Purger - CloudFlare CDN integration.
Install & enable all of the Purge module suite options then enable the Cloudflare settings in the administration panel. You will be unable to install the Cloudflare module without first installing the Cloudflare PHP SDK using Composer for dependencies.
Settings: Manage the cache performance in Drupal 8 administration at: admin/config/development/performance/purge & use the Adv Agg module for options.
Step Three: Enable Path-Based Caching for Free Plans
Drupal 8 websites using the free Cloudflare CDN account will need to also enable path-based caching. Tag-based caching is the default value but limited to the Plus plan.
In order to do this, you must have Drush installed on the web server & run the following commands:
Drush Commands - Cloudflare CDN Module:
- To uninstall purge_queuer_coretags:
drush dis purge_queuer_coretags
- To install purge_queuer_url:
drush en purge_queuer_url
- To clear stale tag clear requests:
drush ev "deletefrom queue wherename = 'purge'"
drush cset system.performance cache.page.max_age31536000
- To empty the Drupal 8 page cache:
To finish, clear the Varnish page cache if it is installed on the web server and empty the Cloudflare cache through the administration panel available in the cPanel section.
Required Modules - Download Files:
Download, install, & enable the module files as usual, then navigate to admin/config/development/performance to configure the settings.
Configuration: Under the Bandwidth Optimization settings panel, select:
- [x] Aggregate CSS files
- [x] Minified Source HTML
Step Five: Create Page Rules for Admin & Anon Users
In order to customize your website for publishing use with Cloudflare CDN caching services to speed up page load times, consider building custom Page Rules for users.
How to Build New Page Rules for Drupal 8 & Cloudflare CDN:
- Select the domain with the Drupal 8 website in the cPanel Cloudflare panel.
- Next, click on the “Page Rules” link.
- To build a new filter system, click on the “Create Page Rule” link.
- Enter the base URL to match against other patterns, i.e. admin/* or /user/*.
- Click “Add a Setting” & choose the new rule created to activate it.
- Save the settings & exit.
Page Rules allow Drupal 8 administrators to filter different sections of a website out of Cloudflare CDN caching like administration pages when this can interfere with functions. See this video to learn more about optimizing Drupal 8 with Page Rules.
With this configuration, a Drupal 8 website can be cached with the free or paid Cloudflare CDN service using the cPanel administration tools & Drupal 8 modules.
Avoid caching pages to a CDN when under development & test settings for optimal performance. Cloudflare CDN is one of the best solutions for Drupal 8 page speed.
Check out these top 3 Drupal hosting services: