Using PHP OPcache is a great way to improve your overall performance. OPcache stores pre-compiled script bytecode in memory, which eliminates the need for PHP to load and parse scripts on every request.

In this tutorial, you will learn how to use the Laravel along with OPcache to speed up your website!

Enable PHP OPcache

There are a couple of ways of checking if you already have PHP OPcache enabled.

You could either add a PHP info file in your Laravel public folder with the following content:

phpinfo();

And then visit the file via your browser. After that, use CTRL+F and search for OPcache.

Another way of checking if OPcache is enabled is to run the following command via your terminal:

php -m | grep -i opcache

The output that you would see the following result:

Zend OPcache

If you don't have OPcache enabled, you can install it with the following command on Ubuntu:

sudo apt install php-opcache

Configure PHP OPcache

Once you have OPcache installed, you can adjust some of the default configuration settings to optimize the performance.

To make those changes, you need to edit your php.ini file. If you are not sure where exactly the php.ini file is located at, you can again use a PHP info file to check the location.

Using your favorite text editor, open the file:

nano /etc/php/7.4/fpm/conf.d/10-opcache.ini

Then at the bottom of the file add the following configuration:

opcache.memory_consumption=256
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32531
opcache.validate_timestamps=0
opcache.enable_cli=1

A quick rundown of the values:

  • opcache.memory_consumption=256: This is the size of the memory storage used by OPcache. You can increase the opcache.memory_consumption=256 value in case that you have enough RAM on your server.
  • opcache.interned_strings_buffer=64: The amount of memory alocated to storing interned strings. The value is in megabytes.
  • opcache.max_accelerated_files=32531: The number of scripts in the OPcache hash table or, in other words, how many scripts can be cached in OPcache.
  • opcache.validate_timestamps=0: With this directive, we specify that OPcache should not be cleared automatically, which means that we would need to do this manually.
  • opcache.enable_cli=1: This enables the OPcache for the CLI version of PHP, which could be beneficial if you are using any artisan commands.

Once you make the change, you need to restart PHP FPM:

systemctl restart php7.4-fpm.service

Configure Laravel OPCache

In order to have some better management over the caching, we will use the Laravel OPcache package.

To use the package, you would need to have Larave 7 or newer. To install the package, just run the following command:

composer require appstract/laravel-opcache

One important thing that you need to keep in mind is that your APP_URL needs to be set correctly in your .env file so that it matches your domain.

Once you have the package installed, it provides you with some excellent PHP artisan commands which you could use to help you manage your OPcache:

- Artisan command to clear OPcache:

php artisan opcache:clear

- Artisan command to show OPcache config:

php artisan opcache:config

- Artisan command to show OPcache status:

php artisan opcache:status

- Artisan command to pre-compile your application code:

php artisan opcache:compile {--force}

Conclusion

Enabling PHP OPcache is an excellent and quick way to boost the performance of your Laravel application without having to make any significant changes.