PHP OPCache production optimisation on Laravel Forge

By Gavin Phillips @ Cowshed Works Ltd

Blog Picture

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 http://php.net/manual/en/intro.opcache.php

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.

Author: Gavin Phillips
Published: Apr 12, 2017 (1 year ago)

Work with Cowshed:Works

Ready to start your next website or app project? Give us a call or send us an email.



© Cowshed Works Ltd: UK Staffordshire-based website design, build and management

VAT: 251 4480 22 - Registered in England: 10399485

Terms: Website | Service