An Operational Code Cache (like PHP OPCache) can speed up page compilation significantly. You can verify if OPCache is installed by running php -i | grep opcache
on the command line of your server.
If it’s not installed, you should install it lie this: sudo apt install php-opcache
.
The settings that have worked smoothly for me with WordPress are:
/etc/php/<version>/fpm/php.ini[opcache] opcache.enable=1 opcache.fast_shutdown=1 opcache.file_cache_consistency_checks=0 opcache.file_update_protection=0 opcache.interned_strings_buffer=8 opcache.jit=tracing ; PHP8 only opcache.jit_buffer_size=100M ; PHP8 only opcache.max_accelerated_files=7963 opcache.max_wasted_percentage=10 opcache.memory_consumption=192 opcache.revalidate_freq=0 opcache.save_comments=0 ; might conflict if you use PHP Annotations opcache.validate_timestamps=0 opcache.validate_permission=0
Let’s take a look at a couple of important lines here: as you can see, I don’t revalidate a files’ timestamp by setting opcache.revalidate_freq=0
. This is done on purpose, because in modern setups PHP files don’t usually change once they are deployed. See Part 4 (Setting up Continuous Integration with GitHub actions) of this article series where I show you how to do atomic deploys. This results in a new physical file and thus path upon every deploy. So no need to verify paths.
The OPCache JIT compiler is somewhat new and available only in PHP 8.0 upwards. What this does is basically translate opcodes into machine code which, bottom line, is supposed to speed things up for us. So let’s activate it as seen above.
All settings with the word validate, check and protection in it, is set to disabled (0) because we don’t want to add extra security and thus cycles but we want maximum performance.
The opcache.save_comments=0
does exactly what you think it does, it removes all comments from OPCodes, so they require less memory. One important note is that this will also remove any PHP Annotations you have in your PHP classes such as Doctrine or Symfony annotations, which can get you into trouble later on. But since we are in a WordPress context where it is very very unlikely that annotations are used, you can set this to 0 without having any fear.
And if you happen to use PHP Annotations (which you shouldn’t) you can prevent possible problems by running required code compilation in your build process. Alternatively you can switch to a different way of marking up Doctrine Models with an XML format for example.
I recommend using this excellent one-file OPCache status monitor to visualise the usage of you OPCache memory. You want little misses, and many hits. If your OPCache Memory consumption is very high, for example above 90%, then you should increase the opcache.memory_consumption
value. Another alternative is the excellent OPCache GUI which basically does the same.
Continue to part 13 of this tutorial series: Deactivate the REST API