In this blog post, you will get to know how to flush PHP Opcache.
PHP can be customized to save precompiled bytecode in shared memory named Opcache. It restricts the parsing and loading of PHP scripts on each request.
Determine the PHP Method
There are several ways to execute or run PHP. The PHP is becoming better and offering more with each update. Some of the methods to run PHP are FastCGI, mod_php, and PHP-FPM method.
To flush the PHP Opcache, you will need to determine the PHP method initially so you could use the best fit method to clear the Opcache.
In case you need a uniform way to flush Opcache then you are required to make a new PHP file named flush_cache.php in the docroot.
Flush_cache.php:
<?php
opcache_reset();
?>
Simply browse the flush_cache.php file, while you need to clear the Opcache. The file will call the ocache_reset() for the whole Opcache.
As the execution of the flush_cache.php files does not impact the running process, you don’t need to worry about the running process
This is a common way, but for a more unique approach, keep reading.
CGI or Fast CGI Method
There is no point in clearing the OP cache if you are running PHP as CGI or Fast CGI.
As the cache is already flushing at each request, a new PHP-CGI process begins with each request.
With each request, the Opcache will be saved in the FastCGI process, though that cache is not useful as it will destroy the instant when a new process starts. It indicates the Opcache is saved, but, there is no way to use it. However, the process is eating the CPU.
So that’s the reason why CGI is not a recommended method to run PHP.
PHP-FPM Method
To clear the PHP Opcache on the PHP-FPM method, you need to transfer a reload to your PHP-FPM daemon. The reload that you sent will flush the Opcache, and on the arrival of the next request, it will compel it to rebuild the cache.
$ service php-fpm reload
By reloading the single master you can clear the whole cache of all the websites. The single master will reset the master’s Opcache and clear the complete cache.
Nevertheless, tools like cachetool can provide you with more extra power over your command line. The cachetool will connect to your PHP-FPM socket, and transfer or send the commands related to the webserver.
You need to initially, download the phar to control the cache.
$ curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
The next step is to use that phar to send commands to your PHP-FPM daemon.
$ php cachetool.phar opcache:reset --fcgi=127.0.0.1:9000
$ php cachetool.phar opcache:reset --fcgi=/var/run/php5-fpm.sock
PHP Opcache CLI Method
CLI is similar to CGI when it comes to the Opcache. The performance of the site will be hurt.
Usually, the command line does not have any Opcache. Once you allow it, the PHP will try to store the Opcache in the memory. However, as soon as the CLI commands are complete, the cache will be expiring as well.
You need to just restart the PHP commands if you want to flush PHP Opcache on CLI.
Press CTRL + C to abort the commands, and restart them.
mod_php Method
You can reload or restart your apache webserver to clear Opcache with mod_php_method.
$ apachectl graceful
$ service httpd reload
A restart or reload will flush the Opcache in PHP.
If 10GB Hosting clients face any problems regarding slow website speeds then they can request our support team to clear the cache for them.