'error', 'message' => 'OPCache is not installed']); exit(1); } // Function to get current OPCache status function get_opcache_status() { $status = opcache_get_status(false); if ($status === false) { return false; } return $status; } // Function to get OPCache configuration function get_opcache_config() { $config = opcache_get_configuration(); if ($config === false) { return false; } return $config; } // Function to find PHP configuration files function find_php_config_files() { $possible_paths = [ '/etc/php.d/', '/etc/php/8.4/cli/conf.d/', '/etc/php/8.3/cli/conf.d/', '/etc/php/8.2/cli/conf.d/', '/etc/php/8.1/cli/conf.d/', '/etc/php/8.0/cli/conf.d/', '/usr/local/lsws/lsphp/etc/php.d/', '/usr/local/lsws/lsphp84/etc/php.d/', '/usr/local/lsws/lsphp83/etc/php.d/', '/usr/local/lsws/lsphp82/etc/php.d/', '/usr/local/lsws/lsphp81/etc/php.d/', '/usr/local/lsws/lsphp80/etc/php.d/' ]; $possible_files = [ '/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini', '/etc/php.d/10-opcache.ini' ]; $config_files = []; foreach ($possible_files as $file) { if (is_file($file)) { $config_files[] = $file; } } $loaded_ini = php_ini_loaded_file(); if ($loaded_ini !== false && is_file($loaded_ini)) { $content = file_get_contents($loaded_ini); if ($content !== false && preg_match('/^\s*;?\s*opcache\.enable\s*=\s*/mi', $content)) { $config_files[] = $loaded_ini; } } $scanned_ini = php_ini_scanned_files(); if ($scanned_ini !== false && trim($scanned_ini) !== '') { foreach (explode(',', $scanned_ini) as $ini_file) { $ini_file = trim($ini_file); if ($ini_file === '' || !is_file($ini_file)) { continue; } if (stripos(basename($ini_file), 'opcache') !== false) { $config_files[] = $ini_file; } } } foreach ($possible_paths as $path) { if (is_dir($path)) { $files = glob($path . '*opcache*'); if (!empty($files)) { $config_files = array_merge($config_files, $files); } } } return array_values(array_unique($config_files)); } // Function to modify OPcache configuration function modify_opcache_config($enable) { $config_files = find_php_config_files(); if (empty($config_files)) { return false; } $success = false; foreach ($config_files as $file) { if (!is_file($file)) { continue; } $content = file_get_contents($file); if ($content === false) { continue; } $replacement_count = 0; $updated_content = preg_replace( '/^\s*;?\s*opcache\.enable\s*=\s*.*$/mi', 'opcache.enable=' . ($enable ? '1' : '0'), $content, -1, $replacement_count ); if ($updated_content === null) { continue; } // If no opcache.enable line exists, add it once. if ($replacement_count === 0) { $updated_content = rtrim($updated_content) . "\nopcache.enable=" . ($enable ? '1' : '0') . "\n"; } if (file_put_contents($file, $updated_content) !== false) { $success = true; } } return $success; } // Handle different actions switch ($action) { case 'enable': // Enable OPCache if (!modify_opcache_config(true)) { echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache - could not modify configuration files']); exit(1); } $message = 'OPCache enabled successfully. PHP restart required to take effect.'; break; case 'disable': // Disable OPCache if (!modify_opcache_config(false)) { echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache - could not modify configuration files']); exit(1); } $message = 'OPCache disabled successfully. PHP restart required to take effect.'; break; case 'status': default: // Get current status $status = get_opcache_status(); $config = get_opcache_config(); if ($status === false || $config === false) { echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']); exit(1); } echo json_encode([ 'status' => 'success', 'data' => [ 'enabled' => $config['directives']['opcache.enable'], 'memory_usage' => [ 'used' => $status['memory_usage']['used_memory'], 'free' => $status['memory_usage']['free_memory'], 'wasted' => $status['memory_usage']['wasted_memory'] ], 'statistics' => [ 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], 'hits' => $status['opcache_statistics']['hits'], 'misses' => $status['opcache_statistics']['misses'] ] ] ], JSON_PRETTY_PRINT); exit(0); } // For enable/disable actions, return success message echo json_encode([ 'status' => 'success', 'message' => $message ], JSON_PRETTY_PRINT);