DEV Community

Cover image for OPcache Configuration for Laravel: The Free Performance Boost You're Ignoring
Deploynix
Deploynix

Posted on • Originally published at deploynix.io

OPcache Configuration for Laravel: The Free Performance Boost You're Ignoring

OPcache is the single most impactful performance optimization you can make for a Laravel application, and the majority of production servers are either not using it at all or using it with suboptimal settings. Enabling and properly configuring OPcache typically delivers a 2x to 4x improvement in requests per second with zero code changes.

Deploynix enables OPcache on all provisioned servers by default. But "enabled" and "optimally configured" are different things. This guide explains what OPcache does, provides optimal settings for Laravel applications, covers the preloading feature, and addresses the cache invalidation challenges that trip up most developers.

What OPcache Actually Does

When PHP executes a script, it goes through four stages:

  1. Lexing: The source code is broken into tokens
  2. Parsing: Tokens are organized into an Abstract Syntax Tree (AST)
  3. Compilation: The AST is compiled into opcodes (bytecode instructions for the PHP virtual machine)
  4. Execution: The opcodes are executed by the Zend VM

Without OPcache, stages 1-3 happen on every single request, for every single PHP file your application loads. A typical Laravel request loads 200-400 PHP files (framework files, package files, your application code). That is 200-400 files being lexed, parsed, and compiled on every request.

OPcache caches the compiled opcodes in shared memory. After the first request compiles a file, subsequent requests skip stages 1-3 entirely and jump straight to execution. The compilation cost is paid once, not thousands of times per second.

This is why the performance improvement is so dramatic. For a Laravel application, compilation typically accounts for 50-75% of the total request processing time. Eliminating it doubles or quadruples your throughput.

Verifying OPcache Is Enabled

On your Deploynix-managed server, check OPcache status:

php -i | grep opcache.enable
Enter fullscreen mode Exit fullscreen mode

You should see:

opcache.enable => On => On
opcache.enable_cli => Off => Off
Enter fullscreen mode Exit fullscreen mode

OPcache should be enabled for FPM (opcache.enable = 1) but typically disabled for CLI (opcache.enable_cli = 0) since CLI scripts are short-lived and do not benefit from caching.

For more detailed status, create a temporary PHP file:


php
Enter fullscreen mode Exit fullscreen mode

Top comments (0)