In previous tutorials I explained how to setup a WordPress hosting environment that is fast because of being hosted using NGINX with PHP 7 and NGINX Proxy Caching. In this tutorial I will show you how to increase the performance even more by enabling PHPs Zend OPcache feature.
Zend OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.
This tutorial will show you how to install and configure Zend OPcache for PHP-FPM (PHP 7) with NGINX.
- Step 1: Install PHP-FPM Zend OPCache.
- Step 2: Configure Zend OPCache.
- Step 3: Test Zend OPCache is Enabled.
Step 1: Install PHP-FPM Zend OPcache
If you haven’t already installed PHP 7, you can learn how by following the previous tutorials on how to setup a WordPress hosting environment.
With PHP installed you can now run the following command to install the Zend OPcache extension:
sudo apt-get install php7.0-opcache
The Zend OPcache extension should now be installed and ready to configure.
Step 2: Configure Zend OPcache
Open the file:
/etc/php/7.0/fpm/php.ini with your favourite text editor.
sudo vim /etc/php/7.0/fpm/php.ini
Make the following changes to the directives located near the bottom of the file in the opcache section.
opcache.enable=1 opcache.validate_timestamps=1 opcache.revalidate_freq=60 opcache.max_accelerated_files=10000 opcache.memory_consumption=64 opcache.interned_strings_buffer=8 opcache.fast_shutdown=1
Save the file and then run the following command to apply the changes.
sudo /etc/init.d/php7.0-fpm restart
The Zend OPcache extension should now be configured and caching PHP scripts hosted by your websites. We can check it’s enabled by running a custom PHP script. We will do this in Step 3 but first let’s go over what the directives mean.
Setting this directive to 1 will enable the Zend OPcache. When set to 0, it will disable the cache.
Setting this directive to 1 will make PHP check the timestamps of each file to see if it has been modified and if so it will update the cache for that file. When set to 0 it will not update each files cache and you will need to restart PHP each time you modify a file.
How often (in seconds) to check file timestamps for changes to the shared memory storage allocation. (“1” means validate once per second, but only once per request. “0” means always validate).
The maximum number of scripts that can be stored in the cache. Only numbers between 200 and 1000000 are allowed. This should be higher than the amount of scripts you have in your hosting directories.
Set this directive to the maximum amount of memory in MB the cache should use. You will be able to see how much is being used using the statics script in Step 3 but for a WordPress installation with minimum modules 64 MB should be high enough.
PHP uses a technique called string interning that reduces memory and improves performance by storing duplicate strings in the code in a single variable. Set this directive to 8 MB and check the stats in the Step 3 to see how much higher you need to increase it by.
If enabled, a fast shutdown sequence is used for the accelerated code. Depending on the used memory manager this may cause some incompatibilities. Set this to 0 if you experience problems.
Step 3: Test Zend OPCache is Enabled
We can test to see if the OPCache is enabled by creating a PHP file and using the
opcache_get_status function. This function shows you lots of useful information like what scripts have been cached and how much memory has been used etc.
To save time there is an open source project called opcache-gui that displays all the stats on a nicely formatted page. The code is all contained in a single index.php file. To get started run the following command to download the file to your www root directory.
sudo curl -o /var/www/opcache.php https://raw.githubusercontent.com/amnuts/opcache-gui/master/index.php
Assuming your web document root is located at
/var/www the file will be downloaded to there with the name opcache.php
Browse to the file using a web browser and you should see the following output.
As you can see from the image above, the opcache is enabled because the enable directive is set to true and you can see 57% of the configured memory is being used. You can use this screen to tweak the settings in step 2. You can also reset the cache from this page or you can look at what scripts are cached and reset each on individually.
In this tutorial you learned how to increase the performance of WordPress or any other PHP application by installing and enabling the Zend OPcache extension. Enabling this feature, as well as NGINX Proxy Caching will drastically increase the speed of your WordPress site.