Make your Laravel App Fly with PHP OPcache
What is OPcache?
Every time you execute a PHP script, the script needs to be compiled to byte code. OPcache leverages a cache for this bytecode, so the next time the same script is requested, it doesn’t have to recompile it. This can save some precious execution time, and thus make your app faster (and maybe save some server costs).
Of course we want to know what kind of optimisation we are talking about. The performance improvements highly depends on your app and server configuration, but we can get an idea by running a quick benchmark.
So I spinned up a small DO droplet with 1 CPU and 1 GB RAM and ran Apache Benchmark. I requested the default welcome page of Laravel 5.4 and let the benchmark run for 1 minute with 10 concurrent connections:
OPcache disabled: 10.18 requests per secondNot even that bad for such a small server, but we can do better.
Enabled with default values: 34.52 requests per second
What a difference! Let’s see if we can push it further.
Enabled with optimized values: 42.53 requests per second
Sounds awesome, how can I use it?
Before we do anything, I recommend that you test everything first on a development / staging machine before doing it on a live server.
At first, we need to make sure OPcache is installed on our server. If you have a recent server configuration, the chances are high that OPcache is already installed, since it becomes more common among different providers. If you use Laravel Forge, it is already installed and enabled (at least on recent versions).
To check if it is installed, we can run a simple PHP script containing:
This will show you all the information about your PHP installation. Search here for ‘OPcache’. If you found it, it is installed.
If you don’t have it installed, search for a installation instruction on the internet, or forum/help section of your provider.
Now that we made sure it is installed, we can tweak the configuration to get the most out of it.
> On Laravel forge you can choose files -> Edit PHP FPM configuration on the server’s overview to open the PHP configuration file.
> On any other server, you have to find the loaded .ini file. It is listed on the php-info page we just created under ‘Loaded Configuration File’. Open it with your favourite editor as root.
Now you we can change some values, I will walk you through the most important ones. Make sure to place them under the [opcache] section in your ini file.
This of course, enables OPcache.
How many Megabyte you want to assign to OPcache. Choose anything higher than 64 (default value) depending on your needs.
How many Megabyte you want to assign to interned strings. Choose anything higher than 16 (default value).
How many scripts can be cached. Make this as close as possible (or more) to the amount of scripts in your project. Choose any of these values: 3907, 7963, 16229, 32531, 65407, 130987 (see the docs for an explanation: http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-accelerated-files)
This will revalidate the script. If you set this to 0(best performance), you need to manually clear the OPcache every time your PHP code changes (we will handle this in the next section). If you don’t want to clear it yourself, you can set this to 1 and configure the revalidate interval with opcache.revalidate_freq, this will cost you some performance as it needs to check for changes every x seconds.
This will preserve comments in your script, I recommend to keep this enabled, as some libraries depend on it, and I couldn’t find any benefits from disabling it (except from saving a few bytes RAM).
Fast shutdown should give a faster mechanism for clearing memory. However, in my Benchmarks it was a bit slower. Maybe it can give some improvement to your app, but you should try it for yourself.
So my final config looks like this:
Experiment with these values, depending on the size of your application and the resources of your server. You can view your usage and stats with my OPcache package (more in the next section) and change values according.
Now save the file and restart your web server. Your app should now be faster!
Prepare your Laravel App
As mentioned earlier, we need to manually clear the OPcache every time we change PHP code. I created a handy package that gives you Artisan commands to work with OPcache.
To install follow the instructions in this repo: github.com/appstract/laravel-opcache
Next, add this command somewhere to the end of your deploy script (I put it just after artisan up) php artisan opcache:clear
The package also contains some other helpful tools for OPcache, you can read more about it on the Github page.
I hope this article was useful and your app is now faster. If you have any questions, don’t hesitate to leave a comment.
Laravel Forge now includes a button for enabling OPcache under the ‘PHP’ tab of your server. This will enable and optimize OPcache (with pretty much the same values listed in this article).