PHP 5.5 Opcache Settings

Oh Dear monitors your entire site, not just the homepage. We crawl and search for broken pages and mixed content, send alerts when your site is down and notify you on expiring SSL certificates.

Start your free 10 day trial! »

Image of Mattias Geniar

Mattias Geniar, December 08, 2014

Follow me on Twitter as @mattiasgeniar

Unless you’re running all your PHP via CLI scripts, you’ll be using an Opcode Caching in your PHP configurations. This used to be APC for anything below PHP 5.5, but since PHP 5.5 there’s a built-in extension called Opcode Cache that replaces APC. (note: APC is still available for user-caching, like a key/value store)

To activate the PHP Opcode Cache extension, you can drop the following lines in your /etc/php.d/ directory, to a new file named 10-opcache.ini. The reasoning behind the number in the front of the file, is to ensure this gets loaded first, before other modules.

zend_extension=opcache.so

; Enable the Opcache
opcache.enable=1

; In MegaBytes, how much memory can it consume?
opcache.memory_consumption=128

; The number of keys/scripts in the Opcache hash table (how many files can it cache?)
opcache.max_accelerated_files=4000

; How often to check script timestamps for updates, in seconds.
; 0 will result in OPcache checking for updates on every request.
opcache.revalidate_freq=60

; If enabled, OPcache will check for updated scripts every
; opcache.revalidate_freq seconds. When this directive is disabled,
; you must reset OPcache manually
opcache.validate_timestamps=1

; If enabled, a fast shutdown sequence is used that doesn't free
; each allocated block, but relies on the Zend Engine memory manager
; to deallocate the entire set of request variables en masse.
opcache.fast_shutdown=1

; Chances are, you won't need this on the CLI
opcache.enable_cli=0

; If enabled, OPcache appends the current working directory to the
; script key, thereby eliminating possible collisions between files
; with the same base name. Disabling this directive improves performance,
; but may break existing applications
opcache.use_cwd=1

Reload your PHP-FPM daemon, and you should be set. If you’re looking at how your Opcache is performing, check out opcache.php (similar to apc.php’s file for checking the APC performance). If you want to know more about PHP Opcode performance, make sure to read the excellent post at EngineYard on Opcode Caching.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.