Do you want to flush PHP Opcache?
It depends on how you are using PHP. However, in this post, you will know how to clear the PHP Opcache.
Find the PHP Method
There are multiple ways to run PHP. With every update, PHP is getting better and offering more and more. There is a CGI way of running, FastCGI, mod_php, and PHP-FPM method.
If you want to flush the PHP, you will have to know the PHP method first; then, you can use a suitable way to clear the OPcache.
However, if you want a general method to flush your Opcache, then you can create a new PHP file called flush_cache.php in the docroot.
Flush_cache.php:
<?php
opcache_reset();
?>
Whenever you want to flush your Opcache, just browse to the flush_cache.php file. The file will call the ocache_reset() for the entire Opcache.
The site will be populated with the cache again with the new PHP request.
One thing to keep in mind is that you should call the URL in the same way you open your website: HTTP or HTTPS.
And you don’t have to worry about the running process, as the execution of the flush_cache.php file does not affect the running processes.
This is the general method, but for a more distinctive approach, keep reading.
CGI or FastCGI Method
If you are running PHP as CGI or FastCGI, there is no point in clearing the OPcache.
Because the cache is already flushing at every request, a new php-cgi process starts with every request when the PHP is running as CGI.
On the other hand, if you force the Opcache for a CGI or FastCGI model, it will hurt the store’s overall performance.
Every request will store the Opcache in the FastCGI process, but that cache is not beneficial as it will destroy the moment the new process begins. It means the Opcache is stored, but there is no way to utilize it. However, the process is eating the CPU.
That’s why CGI is not a recommended way to run PHP.
CLI Method
CLI is kind of similar to CGI when it comes to the Opcache. It will hurt the performance of the site.
First of all, the command line does not have any Opcache. Once you enable it, the PHP will try to store the Opcache in the memory. But then as soon as the CLI commands end, the cache will be dead too.
However, if you want to flush PHP opcache on CLI, you only have to restart the PHP commands.
Simply do the CTRL + C to abort the commands, and start it again.
mod_php Method
You can reload or restart your apache webserver to flush to Opcache is mod_php method.
$ apachectl graceful
$ service httpd reload
A reload will simply clear the Opcache in PHP, while a restart will also do similar work. However, a restart will kill all of the active HTTP connections.
PHP-FPM Method
To flush PHP Opcache on the PHP-FPM method, you have to send a reload to your PHP-FPM daemon. The reload will clear the Opcache, and when the next request arrives, it will force it to rebuild the cache.
$ service php-fpm reload
You can flush the entire cache of all the websites by reloading the single master. The single master will reset the master’s Opcache, and flush the entire cache.
However, tools like cachetool can give you more control over your command line. The cachetool will connect to your PHP-FPM socket, and send the commands similar to the webserver.
For that, first, download the phar to manipulate the cache.
$ curl -sO http://gordalina.github.io/cachetool/downloads/cachetool.phar
Next, 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
Summary
PHP Running Method | How to Flush PHP Opcache? |
General Method | Create flush_cache.php in the docroot and browse it |
CGI or FastCGI Method | Cache is already flushing at every request |
CLI Method | Restart the PHP commands (CTRL + C) |
mod_php Method | Reload or restart your apache webserver |
PHP-FPM Method | Send a reload to your PHP-FPM daemon |
Also Read:
- How to Disable PHP Execution in WordPress Directories?
- Top 12 PHP Based Open Source Ecommerce Platform
- Best PHP Editors and PHP IDE for the Development
Wrapping Up
Opcache is a fantastic way to improve the performance of the site. It stores the precompiled script in shared memory, so there is no need for PHP to load and parse scripts every time.
But sometimes, Opcache does not refresh the updated files in the cache. For that, you have to flush the cache, so it can generate the updated cache.
It is vital for the eCommcerce stores using PHP based platforms.
In this post, I mentioned the ways to flush Opcache, for all the PHP running methods. Check the post on clear WordPress cache.
The ServerGuy customers can request our support team to do this for them.
If there is any problem with any step, leave it in a comment.
1 thought on “How To Flush PHP Opcache?”
Nice post thank you