Fixed enable and disable opcache

main
Anthony 2025-06-17 01:02:24 +08:00
parent a2021a3e35
commit 458c89312e
2 changed files with 96 additions and 31 deletions

View File

@ -172,6 +172,21 @@ menu:
action: opcache_update_settings action: opcache_update_settings
settings: opcacheConfig settings: opcacheConfig
successText: "${response.out}" 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? - confirmText: Do you want to export the WordPress database?
loadingText: Exporting WordPress Database... loadingText: Exporting WordPress Database...
caption: Export WordPress DB caption: Export WordPress DB
@ -444,18 +459,24 @@ actions:
user: root user: root
commands: commands:
- php /home/litespeed/mbmanager/scripts/toggle_opcache.php enable - php /home/litespeed/mbmanager/scripts/toggle_opcache.php enable
- restartNodes:
- nodeGroup: "cp"
reboot: true
- return: - return:
type: info type: info
message: "${response.out}" message: "OPCache enabled successfully and PHP restarted."
disable_opcache: disable_opcache:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: commands:
- php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable - php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable
- restartNodes:
- nodeGroup: "cp"
reboot: true
- return: - return:
type: info type: info
message: "${response.out}" message: "OPCache disabled successfully and PHP restarted."
clear_opcache: clear_opcache:
- cmd[cp]: - cmd[cp]:

View File

@ -26,24 +26,89 @@ function get_opcache_config() {
return $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 // Handle different actions
switch ($action) { switch ($action) {
case 'enable': case 'enable':
// Enable OPCache // Enable OPCache
if (!ini_set('opcache.enable', '1')) { if (!modify_opcache_config(true)) {
echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache']); echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache - could not modify configuration files']);
exit(1); exit(1);
} }
$message = 'OPCache enabled successfully'; $message = 'OPCache enabled successfully. PHP restart required to take effect.';
break; break;
case 'disable': case 'disable':
// Disable OPCache // Disable OPCache
if (!ini_set('opcache.enable', '0')) { if (!modify_opcache_config(false)) {
echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache']); echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache - could not modify configuration files']);
exit(1); exit(1);
} }
$message = 'OPCache disabled successfully'; $message = 'OPCache disabled successfully. PHP restart required to take effect.';
break; break;
case 'status': case 'status':
@ -76,29 +141,8 @@ switch ($action) {
exit(0); exit(0);
} }
// Get status after action // For enable/disable actions, return success message
$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);
}
echo json_encode([ echo json_encode([
'status' => 'success', 'status' => 'success',
'message' => $message, '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']
]
]
], JSON_PRETTY_PRINT); ], JSON_PRETTY_PRINT);