Skip to content

Instantly share code, notes, and snippets.

@jdeathe
Last active January 11, 2023 23:46
Show Gist options
  • Save jdeathe/57287f5833a94a309d78996fba5168b2 to your computer and use it in GitHub Desktop.
Save jdeathe/57287f5833a94a309d78996fba5168b2 to your computer and use it in GitHub Desktop.
How to Clear PHP Opcache without Restarting PHP-FPM.

PHP CacheTool - Manage cache in the CLI

Use CacheTool to view stats for and manage PHP's APC or Zend Opcache opcode cache.

Using CacheTool you can clear the PHP opcache without reloading PHP-FPM.

In this example, CacheTool is to be installed alongside a demonstration PHP-FPM Docker container.

Prerequisites

Create and Run a PHP-FPM App.

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

Check it works.

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

Request again, or request another URL, to generate some cache usage.

$ curl -I http://localhost:8888/_phpinfo.php

Installation

In the demonstration app PHP files are restricted to /var/www/app by the open_basedir directive so the CacheTool should be installed into the /var/www/app/bin directory with appropriate 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.phar

Basic Operation

Show summary information about the opcode cache

$ 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:status

Available opcache Commands

  • opcache:configuration Get configuration information about the cache
  • opcache:invalidate:scripts Remove scripts from the opcode cache
  • opcache:reset Resets the contents of the opcode cache
  • opcache:status Show summary information about the opcode cache
  • opcache:status:scripts Show scripts in the opcode cache

Discover Usage

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

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment