Skip to main content

OPcache

Learning Focus

By the end of this lesson you will understand what OPcache does, how to enable it for lsphp, and what settings matter.

What OPcache Does

OPcache compiles PHP scripts into bytecode and stores them in shared memory. On subsequent requests, PHP skips the parsing and compilation steps and executes the cached bytecode directly.

Without OPcacheWith OPcache
Parse → Compile → ExecuteExecute (from cache)
Every request recompilesFirst request only compiles
Slower response timesFaster response times

Enabling OPcache

OPcache is usually included with lsphp but may need to be enabled:

# Check if OPcache is enabled
/usr/local/lsws/lsphp84/bin/lsphp -m | grep OPcache

# If not listed, enable it in php.ini
sudo nano /usr/local/lsws/lsphp84/etc/php/8.4/litespeed/php.ini

Add or verify these settings:

[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0 ; Set to 1 during development
opcache.save_comments=1
opcache.revalidate_freq=0
# Restart to apply
sudo /usr/local/lsws/bin/lswsctrl restart
warning

Set opcache.validate_timestamps=0 in production for best performance, but set it to 1 during development. With validation disabled, you must restart PHP to pick up code changes.

Key Takeaways

  • OPcache eliminates repeated PHP compilation — it is essential for production.
  • Allocate enough memory (128MB+) for all your PHP files.
  • Disable timestamp validation in production, enable it in development.

What's Next