Bobcares

Clear PHP OPcache Without Restarting PHP-FPM | With CacheTool

by | Sep 14, 2022

Let’s see how to clear PHP OPcache without restarting PHP-FPM in this article. At Bobcares, with our Server Management Services, we can handle your server issues.

How to clear PHP OPcache without restarting PHP-FPM?

We can now easily clear the PHP Opcache without restarting PHP-FPM with the help of CacheTool. In this article, we will provide the instructions to install the CacheTool alongside a demonstration PHP-FPM Docker container.

 

clear php opcache without restarting php-fpm

Installing CacheTool

  • Firstly, we need to create and Run a PHP-FPM App.
  • Then install the CacheTool.
  •  Basic Operation resets the contents of the opcode cache.
  • Lastly, confirm OPcache is empty.
Create And Run A PHP-FPM App

On the CLI, run the following command:

$ docker run \
  --detach \
  --name php56 \
  --publish 8888:80 \
  jdeathe/centos-ssh-apache-php:2.1.1Copy Code

Now to verify that it functions.

$ curl -I http://localhost:8888/Copy Code

In order to create some cache usage, make another request. We can also try a different URL.

$ curl -I http://localhost:8888/_phpinfo.phpCopy Code
Install The CacheTool

The

open_basedirCopy Code
directive in the demonstration app restricts PHP files to the
/var/www/appCopy Code
directory, thus the CacheTool should be installed to
/var/www/app/binCopy Code
with the proper permissions.

$ docker exec -i php56 curl -s \
  -o /var/www/app/bin/cachetool.phar \
  http://gordalina.github.io/cachetool/downloads/cachetool.phar
$ docker exec -i php56 chmod 750 /var/www/app/bin/cachetool.phar
$ docker exec -i php56 chown app:app /var/www/app/bin/cachetool.pharCopy Code
Basic Operation

With the following code, we can clear the contents of the opcode cache without restarting PHP-FPM.

$ docker exec -i php56 \
  php /var/www/app/bin/cachetool.phar \
  --tmp-dir=/var/www/app/var/tmp \
  --fcgi=/var/run/php-fpm/app-www.sock \
  opcache:resetCopy Code
Verify OPcache Is Empty

We have to include the command in the composer post install/update scripts to ensure that the OPcache is cleared after each deployment without restarting PHP-FPM. In this manner, OPcache will also update its cache each time we install the dependencies:

"post-install-cmd": [
        // ...
        "cachetool opcache:reset --fcgi=/var/run/php5-fpm.sock"
    ]
Copy Code

Identify Usage

In order to explore the CacheTool usage, use the following code:

$ docker exec -i php56 \
  php /var/www/app/bin/cachetool.phar helpCopy Code

Other OPcache Commands

1.

opcache:configurationCopy Code
: Access the cache’s configuration data.

2.

opcache:invalidate:scriptsCopy Code
: Clear the scripts in the opcode cache.

3.

opcache:resetCopy Code
: Resets the opcode cache’s contents.

4.

opcache:statusCopy Code
: Display a summary of the opcode cache’s information.

5.

opcache:status:scriptsCopy Code
: Display opcode cache’s scripts.

[Need assistance with another issue? We’re here to help.]

Conclusion

In this article, we have included the steps from our Support team to install PHP CacheTool, along with the code to find its usage and also available commands with the Opcache.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

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

Speed issues driving customers away?
We’ve got your back!