PHP 5.5 Opcache Settings

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile 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.

; Enable the Opcache

; In MegaBytes, how much memory can it consume?

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

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

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

; 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.

; Chances are, you won't need this on the CLI

; 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

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.