PHP OPcache Deep Dive: How to Tune It for Maximum Performance (2026)


Introduction

OPcache is one of the most critical extensions for PHP performance. It caches compiled PHP scripts in shared memory, eliminating the need to recompile on every request. While OPcache is enabled by default in PHP 8.5, its default settings may not be optimal for your application.

In this benchmark, we test three key OPcache parameters:

  • opcache.memory_consumption – how much memory to use for caching
  • opcache.max_accelerated_files – how many files to cache
  • opcache.revalidate_freq – how often to check for file changes

We test these parameters across three scenarios:

  • Pure PHP CPU‑intensive script (recursive Fibonacci + 1M loop)
  • WordPress homepage (I/O‑bound, 100 posts, 500 comments)
  • Laravel CPU route (framework‑heavy, same Fibonacci task)

All tests use PHP 8.5.2 with JIT disabled to isolate OPcache effects.


Test Environment

ComponentSpecification
CPUIntel Xeon Platinum (4 vCores)
RAM8 GB ECC
OSUbuntu 24.04 LTS
Web ServerNginx 1.24
PHP Version8.5.2
PHP‑FPMStatic, pm.max_children = 100
OPcacheEnabled, JIT disabled
Test ToolApache Bench (ab)
Concurrency1 (single request)
Requests per Run1,000
Warm‑up500 requests before each run

Results

Parameter 1: opcache.memory_consumption

MemoryCPU (req/s)WordPress (req/s)Laravel (req/s)
64 MB10.389.368.80
128 MB10.169.368.39
256 MB10.239.368.67
512 MB10.589.379.23
  • CPU script: Throughput increases with more memory, peaking at 512 MB.
  • WordPress: Almost no change across values – I/O‑bound sites see little benefit.
  • Laravel: Clear improvement from 8.39 req/s (128 MB) to 9.23 req/s (512 MB), a 10% gain.

Recommendation: Set opcache.memory_consumption = 512 for Laravel/Symfony apps, or at least 256 MB for general use.


Parameter 2: opcache.max_accelerated_files

Max FilesCPU (req/s)WordPress (req/s)Laravel (req/s)
2,00010.579.378.77
5,00010.349.378.99
10,00010.499.389.19
20,00010.809.379.27
  • CPU: Slight improvement at 20,000 files (10.80 req/s).
  • WordPress: Unaffected – few files, default is enough.
  • Laravel: Steady increase from 8.77 to 9.27 req/s – +5.7%.

Recommendation: Set opcache.max_accelerated_files = 20000 for modern frameworks. For WordPress-only, 5000 is sufficient.


Parameter 3: opcache.revalidate_freq

Revalidate (sec)CPU (req/s)WordPress (req/s)Laravel (req/s)
0 (always)10.659.388.75
210.569.379.39
6010.799.359.39
30010.539.379.15
  • CPU: 60 seconds gives the best result (10.79 req/s).
  • WordPress: All values nearly identical.
  • Laravel: Noticeable drop at revalidate_freq = 0 (8.75 req/s) – constant file checks hurt performance. Values 2 and 60 perform equally well (9.39 req/s).

Recommendation: Set opcache.revalidate_freq = 60 for production (slightly better CPU). Use 0 only during active development (immediate file changes) – but note it slows down Laravel.


Optimal Configuration Summary

Environmentmemory_consumptionmax_accelerated_filesrevalidate_freq
Development128 MB100000
Small WordPress128 MB50002
Medium/Large WordPress256 MB1000060
Laravel / Symfony512 MB2000060

General‑purpose (balanced):

ini

opcache.memory_consumption = 256
opcache.max_accelerated_files = 20000
opcache.revalidate_freq = 60

How to Apply These Settings

Edit your php.ini (or php-cli.ini for CLI):

ini

; OPcache core settings
opcache.enable = 1
opcache.enable_cli = 1
; Optimized for Laravel / Symfony
opcache.memory_consumption = 512
opcache.max_accelerated_files = 20000
opcache.revalidate_freq = 60
; Optional but recommended
opcache.interned_strings_buffer = 16
opcache.fast_shutdown = 1

After changes, restart PHP-FPM:

bash

/etc/init.d/php-fpm-85 restart

Verify with phpinfo() or:

bash

php -r "print_r(opcache_get_status(false));"

Key Takeaways

  1. memory_consumption matters most for framework‑heavy apps – 512 MB gives a measurable boost.
  2. max_accelerated_files should be high enough to cache all your PHP files. For Laravel, 20,000 is safe.
  3. revalidate_freq = 60 seconds balances performance and update detection. Avoid 0 – it hurts Laravel.
  4. WordPress is I/O‑bound – OPcache tuning has minimal impact on its frontend.

Frequently Asked Questions

Q: How do I know if my OPcache is too small?

A: Check opcache_get_status()['memory_usage']['free_memory']. If near zero, increase memory_consumption.

Q: How do I know if max_accelerated_files is too low?

A: Compare opcache_statistics['num_cached_scripts'] with your setting. If they’re close, increase it.

Q: Should I set revalidate_freq = 0 in production?

A: No – it forces PHP to check every file on every request, killing performance. Use 60+ seconds.

Q: Do these settings work for PHP 8.4 or 8.3?

A: Yes, OPcache parameters are identical across PHP 8.x.


Test Scripts (for Reproducibility)

Important: Run these commands only on your own isolated test server.

bash

# Pure CPU script
ab -n 1000 -c 1 https://phpbenchlab.com:8080/test.php
# WordPress homepage
ab -n 1000 -c 1 https://phpbenchlab.com:8084/
# Laravel CPU route
ab -n 1000 -c 1 https://phpbenchlab.com:8085/cpu-test

Full test scripts are available in our previous benchmarks.


Conclusion

OPcache tuning can significantly improve PHP performance, especially for framework‑based applications:

  • CPU‑intensive tasks: Up to 10% gain with optimal settings.
  • Laravel: Up to 10% gain from memory and file limit increases.
  • WordPress: Minimal impact – focus on database and page caching instead.

Use our recommended configuration as a starting point, then monitor your opcache_get_status() to fine‑tune for your specific workload.

OPcache tuning is just one of the four pillars covered in our complete PHP optimization guide, which also addresses JIT configuration, application server selection, and I/O latency strategies.


All raw data and methodology are provided. Reproduce this benchmark on your own hardware to validate results.

2 thoughts on “PHP OPcache Deep Dive: How to Tune It for Maximum Performance (2026)”

  1. I’ve been exploring for a little bit for any high-quality articles
    or blog posts in this sort oof house . Exploring
    in Yahoo I finally stumbled upon this website. Studying this info So i am glad to show that I’ve a
    very good uncanny feeling I discovered just what I
    needed. I such a lot indubitably will make
    certain to don?t disregard this website
    and give it a look regularly.

    Reply

Leave a Comment