PHP cache incredible increases the speed of a site. For instance, if request preparation takes 1.2 seconds, after cache enabling it will take 0.6 seconds (increase by about 50%). Particularly, it influences on WordPress significantly as well because it contains a lot of PHP code.
On some sites based on e.g. WAMP (Apache, MySQL and PHP bundle for Windows) PHP code cache is turned off by default so it’s better for us to check the settings.
The most popular way to do that is to use OpCache – a special PHP module that does code caching. In PHP 5.5 and above this module already integrated into.
How it works
OpCache provides faster PHP execution through compiled code caching and optimization. It improves PHP performance by storing precompiled script bytecode in the shared memory. Every time a PHP script is requested, the PHP script will be parsed and compiled into opcode, which then is executed in the engine. When an opcode cache is introduced, after a PHP script is interpreted and turned into opcode (compiled), it’s saved in shared memory, and subsequent requests will skip the parsing and compilation phases and leverage the opcode stored in memory, reducing the script’s execution time.
Configuration mainly is done by editing a special file named php.ini.
Locate configuration file
The location is depend on a system and can be like /etc/php5/apache2/php.ini or /etc/php5/fpm/php.ini, /usr/local/lib/php.ini on Unix-similar systems and C:\wamp\bin\php\php5.5.12\php.ini on Windows.
In WAMP there is a special menu item that opens it.
Also, a file’s location can always be discovered from PHP info. Just create phpinfo.php file with content <?php phpinfo(); ?>, copy it to the web server’s root directory and run it by http://localhost/phpinfo.php. The location is in the Configuration File (php.ini) Path section of the first block (look at the picture below).
All settings are located in [opcache] section of php.ini.
Ensure that the main settings exist there:
Other additional settings can be left untouched with default values. But sometimes it’s required to tune them. So here is a short description of each important one with recommended values.
opcache.enable = 1
Enables or disables the cache. The main setting.
opcache.use_cwd = 1
Eliminates possible collisions between files with the same base name.
opcache.validate_timestamps = 1
Enables runtime validation of script files changes. It’s recommended for use if the site is being changed with different PHP scripts. E.g. plugins installation in WordPress. But if there is insurance that all set of scripts is stable, this setting can be turned off to increase a little bit of runtime execution.
opcache.revalidate_freq = 2
How often to check script changes, in seconds. 0 will result in checking on every request. It doesn’t work if opcache.validate_timestamps is disabled.
opcache.max_accelerated_files = 10000
The maximum number of cached files. Bigger values recommended for bigger sites with a huge amount of scripts and installed memory.
The actual parameters can always be checked at the OPcache source configuration page and in the list of all parameters.
Restart web service
Just restart Web service (e.g. Apache) and PHP will be restarted too.
In WAMP it can be done by restart Apache.
The obvious way to check it is to compare server response before and after turning on a cache. E.g. we can do that by Google Chrome browser.
Another way to check that OpCache is working is to create a file opcache.php with content <pre><?php print_r(opcache_get_status());?></pre>, copy it to the web server’s root directory and run it by http://localhost/opcache.php. The sample result in the picture below.
The value of opcache_enabled should be 1. And lower there is PHP scripts statistics can be viewed like total count and detailed info for each one.
If the site’s performance is not enough it’s possible to tune up WordPress by cache plugins.
More information can be discovered in the original post about Increase site’s speed by enable PHP code caching and on the main site.