fbpx

OPcache Reset for WordPress

To further enhance WordPress performance, especially for websites with high traffic or large-scale deployments, it is recommended to run PHP-FPM with the setting opcache.validate_timestamps = Off. However, it requires clearing OPcache explicitly. In this situation the cachetool utility can be a game-changer. It is a command-line utility designed to clear OPcache and other caches directly from the terminal.

For a perfect WordPress PHP-FPM performance you need to use OPcache Reset WordPress plugin. This runs cachetool for you in order to automatically clear OPcache after updates of WordPress core, themes, and files.

Unlike other plugins, it is also compatible with WordPress updates made by Linux cron.

Here’s a short guide on the optimal setup for the plugin.

1. Install and configure the cachetool

The cachetool is a little CLI utility that is capable of talking to your PHP-FPM instance in order to clear PHP OPcache.

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install cachetool

Configuring cachetool

To set up cachetool, especially when utilizing the OPCache Reset plugin, a specific configuration file, .cachetool.yml, needs to be created in the website’s root directory. This configuration ensures that cachetool communicates correctly with the PHP-FPM process using its socket and specifies a temporary directory for its operations. Here’s how to do it.

Navigate to the root directory of your WordPress installation and create a new file named .cachetool.yml. This file should contain the following configuration:

adapter: fastcgi
fastcgi: /var/run/php-fpm/example.sock
temp_dir: /dev/shm/cachetool

This configuration specifies that cachetool should use the FastCGI adapter to communicate with PHP-FPM. The fastcgi option points to the socket file of the PHP-FPM process, which in this example is /var/run/php-fpm/example.sock. This is the path you need to change to your own, it should match with the UNIX socket specified in your PHP-FPM settings.

The temp_dir option sets a temporary directory for CacheTool’s operations, /dev/shm/cachetool, which resides in shared memory for faster access.

Understanding the configuration options

  • adapter: This defines the method CacheTool uses to communicate with PHP. fastcgi is typically used for PHP-FPM setups.
  • fastcgi: Specifies the path to the PHP-FPM socket. This path may vary based on your server configuration, so adjust it according to your PHP-FPM setup.
  • temp_dir: A temporary directory for CacheTool’s operations. Using /dev/shm (shared memory) is recommended for its faster read/write speeds.

2. Set up opcache.validate_timestamps setting

The most efficient way to run PHP scripts of any kind in production: opcache.validate_timestamps set to Off.

In this way, any scripts are not checked for changes. This saves a lot of disk activity for PHP frameworks which have to parse dozens and dozens of scripts on every request.

Set this in your PHP-FPM configuration, or global PHP configuration and when you invoke PHP from cron:

opcache.validate_timestamps=0

Learn more.

3. WP-CLI

sudo yum -y install wp-cli

Install the plugin using WP-CLI:

wp plugin install opcache-reset --activate

That’s it. Zero configuration is required.