PHP OPCache production optimisation on Laravel Forge

Laravel Forge now provides OPCache optimisation for production environments

Written 4 years ago on Apr 12, 2017

For those who know about PHP's OPCache you'll be aware of the huge implications it's had for speeding up response times and reducing load on servers. PHP generally comes with OPCache up and running out of the box, this is already a massive helping hand but it can actually be pushed further. Laravel's Forge PHP server provisioning service now has a simple button to make this happen on Forge-built VMs.

What is OPCache?

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.
PHP website

In plain English, PHP needs to convert scripts to machine readable 'bytecode' whenever it needs to process that script's instructions. Having to do this with every request for that script can put unnecessary stress on the server as it's quite an intensive process. For example say you have a script that takes rows from your database and runs a few routines on the data to generate a report. Those PHP functions aren't changing on each and every request so it's a waste of CPU time to have to convert them every time. OPCache solves this by creating a 'cache' of reusable scripts, when a script is called it's then taken from the cache, if possible, rather than recompiled. The results are dramatic, tests have shown increases in requests per second of 3x with no optimisation.

Laravel Forge OPCache Optimisation

As mentioned above the default settings for OPCache can actually be optimised further by altering the PHP configuration. OPCache is set up to check if a script has been changed by default, this has a performance cost but is how it should generally be set up when in development where scripts are constantly changing. However, in production the code doesn't tend to change much and so it's not really necessary to be checking this, it can dramatically speed things up again if PHP doesn't have to check this. The downside is that any changes to scripts will be missed, for example if you change something in your DB report script and upload it to the server OPCache will continue to pull the old script out of the cache if it's set to not check for changes. The way around this is to restart the web server and force OPCache to reload everything. The issue is this can easily be missed and lead to lots of weird problems if not managed properly.

The good news is that Laravel Forge now has the option to optimise these settings on production servers. I'm not sure what it's changing but I guess it's tweaking the PHP configuration based on some assumptions and data it can gather from your code.

We're using Forge at Cowshed Works now for client projects and this is something we'll be using going forward, another excellent addition to the toolbox.

Article Category Tags

Click one to see related articles hand-written by Cowshed Works


Want to discuss your next web project?

We're happy to host meetings at the Cowshed or via conference call, we're equally happy to come and meet you to discuss your project.

Just drop us a line and we'll get it booked in and get the ball rolling.

Cowshed Works

What our clients say...