The Zend OPcache extension improves PHP performance by storing precompiled script bytecode in shared memory, eliminating the need for PHP to load and parse scripts on each request.
When PHP processes a script, it typically follows these steps:
Incoming Request → Zend Engine reads .php file → Lexical analysis and parsing → Generation of Opcode (executable computer code) → Execusion of Opcode → Response output
Without caching, this process repeats entirely for every request, even when the source code remains unchanged. Since identical source produces identical opcode, caching eliminates redundant compilation overhead.
After enabling OPcache, the execution flow becomes more efficeint by reusing cached opcodes from shared memory.
Installation
To install OPcache on systems using yum package manager:
# List available PHP 7.1 packages
yum list php71*
# Install OPcache extension
yum install php71w-opcache.x86_64
Configuration
Add the following directives to your php.ini configuration file:
; Load the OPcache extension
zend_extension=opcache.so
[opcache]
; Enable OPcache functionality
opcache.enable=1
; Activate OPcache in CLI mode
opcache.enable_cli=1
; Allocate 128 MB of shared memory for OPcache
opcache.memory_consumption=128
; Share interned string buffer across all PHP-FPM processes
opcache.interned_strings_buffer=8
; Maximum number of files that can be cached (must exceed total project files)
opcache.max_accelerated_files=4000
; Check for file updates every 60 seconds
opcache.revalidate_freq=60
; Enable fast shutdown mechanism for quicker resource cleanup
opcache.fast_shutdown=1
; Disable automatic timestamp validation in production environments
opcache.validate_timestamps=0
; Store compiled opcode in external file cache (/tmp directory)
opcache.file_cache=/tmp
For production deployments, setting opcache.validate_timestamps=0 requires manual restart of PHP-FPM or web server services after deploying new code changes.
Performance improvements vary based on application complexity, but typical REST API endpoints show significant reduction in response times—from several hundred milliseconds down to approximately 50 milliseconds.