From 458c89312e16e3a2299bece820f36201ca9684ed Mon Sep 17 00:00:00 2001 From: Anthony Date: Tue, 17 Jun 2025 01:02:24 +0800 Subject: [PATCH] Fixed enable and disable opcache --- mbadmin.jps | 25 ++++++++- scripts/toggle_opcache.php | 102 ++++++++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/mbadmin.jps b/mbadmin.jps index 2217004..44d612b 100644 --- a/mbadmin.jps +++ b/mbadmin.jps @@ -172,6 +172,21 @@ menu: action: opcache_update_settings settings: opcacheConfig successText: "${response.out}" + - confirmText: Are you sure you want to enable OPcache? + loadingText: Enabling OPcache and restarting PHP... + caption: Enable OPcache + action: enable_opcache + successText: "${response.out}" + - confirmText: Are you sure you want to disable OPcache? + loadingText: Disabling OPcache and restarting PHP... + caption: Disable OPcache + action: disable_opcache + successText: "${response.out}" + - confirmText: Are you sure you want to clear OPcache? + loadingText: Clearing OPcache... + caption: Clear OPcache + action: clear_opcache + successText: "${response.out}" - confirmText: Do you want to export the WordPress database? loadingText: Exporting WordPress Database... caption: Export WordPress DB @@ -444,18 +459,24 @@ actions: user: root commands: - php /home/litespeed/mbmanager/scripts/toggle_opcache.php enable + - restartNodes: + - nodeGroup: "cp" + reboot: true - return: type: info - message: "${response.out}" + message: "OPCache enabled successfully and PHP restarted." disable_opcache: - cmd[cp]: user: root commands: - php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable + - restartNodes: + - nodeGroup: "cp" + reboot: true - return: type: info - message: "${response.out}" + message: "OPCache disabled successfully and PHP restarted." clear_opcache: - cmd[cp]: diff --git a/scripts/toggle_opcache.php b/scripts/toggle_opcache.php index 3f0c0a2..3053f39 100644 --- a/scripts/toggle_opcache.php +++ b/scripts/toggle_opcache.php @@ -26,24 +26,89 @@ function get_opcache_config() { 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/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/' + ]; + + $config_files = []; + foreach ($possible_paths as $path) { + if (is_dir($path)) { + $files = glob($path . '*opcache*'); + if (!empty($files)) { + $config_files = array_merge($config_files, $files); + } + } + } + + return $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_writable($file)) { + $content = file_get_contents($file); + if ($content !== false) { + // Replace opcache.enable setting + if ($enable) { + $content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); + $content = preg_replace('/^;opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); + } else { + $content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=0', $content); + } + + // If no opcache.enable line exists, add it + if (!preg_match('/^opcache\.enable\s*=/m', $content)) { + $content .= "\nopcache.enable=" . ($enable ? '1' : '0') . "\n"; + } + + if (file_put_contents($file, $content) !== false) { + $success = true; + } + } + } + } + + return $success; +} + // Handle different actions switch ($action) { case 'enable': // Enable OPCache - if (!ini_set('opcache.enable', '1')) { - echo json_encode(['status' => 'error', 'message' => 'Failed to 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'; + $message = 'OPCache enabled successfully. PHP restart required to take effect.'; break; case 'disable': // Disable OPCache - if (!ini_set('opcache.enable', '0')) { - echo json_encode(['status' => 'error', 'message' => 'Failed to 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'; + $message = 'OPCache disabled successfully. PHP restart required to take effect.'; break; case 'status': @@ -76,29 +141,8 @@ switch ($action) { exit(0); } -// Get status after action -$status = get_opcache_status(); -$config = get_opcache_config(); - -if ($status === false || $config === false) { - echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status after action']); - exit(1); -} - +// For enable/disable actions, return success message echo json_encode([ 'status' => 'success', - 'message' => $message, - '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'] - ] - ] + 'message' => $message ], JSON_PRETTY_PRINT); \ No newline at end of file