How To Clear PHP’s Opcache

PHP can be configured to store precompiled bytecode in shared memory, called Opcache. It prevents the loading and parsing of PHP scripts on every request. This guide will tell you how to flush that bytecode Opcache, should you need it.

You may want to flush the APC (PHP < 5.5) or Opcache (PHP >= 5.5) in PHP when it has cached code you want to refresh. As of PHP 5.5, the APC cache has been replaced by Opcache and APC only exists as a user key/value cache, no longer a bytecode cache.

Determine your PHP method

You can run PHP in multiple ways. The last few years, PHP has evolved into new methods, ranging from CGI to FastCGI to mod_php and PHP-FPM. Flushing your Opcache depends on how you run PHP.

If you want a uniform way of flushing your Opcache, you can create a PHP file called flush_cache.php in your docroot with content like this.<?php opcache_reset(); ?>

Every time you want to flush your Opcache, you can browse to that file and it’ll call opcache_reset(); for your entire Opcache. The next PHP request to your site will populate the cache again.

It’s important that you call that URL in the same way you would reach your website, either via a HTTP:// or HTTPS:// URL. Running php flush_cache.php at the command line won’t flush the cache of your running processes.

This can be part of your deployment process, where after each deploy you curl that particular URL.

If you want a server-side solution, check further.

PHP running as CGI or FastCGI

Flushing the Opcache on CGI or FastCGI PHP is super simple: it can’t be done.

Not because you can’t flush the cache, but because the cache is flushed on every request anyway. FastCGI starts a new php-cgi process on every request and does not have a parent PHP process to store the Opcache results in.

In fact, having Opcache running in a CGI or FastCGI model would hurt performance: on every request the Opcache is stored in the FastCGI process (default behaviour if the Opcache extension activated), but that cache is destroyed as soon as that process dies after finishing the request.

Storing the Opcache takes a few CPU cycles and is an effort that cannot be benefited from again later.

CGI or FastCGI is about the worst possible way to run your PHP code.

PHP running at the CLI

All PHP you run at the command line has no Opcache. It can be enabled, and PHP can attempt to store its Opcache in memory, but as soon as your CLI command ends, the cache is gone as well.

To clear the Opcache on CLI, just restart your PHP command. It’s usually as simple as CTRL+C to abort the command and start it again.

For the same reason as running PHP as CGI or FastCGI above, having Opcache enabled for CLI requests would hurt performance more than you would gain benefits from it.

Apache running as mod_php

If you run Apache, you can run PHP by embedding a module inside your Apache webserver. By default, PHP is executed as the same user your Apache webserver is running.

To flush the Opcache in a mod_php scenarion, you can either reload or restart your Apache webserver.$ service httpd reload $ apachectl graceful

A reload should be sufficient as it will clear the Opcache in PHP. A restart will also work, but is more invasive as it kills all active HTTP connections.

PHP running as PHP-FPM

If you run your PHP as PHP-FPM, you can send a reload to your PHP-FPM daemon. The reload will flush the Opcache and force it to be rebuilt on the first incoming request.$ service php-fpm reload

If you are running multiple PHP master, you can reload a single master to only reset that masters’ Opcache. By default, it will flush the entire cache, no matter how many websites you have running.

If you want more control at the command line, you can use a tool like cachetool that can connect to your PHP-FPM socket and send it commands, the same way a webserver would.

First, download the phar that you can use 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

Using something like cachetool can also be easily integrated in your automated deploy process.

3 thoughts on “How To Clear PHP’s Opcache

Leave a Reply

Your email address will not be published. Required fields are marked *

RELATED POST

How to automatically restart Linux services with Systemd

Getting your Linux deployments working reliably is of paramount concern for production applications. One way to guarantee that a service…

How to Install SFTPGo on Ubuntu 22.04

Setup UFW Firewall For security reasons, it is recommended to install and configure the UFW firewall in your system. First,…

Executing Bash Scripts at Startup in Ubuntu Linux

Creating a Bash script in Ubuntu To create a Bash script in Ubuntu, you can use any text editor of…

iLO port configuration on HP servers + license

HP Integrated Lights-Out (iLO) Advanced License 34RQQ-6D5R4-G8NW3-LBMG6-MLJXR325WC-J9QJ7-495NG-CP7WZ-7GJMM iLO 2 Advanced features have been activated License Key: License Key: 35DPH-SVSXJ-HGBJN-C7N5R-2SS4W32Q8Y-XZVGQ-4SGJB-4KY3R-M9ZBN –…