Update Fix OPCache Litespeed

main
Anthony 2025-06-14 01:23:54 +08:00
parent 83861a3b06
commit 599aec5a70
3 changed files with 156 additions and 49 deletions

View File

@ -1,31 +1,55 @@
<?php
if (!extension_loaded('Zend OPcache')) {
echo json_encode(['status' => 'not_installed']);
exit(1);
}
if (!ini_get('opcache.enable')) {
echo json_encode(['status' => 'disabled']);
// Check if OPCache is enabled
if (!function_exists('opcache_get_status')) {
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
exit(1);
}
// Get OPCache status
$status = opcache_get_status(false);
if ($status === false) {
echo json_encode(['status' => 'error', 'message' => 'Cannot retrieve OPCache status']);
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']);
exit(1);
}
echo json_encode([
'status' => 'enabled',
'enabled' => $status['opcache_enabled'],
// Get OPCache configuration
$config = opcache_get_configuration();
if ($config === false) {
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache configuration']);
exit(1);
}
// Format the response
$response = [
'status' => 'success',
'data' => [
'enabled' => $config['directives']['opcache.enable'],
'memory_usage' => [
'used' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2),
'free' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2),
'wasted' => round($status['memory_usage']['wasted_memory'] / 1024 / 1024, 2)
'used' => $status['memory_usage']['used_memory'],
'free' => $status['memory_usage']['free_memory'],
'wasted' => $status['memory_usage']['wasted_memory'],
'current_wasted_percentage' => $status['memory_usage']['current_wasted_percentage']
],
'statistics' => [
'interned_strings_usage' => [
'buffer_size' => $status['interned_strings_usage']['buffer_size'],
'used_memory' => $status['interned_strings_usage']['used_memory'],
'free_memory' => $status['interned_strings_usage']['free_memory'],
'number_of_strings' => $status['interned_strings_usage']['number_of_strings']
],
'opcache_statistics' => [
'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'],
'num_cached_keys' => $status['opcache_statistics']['num_cached_keys'],
'max_cached_keys' => $status['opcache_statistics']['max_cached_keys'],
'hits' => $status['opcache_statistics']['hits'],
'misses' => $status['opcache_statistics']['misses'],
'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2)
'blacklist_misses' => $status['opcache_statistics']['blacklist_misses'],
'blacklist_miss_ratio' => $status['opcache_statistics']['blacklist_miss_ratio'],
'opcache_hit_rate' => $status['opcache_statistics']['opcache_hit_rate']
],
'configuration' => [
'directives' => $config['directives']
]
]);
]
];
echo json_encode($response, JSON_PRETTY_PRINT);

View File

@ -1,18 +1,37 @@
<?php
if (!extension_loaded('Zend OPcache')) {
// Check if OPCache is enabled
if (!function_exists('opcache_reset')) {
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
exit(1);
}
if (!ini_get('opcache.enable')) {
echo json_encode(['status' => 'error', 'message' => 'OPCache is disabled']);
exit(1);
}
// Reset OPCache
$result = opcache_reset();
if ($result === false) {
echo json_encode(['status' => 'error', 'message' => 'Failed to reset OPCache']);
exit(1);
}
echo json_encode(['status' => 'success', 'message' => 'OPCache cleared successfully']);
// Get status after reset
$status = opcache_get_status(false);
if ($status === false) {
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status after reset']);
exit(1);
}
echo json_encode([
'status' => 'success',
'message' => 'OPCache cleared successfully',
'data' => [
'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);

View File

@ -1,40 +1,104 @@
<?php
// Get the action from command line arguments
$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']);
// Check if OPCache is installed
if (!function_exists('opcache_get_status')) {
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
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);
// 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;
}
// Handle different actions
switch ($action) {
case 'enable':
$new_content = preg_replace('/opcache\.enable\s*=\s*0/', 'opcache.enable = 1', $ini_content);
// Enable OPCache
if (!ini_set('opcache.enable', '1')) {
echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache']);
exit(1);
}
$message = 'OPCache enabled successfully';
break;
case 'disable':
$new_content = preg_replace('/opcache\.enable\s*=\s*1/', 'opcache.enable = 0', $ini_content);
// Disable OPCache
if (!ini_set('opcache.enable', '0')) {
echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache']);
exit(1);
}
$message = 'OPCache disabled successfully';
break;
case 'status':
default:
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
// 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);
}
// 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);
}
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']);
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']
]
]
], JSON_PRETTY_PRINT);