Make your Laravel App Fly with PHP OPcache

Easy guide to enable and optimize OPcache for a faster application

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).

Talking numbers

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 second

Not 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

Convinced yet?

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:

<?php
phpinfo();

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.

opcache.enable=1

This of course, enables OPcache.

opcache.memory_consumption=512

How many Megabyte you want to assign to OPcache. Choose anything higher than 64.

opcache.interned_strings_buffer=64

How many Megabyte you want to assign to interned strings. Choose anything higher than 16.

opcache.max_accelerated_files=32531

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.

opcache.validate_timestamps=0

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.

opcache.save_comments=1

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).

opcache.fast_shutdown=0

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:

opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32531
opcache.validate_timestamps=0
opcache.save_comments=1
opcache.fast_shutdown=0

You can experiment with these values, depending on the size of your application and the resources of your server.

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.

UPDATE:

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).

← more stories in Appstract

Show your support

Clapping shows how much you appreciated Olav van Schie’s story.