PHP OPcache

Jump to: navigation, search

LiquidWeb API Banner.jpg

OPcache Overview

For PHP 5.5 and newer, opcaching is now built into PHP 5.5 by default. Previously, this opcache was known as Zend Optimizer +, it was and is still free, but it was not totally open source and was not bundled in with PHP. Because OPcache is the only opcode cache bundled in with PHP 5.5 so this is really the opcache to use. You can still use APC or Xcache or whatever, but this built in OPcache is pretty damn good, so you should probably use it unless you can use [HHVM] which is not really an OPcache, but still awesomesauce.

OPcaching involves storing and reusing parts of PHP code that have already been compiled and are ready to go. If you don't use some form of opcode caching then you have to interpret the code and compile it every time it gets requested, which is a lot of you are index.php. So the idea is to avoid this slow, read and compile process, the Zend Engine basically compiles it once, then stores it in Memory and serves up requests from RAM, barely using any CPU.

By utilizing opcode caching, mainly opcache, you can significantly improve performance while reducing CPU utilization, which is how things happen in an ideal world, luckily if you enable OPcache, you will be living in the ideal world. :)

TL;DR You should use opcode caching as much as possible otherwise you are doing it wrong.

Zend OPcache Links

Zend OPcache Configuration Settings


opcache.max_accelerated_files - Controls how many PHP files can be held in memory at the same time. Should be set to a Prime number for better performance. { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 }

To find out how many files you have, you can use a command like this.

find . -type f -print | grep php | wc -l

I set this to 7963 for my 1GB server and this value seems to work well.

opcache.max_accelerated_files = 7963


opcache.memory_consumption - The default is 64MB, if you have a ton of sites or large amounts of PHP code in general you might want to raise this. For a 1GB server I set this to 64MB.

opcache.memory_consumption = 64


opcache.interned_strings_buffer - PHP uses string interning to improve performance. If you have the string "mahstring" 100 times in your code, PHP will store 1 variable for the string and will use a pointer to it for the other 99 times it gets used. This value is in MB. Raising this value too high causes each PHP process to use more RAM, or at least appear to. When this was set to 16MB each PHP process was around 48MB. When set to 4MB each PHP process used around 32MB RAM each. This is shared RAM so the usage may not matter much, but just keep that in mind when you tune this and use high values.

Default Value

opcache.interned_strings_buffer = 4

The default value for PHP 5.5 is 4MB. It is suggested to raise opcache.interned_strings_buffer to 8MB or 16MB for increased performance and potentially a lot of saved memory.

For better performance, try raising this to either "8" or "16".

opcache.interned_strings_buffer = 8


If set to "1", PHP will check for changes according to the interval defined via opcache.revalidate_freq. If revalidate_freq=0 and validate_timestamps=1, then PHP will revalidate files on every request. This is good if you are testing out changes to code, but in a production environment it can hurt performance.

For Development / testing

opcache.validate_timestamps = 1

For performance / production

opcache.validate_timestamps = 0


opcache.revalidate_freq How often in seconds should the code cache expire and check if your code has changed. 0 means it checks your PHP code every single request IF YOU HAVE opcache.validate_timestamps ENABLED. opcache.validate_timestamps should not be enabled by default, as long as it's disabled then any value for opcache.revalidate_freq will basically be ignored. You should really only ever enable this during development, you don't really want to enable this setting for a production application.


For optimal opcode caching performance, use the settings below


PHP 5.5 Zend OPcache Configuration

Assuming you are using PHP 5.5+ you should be able to easily enable PHP's built in OPcache by modifying /usr/local/lib/php.ini. Make sure that is located in your extensions directory that is specified in the php.ini file. Once you have edited the file restart php-fpm.

How to Enable Zend opcache on cPanel

If you have a cPanel server and already have PHP version 5.5 installed all you need to do is modify the main php.ini file and add in the lines below. Using Opcache can significantly improve PHP performance which means your website will run faster. If you have a large website I would suggest raising the opcache.memory_consumption value to something higher than 64 (mb).

The first step is to make sure you are using PHP 5.5. To check this run:

php -v

If you are not running PHP 5.5 then you will want to run EasyApache and upgrade PHP to 5.5.


Once you are running PHP 5.5 you will want to enable opcache by adding the lines below to your php.ini

vim /usr/local/lib/php.ini

##Add these lines to the bottom of the file


Restart Apache

service httpd restart

At this point you should see that the zend and php modules for opcache are both loaded.

php -m | grep OP

Zend OPcache
Zend OPcache

Additional Information

Video: Scaling PHP in the Real World - Dustin Whittle