Clearing PHP OPcache: A Complete Guide

Clearing PHP OPcache: A Complete Guide

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:

Code:
<?php
opcache_reset();
?>
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.
  • PHP running as CGI/FastCGI
If you’re using CGI or FastCGI for running Opcache, then the process of clearing PHP Opcache is quite simple. That’s because cache gets cleared on each and every request. FastCGI will conjure a new php-cgi process per request.

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
You don’t have Opcache is any of the PHP scripts that you execute from the CLI. But PHP may store Opcache in memory but if you enable Opcache extension. Then the cache will get deleted and removed when you terminate the CLI command.

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
Apache supports execution of PHP process if you embed the required module in the Apache webserver. In most cases, PHP is run by the user that runs Apache too. In case of mod_php, you need to restart or reload Apache to clear Opcache. Here’s what to execute:

Code:
$ service httpd reload
$ apachectl graceful
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.
  • PHP running as PHP-FPM
FPM or FastCGI Process Manager can also be used for clearing PHP Opcache. You’re required to send a reload to PHP-FPM daemon and it’ll handle the rest. This requires you to run PHP as PHP-FPM. When it reloads, it’ll cause Opcache to rebuild and flush itself. Here’s what you need to execute:

Code:
$ service php-fpm reload
So that’s how you can clear PHP Opcache.
Author
kumkumsharma
Views
5,729
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top