mb-admin/scripts/toggle_opcache.php

40 lines
1.3 KiB
PHP
Raw Normal View History

2025-06-13 17:17:12 +00:00
<?php
$action = $argv[1] ?? 'status';
$ini_file = '/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini';
if (!file_exists($ini_file)) {
echo json_encode(['status' => 'error', 'message' => 'OPCache configuration file not found']);
exit(1);
}
$ini_content = file_get_contents($ini_file);
if ($ini_content === false) {
echo json_encode(['status' => 'error', 'message' => 'Cannot read OPCache configuration file']);
exit(1);
}
switch ($action) {
case 'enable':
$new_content = preg_replace('/opcache\.enable\s*=\s*0/', 'opcache.enable = 1', $ini_content);
break;
case 'disable':
$new_content = preg_replace('/opcache\.enable\s*=\s*1/', 'opcache.enable = 0', $ini_content);
break;
default:
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
exit(1);
}
if (file_put_contents($ini_file, $new_content) === false) {
echo json_encode(['status' => 'error', 'message' => 'Cannot write to OPCache configuration file']);
exit(1);
}
// Restart LiteSpeed to apply changes
exec('service lsws restart', $output, $return_var);
if ($return_var !== 0) {
echo json_encode(['status' => 'warning', 'message' => 'OPCache ' . $action . 'd but LiteSpeed restart failed']);
exit(1);
}
echo json_encode(['status' => 'success', 'message' => 'OPCache ' . $action . 'd successfully']);