OPcache Clear - Deployment Pipeline

Introduction

The PHP process on the web server differs from the PHP process in the CLI environment. Because of this, it is not possible to clear the PHP OPcache via the CLI.



Deploying the Clear Script

The following PHP script must be deployed in the document root directory of the Shopware 6 application:


Example path:

/var/www/creoline-demo.com/releases/current/public/opcache.php


opcache.php

<?php

$allowList = [
    '127.0.0.1',
    '10.20.0.1',
    '10.20.0.2',
    '10.20.0.3',
    '10.20.0.4',
];

header('Content-Type: text/plain');

if(!in_array($_SERVER['REMOTE_ADDR'], $allowList)) {
    http_response_code(403);
    echo "The IP address {$_SERVER['REMOTE_ADDR']} is not allowed to execute this script.\n";
    return;
}

$status = opcache_get_status(false);

if(!$status['opcache_enabled']) {
    http_response_code(400);
    echo "OPcache is not enabled.\n";
    return;
}

$memoryUsed = round($status['memory_usage']['used_memory'] / 1024 / 1024);
$memoryFree = round($status['memory_usage']['free_memory'] / 1024 / 1024);

if(opcache_reset()) {
    http_response_code(200);
    echo "OpCache has been cleared successfully. Used memory: {$memoryUsed} MiB - Free memory: {$memoryFree} MiB\n";
} else {
    http_response_code(400);
    echo "OpCache could not be cleared.\n";
}


The IP addresses in AllowList must be updated to match the VPC IP addresses of the app servers.



Deployment Integration

In the final step, after the symlink flip, the script must be run from each app server as follows:

curl -k --fail-with-body --retry 3 --retry-all-errors http://127.0.0.1/opcache.php -H "Host: www.creoline-demo.com"


The Host header must be adjusted to the corresponding domain specified in the NGINX vhost configuration. If no domain was specified, the Host header can be omitted.



Error Handling

Success

  • cURL exit code: 0
  • The OPcache was successfully cleared


Error

  • cURL exit code: 22
  • The OPcache could not be cleared even after 3 attempts. See the HTTP body for error analysis.
Similar articles