OPcache caching system is a great way to speed up your WordPress website. It saves precompiled bytecode in the server which saves bandwidth and server resources. But there are times when caching create problems. For example, when you update the server code, the user may not see the changes on the browser because they’re still using the bytecode saved in the cache. To refresh the code, you need to flush Opcache. In this article, we learn step-by-step how you can clear PHP OPcache.
Create flush_cache.php File
To flush PHP Opcache uniformly, create a php file and name it flush_cache.php in the docroot. Place this code inside of it:
When you need to clear Opcache, simply navigate to this file and the file will call the php opcache_reset() function. You don’t have to care about the process since the execution of the file doesn’t interfere with it.
Determine the PHP Running Method
The exact process depends on how PHP runs in your WordPress system. Thus, you need to first determine the PHP method. Then you need to select an appropriate method for clearning OPcache.
You may also note that CGI or FastCGI when used for running Opcache will degrade performance since Opcache is in the FastCGI process (that’s the given process if you’ve enabled Opcache). But the cache is deleted after the request is completed and process terminates itself.
If you want to store Opcache, it’d require a few CPU cycles. This can’t be repaid later.
So in this case, all you have to do is restart the PHP command. Press CTRL+C to abort. Then restart command. It should do the job for you.
Wherever possible, you should opt for reload over restart. That’s because restarting Apache webserver will terminate all HTTP connections as well, thus creating problems.
So that’s how you can clear PHP Opcache.
Create flush_cache.php File
To flush PHP Opcache uniformly, create a php file and name it flush_cache.php in the docroot. Place this code inside of it:
Code:
<?php
opcache_reset();
?>
Determine the PHP Running Method
The exact process depends on how PHP runs in your WordPress system. Thus, you need to first determine the PHP method. Then you need to select an appropriate method for clearning OPcache.
- PHP running as CGI/FastCGI
You may also note that CGI or FastCGI when used for running Opcache will degrade performance since Opcache is in the FastCGI process (that’s the given process if you’ve enabled Opcache). But the cache is deleted after the request is completed and process terminates itself.
If you want to store Opcache, it’d require a few CPU cycles. This can’t be repaid later.
- PHP running at CLI
So in this case, all you have to do is restart the PHP command. Press CTRL+C to abort. Then restart command. It should do the job for you.
- Apache running as mod_php
Code:
$ service httpd reload
$ apachectl graceful
- PHP running as PHP-FPM
Code:
$ service php-fpm reload