PHP Opcode caching for Laravel

Nagvekar
4 min readMay 13, 2024

--

Opcode caching in PHP is an optimisation method that enhances PHP’s performance by reducing the repetitive loading and parsing of scripts for each request. This technique involves saving the precompiled bytecode of PHP scripts into shared memory.

Performance difference between site with PHP and OPCode
Difference in Performance: With and Without OPcache

As a result, PHP can skip the steps of loading and parsing the scripts on subsequent requests because the precompiled code is readily available. This means that the server can execute the code directly without the usual overhead of compiling it every time a request is made.

OPcache is one of the most renowned opcode caching tools available for PHP, and it has been a default inclusion and enabled in the PHP distribution starting from version 5.5. This integration ensures that developers can leverage opcode caching out of the box to optimize their PHP applications efficiently.

Enabling and Configuring OPcache on the Latest PHP Version

Starting with PHP 8, OPcache is typically bundled as a default feature, though activation and specific configuration may be required to tailor it to your server’s environment and particular performance demands. This customisation allows you to optimise settings based on how PHP is used within your infrastructure. For instance, a high-traffic website might need different settings compared to a development server or a low-traffic content site.

Laravel, a comprehensive and well-structured MVC (Model-View-Controller) framework for PHP, derives considerable advantages from the use of opcode caching. Due to its architecture, Laravel involves a substantial number of PHP files. Every time a request is made to a Laravel application, PHP would normally need to load and interpret these files, which can be resource-intensive. By utilizing OPcache, Laravel can store the bytecode of these files in memory after the first compilation. This stored bytecode can then be reused for subsequent requests, significantly reducing the processing overhead and improving the response time and overall efficiency of Laravel applications. This makes opcode caching especially beneficial for performance optimisation in Laravel projects.

Step 1: Ensure OPcache is Installed

Initially, confirm whether OPcache is activated in your PHP setup. You can achieve this by reviewing your php.ini configuration file or executing the subsequent command:

php -m | grep opcache

Step 2: Install Required Packages

If OPcache isn’t installed, you’ll have to install it. The installation procedure varies depending on the operating system. For Ubuntu, you can utilize the following command:

sudo apt-get install php-opcache

For CentOS or similar distributions:

sudo yum install php-opcache

Make sure to restart your web server after installing OPcache.

Step 3: Configure OPcache in `php.ini`

To enhance OPcache’s performance with Laravel, you must configure it accordingly. Modify your php.ini file. Please note that the location of this file may differ based on your setup.

# php.ini File
opcache.enable=1
opcache.revalidate_freq=2
opcache.validate_timestamps=1
opcache.max_accelerated_files=10000
opcache.memory_consumption=192
opcache.max_wasted_percentage=10
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1
  • opcache.enable: Enables OPcache.
  • opcache.revalidate_freq: How often to check script timestamps for updates, in seconds. Setting it to `2` seconds is a balanced choice during development.
  • opcache.validate_timestamps: If disabled, you must reset OPcache manually or restart the web server if scripts change.
  • opcache.max_accelerated_files: Increases the number of scripts that can be cached in memory.
  • opcache.memory_consumption: The amount of memory for storing precompiled scripts in megabytes.
  • opcache.max_wasted_percentage: The maximum percentage of “waste” in the cache before a restart is scheduled.
  • opcache.interned_strings_buffer: The amount of memory for interned strings in megabytes.
  • opcache.fast_shutdown: Accelerates the shutdown time of PHP requests.

Step 4: Restart Web Server

After changing your php.ini settings, restart your web server to apply changes:

#Apache 
sudo service apache2 restart

#Nginx
sudo systemctl restart nginx

Step 5: Integrate with Laravel

Laravel doesn’t necessitate any specific plugins or packages for OPcache usage since it’s an inherent PHP feature. However, you can administer and clear OPcache within Laravel by utilising packages. Here’s one of the popular packages:

laravel-opcache by Appstract:

Installation using Composer:

composer require appstract/laravel-opcache  

# $Publish the config file:
php artisan vendor:publish --provider="Appstract\Opcache\OpcacheServiceProvider"

#Now, you can use Artisan commands to clear and configure OPcache:
php artisan opcache:clear

#Get status:
php artisan opcache:status

This package provides a convenient way to interact with OPcache through Artisan commands, fitting well into the Laravel ecosystem.

Step 6: Test and Debug

Once all configurations are in place, it’s essential to conduct thorough testing of your Laravel application within a staging environment before deploying to production. Verify that alterations made to your PHP scripts are properly reflected and monitor the application for performance enhancements or any potential issues that may surface.

OpCode caching is anticipated to yield significant improvements in response times and reduce CPU usage across your Laravel applications. By following these steps, you can effectively implement and oversee OpCode caching within your PHP environment.

Thank you for taking the time to read my post. Your suggestions and feedback are invaluable to me. I’m committed to enhancing my current content, and your input will inspire me to share more posts in the future.

--

--

Nagvekar

Experienced tech leader skilled in programming, web services, and AI. Committed to developing scalable microservices and enhancing cybersecurity.