If you have never heard of Opcache before, you may be more familiar with its former name: Zend Optimizer. In the old days, PHP “bytecode caching” did exist but it was always a 3rd party service that had to be re-compiled as part of your PHP install (or entire Apache install, as it were). With the release of PHP 5.5+ however, the folks behind PHP decided to include a default cache engine inside PHP itself, and thereby chose Zend due to its overwhelming popularity, renaming it to Opcache (plus, it helped a bit that Zend decided to become a totally open-source code base rather than proprietary as it was before).
In turn, this has caused a mass migration away from APC (Alternative Performance Cache) which is now all but dead (despite douchey “gurus” like W3TC’s Frederick Downes refusing for 3+ years to acknowledge this). In fact, there are several reports that using old-school APC on PHP 5.4 or later can actually cause application errors, server crashes, or even data loss, so the adoption of Opcache around the web is growing exponentially fast.
Ultimately, the beauty of Opcache is its simplicity. It is very easy to activate and manage now that its included within the default
php.ini file that comes with PHP installations. And while there are tons of runtime configuration options, there are only a few that you really need to worry about in order to get Opcache up and running quickly.
Assuming you’ve already installed PHP5 and are running an Nginx/Ubuntu server, proceed to PHP configuration:
sudo nano /etc/php5/fpm/php.ini
The most important setting is of course first changing the
;opcache.enable=0 line to the below:
Next, you must determine how much RAM memory you want to allocate to Opcache. In other words, your server will be instructed to not use any more memory for caching PHP files than what you specify on the line below. Clearly, this setting depends largely on the amount of RAM your server has installed, and then, how much of that RAM you think can be spared on average for the Opcache itself. In general, I’ve found that typical WordPress sites use around 100MB of OPcache or less, so for most WordPress sites 128MB is probably sufficient. Still, if you can spare it on larger servers with tons of RAM, consider a higher setting such as 256MB or even 512MB on very busy sites.
At a certain point, however, there can be a “diminishing return” on the benefits here if certain PHP files are used rarely and/or depending on the size of your PHP code base for your site. So of course keep an eye on performance and play around with this setting (and others) as you monitor results. Also, if allocating XXXMB of RAM means that your server’s memory will then be exhausted, it’s probably not a good idea to proceed, as you risk instability.
Another setting that Opcache uses is the maximum number of PHP files that it will cache. At first this may seem repetitive alongside a “max memory” setting, however it truly adds a lot more flexibility to Opcache. For example, maybe you have a 128MB memory limit set but your site has a lot of small PHP files and by the time 128MB usage is exhausted, you’ve somehow cached a whopping 10,000 files, and many of those are not your most hard hit or resource intensive PHP files. So, if you tweak the max files down to something like 5,000, it forces the more often hit files to remain in the Opcache (theoretically, anyway). Most typical WordPress sites don’t cache over a few thousand files on average, I’ve found.
The final important setting is how often you want Opcache to check files for any updated coded, aka the file refresh rate or cache refresh rate (per file, not overall). So on a busy site, this setting could be really effective in decreasing load on your PHP parsing, and thus, your server/CPU. Setting this to around a few minutes is usually safe enough while still being very effective. If you notice issues, you can decrease this number significantly i.e. to no more than a few seconds.
Well, that is pretty much it! You should finish by making sure the Opcache module is activated in PHP5. Then, simply restart the PHP-FPM service (on Nginx servers) or the Apache service (which has PHP embedded).
sudo php5enmod opcache
sudo service php5-fpm restart
As far as monitoring the performance and usage of Opcache, there are several free scripts that work wonderfully. One of my favorite “drop-in” scripts is Opcache Status, or for a solution that works directly inside of the WordPress dashboard, there is a free plugin called Opcache Dashboard as well.
Optimal settings added 25 August 2016:
opcache.enable=1 opcache.enable_cli=1 opcache.memory_consumption=256 opcache.interned_strings_buffer=16 opcache.max_accelerated_files=7963 opcache.max_wasted_percentage=10 ;opcache.use_cwd=1 ;opcache.validate_timestamps=1 opcache.revalidate_freq=90 ;opcache.revalidate_path=0 ;opcache.save_comments=1 opcache.fast_shutdown=1 ;opcache.enable_file_override=0 ;opcache.optimization_level=0xffffffff ;opcache.inherited_hack=1 ;opcache.dups_fix=0 ;opcache.blacklist_filename= ;opcache.max_file_size=0 ;opcache.consistency_checks=0 ;opcache.force_restart_timeout=180 ;opcache.error_log= ;opcache.log_verbosity_level=1 ;opcache.preferred_memory_model= ;opcache.protect_memory=0 ;opcache.restrict_api= ;opcache.mmap_base= ;opcache.file_cache= ;opcache.file_cache_only=0 ;opcache.file_cache_consistency_checks=1 ;opcache.huge_code_pages=1