PHP 8 Performance Tuning: OpCache and JIT Explained

Backend2026-09-24TryQuickToolBox

Your PHP 8 application feels sluggish under load. You've optimized queries, added caching, but response times still lag. The culprit might be PHP's own execution overhead. Two powerful features—OpCache and JIT—can dramatically speed up your code, but only if configured correctly. This guide explains how they work and how to tune them for real-world workloads.

What is OpCache and Why It Matters

PHP is an interpreted language: each request normally parses and compiles your source files into bytecode before executing. OpCache stores that compiled bytecode in shared memory, so subsequent requests skip parsing and compilation. The result? Lower CPU usage and faster response times.

OpCache is enabled by default in many production setups, but default settings are conservative. Tuning it can yield 2–3x throughput improvements for CPU-bound applications.

Key OpCache Configuration Directives

Recommended OpCache Settings for Production

; php.ini
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0
opcache.revalidate_freq=0
opcache.interned_strings_buffer=32
opcache.preload=/path/to/preload.php
opcache.preload_user=www-data

After changing settings, restart PHP-FPM. Monitor with opcache_get_status() to check hit rate and memory usage.

Understanding JIT in PHP 8

JIT (Just-In-Time) compilation takes OpCache a step further: it compiles bytecode into native machine code at runtime. This can significantly speed up CPU-intensive tasks like mathematical computations, but it does little for typical web requests dominated by I/O (database, filesystem, network).

JIT is part of OpCache, so you must enable OpCache first. In PHP 8, JIT is configured via opcache.jit and opcache.jit_buffer_size.

JIT Configuration Directives

Recommended JIT Settings

opcache.jit=tracing
opcache.jit_buffer_size=128M

Note: JIT only works with OpCache enabled. In CLI, you may need to set opcache.enable_cli=1 to test JIT.

OpCache vs JIT: When to Use Which

FeatureOpCacheJIT
Primary benefitCaches compiled bytecodeCompiles bytecode to native code
Best forAll PHP applicationsCPU-intensive, long-running scripts
Impact on I/O-bound appsHigh (reduces parsing/compilation)Low to none
Memory overheadModerateAdditional buffer
ComplexityLowMedium

Step-by-Step Tuning Guide

  1. Enable OpCache with the recommended settings above.
  2. Monitor OpCache using opcache_get_status() or tools like OpCache GUI. Aim for a hit rate above 99%.
  3. Adjust memory if opcache.memory_usage is near the limit.
  4. Consider preloading for large frameworks (Laravel, Symfony). Create a preload script that requires frequently used classes.
  5. Enable JIT only if your app is CPU-bound. Test with and without JIT to measure impact.
  6. Benchmark using tools like ab, wrk, or siege. Compare requests per second and latency.
  7. Deploy changes gradually and monitor error logs for issues.

Common Pitfalls and Best Practices

FAQ

Does JIT improve performance for all PHP applications?

No. JIT benefits CPU-intensive tasks like complex calculations or data processing. For I/O-bound web apps (database queries, API calls), the gains are minimal. OpCache alone often provides the biggest speedup.

How do I know if OpCache is working?

Check phpinfo() for OpCache status, or call opcache_get_status() in a script. Look for opcache_enabled => true and a high hits count relative to misses.

Can I use JIT with PHP 7?

No. JIT was introduced in PHP 8.0. PHP 7 only supports OpCache. If you're on PHP 7, focus on OpCache tuning and consider upgrading to PHP 8 for JIT and other performance improvements.

Ready to analyze your server's performance? Check out our Nginx Log Analyzer to gain insights from your access logs and identify bottlenecks.