Introduction: Drupal 8 Caching & PHP 7 Speed Optimization
This Tutorial will show how to optimize Drupal 8 caching on PHP 7 using Alternative PHP Cache (APC) & third-party contributed modules that integrate with the platform.
Most cPanel web hosting companies running a version of CentOS, CloudLinux, or RHEL provide pre-installation of custom PHP stack extensions like PECL modules.
First confirm whether your cPanel web host has APC pre-installed on the Linux web server or consider upgrading to a VPS/Dedicated server to run APC caching in traffic.
Step One: Installation of APC on the Web Server
Alternative PHP Cache (APC) is one of the oldest PHP caching solutions for web servers and is pre-installed on most cPanel web hosting plans by default.
To install the PECL Modules by command line on VPS hosting or Dedicated servers:
sudo apt-get install php-apc
Or use the command:
pecl install apc
For shared web hosting plans without the ability to install PHP extensions, contact the company to request APC installation on the web server or upgrade to a VPS plan.
APC will not perform well on Drupal 8 websites running on shared Linux plans if the RAM allocation to the cache is too low. The default APC cache size is 32 MB.
For effective use of APC, plan for a minimum of a 128 MB to 256 MB cache allocation, where the rest of the available RAM will be used for Drupal 8 CMS processing.
Note that the RAM allocation must be calculated per Drupal 8 website & APC instance running on shared hosting plans with multiple domain names active on an account.
If the cache settings for APC are not sufficient, i.e. too low, a situation can result where the cache is continually re-populated by PHP code requirements & no speed-up results.
For best APC performance, increase the RAM allocation on your shared Linux plan where possible so that the server caching will not interfere with CMS processing.
Step Two: Edit the php.ini or apc.ini Configuration File
After installation, the administrator will have access to the apc.ini file for configuration settings. In shared hosting environments, it is possible to override this with php.ini.
In the apc.ini or php.ini file for the Drupal 8 installation, add the following lines:
apc.ttl=604800 apc.user_ttl=3600 apc.shm_size=256M apc.include_once_override=1 auto_globals_jit=Off apc.num_files_hint = 2048 apc.stat_ctime = 0 apc.file_update_protection = 2 apc.stat = 0
These settings will establish a 256 MB APC allocation for the PHP caching facility. Adjust as required for lower/higher values and review the cache timing variables.
Step Three: Copy apc.php file to Drupal Root for Analytics
In order to access analytics information about PHP performance & server resource usage, it is required to copy the apc.php file from the server to the Drupal root folder.
Drupal 8 does not have an integration module for APC, so it is required to run the apc.php file from the root of the installation rather than the /libraries folder.
Navigate to: /usr/share/pear/apc.php on the web server & copy the apc.php file to the root folder. Then open the page in a web browser at https://example.com/apc.php:
Drupal 8 administrators can access analytics information about the number of cached files/cache requests, amount of free memory, & total allocated RAM usage for APC.
Test the apc.ini settings in production or in a development sandbox with simulated chaos from high traffic patterns to see the performance rates under strain of use.
Step Four: Add Drupal 8 Modules for Cache Management
Memcached, APC, & Zend Opcache are the most popular PHP Accelerators for Drupal web hosting on a LAMP/WAMP environment but all target the PHP layer only.
Drupal 8 Views can be cached for much faster delivery in production:
Additionally, Drupal 8 publishers on shared web hosting can consider a CDN (Content Delivery Network) as a means for additional caching & extending server resources.
Step Five: Build Full-Stack Caching Solutions for Drupal 8
For more advanced PHP Acceleration solutions for Drupal 8 consider HHVM (Hip Hop Virtual Machine), the PHP runtime engine developed by Facebook for faster speeds.
High-traffic Drupal 8 websites frequently implement NGINX, Varnish Cache, Redis, & Memcached to support enterprise-level web traffic of 10 million + monthly page views.
Running APC caching with PHP on the web server as a PECL Module adds an additional layer to the platform optimization that is required to support high traffic.
Conclusion: Run Drupal 8 & APC on VPS/Dedicated Plans
APC is an important component of full-stack web server optimization for PHP 7 acceleration and works best when there is extra RAM allocation to the installation.
Running APC with Drupal 8 can be problematic on cPanel web hosting plans because of limited RAM resources and multiple domains sharing the same IP Address.
Use APC with Drupal 8 on VPS & Dedicated server hosting plans for page speed optimization through better PHP caching, with Memcached, Varnish, NGINX, & Redis.