Enabling and tuning PHP OpCache for faster executionfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In the previous lesson, we explored using Batch API for efficient bulk updates. To further enhance Drupal's performance, this lesson focuses on enabling and tuning PHP OpCache, a crucial component for reducing PHP execution time and server resource utilization.

What is PHP OpCache?

PHP OpCache is a module integrated within PHP that speeds up execution by storing precompiled script bytecode in memory. This eliminates the need for PHP to load and parse scripts on each request, significantly reducing processing time and enhancing site responsiveness.

Benefits of PHP OpCache

  • Faster Execution: Precompiled scripts result in quicker execution times and increased throughput.
  • Reduced Load: Minimizes CPU usage and server demands, allowing more resources for other tasks.
  • Improved User Experience: Faster page loads and reduced latency enhance overall user satisfaction.

Enabling PHP OpCache

Ensure that PHP OpCache is installed and active on your server. Many PHP distributions come with OpCache enabled by default, but you can verify and configure it manually.

Step-by-Step Guide to Enabling OpCache:

Step 1: Verify OpCache Installation

Check if OpCache is enabled on your server using the phpinfo() function:


<?php
phpinfo();
?>

Load this script in your browser. Search for "OpCache" in the output to confirm its presence.

Step 2: Enable OpCache in PHP Configuration

Locate your php.ini file and ensure the following directives are set:


; Enable the OpCache extension
zend_extension=opcache

; Enable OpCache
opcache.enable=1

; Enable the OpCache for CLI
opcache.enable_cli=1

Step 3: Restart Your Web Server

After making changes to php.ini, restart your web server to apply the new settings:


# For Apache
sudo service apache2 restart

# For Nginx
sudo service nginx restart

Tuning PHP OpCache for Optimal Performance

Tuning OpCache involves configuring it to maximize efficiency according to your site's needs. Adjust these settings in php.ini:

Key OpCache Configuration Settings

  • opcache.memory_consumption: Define the memory size allocated to the OpCache. Increase this value if your site hosts numerous or large scripts. Ideal starting point: 128MB.
  • opcache.max_accelerated_files: Set the maximum number of files that can be cached. Higher values accommodate more files. Recommended: 10,000.
  • opcache.revalidate_freq: Sets the frequency (in seconds) at which script timestamps are checked for updates. A shorter interval ensures recent changes are captured but mildly increases load.

Conclusion

Enabling and finely tuning PHP OpCache is a straightforward yet powerful method to boost Drupal's execution performance. It ensures that your site runs efficiently, providing users with a faster and more robust experience.

What's Next?

Next, we will explore "Optimizing PHP-FPM Settings for Performance." This session will focus on maximizing your server's PHP processing capabilities to further optimize response times. Stay engaged as we dive deeper into optimizing your Drupal environment!