Optimizing Laravel with PHP OPcache: How to Reduce Page Load Time by Half
Feb 4, 2025
2 min read
23 views
Share

By admin

Laravel applications often contain a large number of files that need to be interpreted with each server request. In my project, the number of files reached 20,000, causing significant disk load and affecting page load speed. The solution to this problem was using PHP OPcache, which significantly reduced IOPS and doubled the speed of request processing.

The simplest way to measure the time required to load the web resource is by using the following command:

$ time curl -Vk https://web.resource.com/

Installing PHP OPcache

To enable OPcache, you need to install or ensure that the OPcache extension is already available in PHP.

1. Installing opcache (if not already installed)

For Debian/Ubuntu, run:

$ sudo apt install php-opcache

Check if OPcache is active:

$ php -m | grep opcache

If opcache is not enabled, add or modify php.ini:

zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=20000 
opcache.validate_timestamps=0

2. Restarting the Web Server

To apply the changes, restart your web server:

$ sudo systemctl restart apache2 # For Apache
$ sudo systemctl restart nginx # For Nginx
$ sudo systemctl restart php-fpm # If using PHP-FPM

Results of Implementing opcache

After enabling OPcache, the performance of the Laravel application doubled due to the following improvements:

  1. Reduced IOPS on the disk, as PHP no longer spends resources reading and interpreting files repeatedly.
  2. Faster request execution due to bytecode caching.
  3. Lower CPU load, as files do not need to be recompiled repeatedly.

Conclusion

If your Laravel application contains a large number of files, enabling OPcache is one of the simplest and most effective ways to improve performance. This solution allowed us to cut page load times in half, significantly enhancing the user experience and reducing server load.