How to speed up your Laravel application with PHP OPcache?

Created September 12, 2020


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!


In order for you to be able to follow along, you need to have a Laravel project up and running.

You can use the following referral link to get free $100 credit that you could use to deploy your servers and test the guide yourself:

DigitalOcean $100 Free Credit

After that, you can follow the steps on How to Install Laravel on DigitalOcean with 1-Click here!

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:



And then visit the file via your browser. After that, use CTRL+F and search for OPcache. You will see the following information:

Checkout our latest product - the ultimate tailwindcss page creator 🚀

PHP OPcache with Laravel

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

If you are not using Ubuntu, you can install PHP OPcache using pecl:

If you used the 1-Click image from the prerequisites, OPcache would already be installed for you.

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.

If you have used the 1-Click image from the prerequisites, you will find the php.ini file at:


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:


A quick rundown of the values:

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

systemctl restart php7.4-fpm.service

For more information on the OPcache configuration, make sure to go through the official documentation here:

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:

php artisan opcache:clear
php artisan opcache:config
php artisan opcache:status
php artisan opcache:compile {--force}


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

If you like the Laravel OPcache package, make sure to give it a star on Github!

I hope that this helps!