Update Fix OPCache Litespeed
parent
83861a3b06
commit
599aec5a70
|
@ -1,31 +1,55 @@
|
||||||
<?php
|
<?php
|
||||||
if (!extension_loaded('Zend OPcache')) {
|
// Check if OPCache is enabled
|
||||||
echo json_encode(['status' => 'not_installed']);
|
if (!function_exists('opcache_get_status')) {
|
||||||
exit(1);
|
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
|
||||||
}
|
|
||||||
|
|
||||||
if (!ini_get('opcache.enable')) {
|
|
||||||
echo json_encode(['status' => 'disabled']);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get OPCache status
|
||||||
$status = opcache_get_status(false);
|
$status = opcache_get_status(false);
|
||||||
if ($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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode([
|
// Get OPCache configuration
|
||||||
'status' => 'enabled',
|
$config = opcache_get_configuration();
|
||||||
'enabled' => $status['opcache_enabled'],
|
if ($config === false) {
|
||||||
'memory_usage' => [
|
echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache configuration']);
|
||||||
'used' => round($status['memory_usage']['used_memory'] / 1024 / 1024, 2),
|
exit(1);
|
||||||
'free' => round($status['memory_usage']['free_memory'] / 1024 / 1024, 2),
|
}
|
||||||
'wasted' => round($status['memory_usage']['wasted_memory'] / 1024 / 1024, 2)
|
|
||||||
],
|
// Format the response
|
||||||
'statistics' => [
|
$response = [
|
||||||
'hits' => $status['opcache_statistics']['hits'],
|
'status' => 'success',
|
||||||
'misses' => $status['opcache_statistics']['misses'],
|
'data' => [
|
||||||
'hit_rate' => round($status['opcache_statistics']['opcache_hit_rate'], 2)
|
'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'],
|
||||||
|
'current_wasted_percentage' => $status['memory_usage']['current_wasted_percentage']
|
||||||
|
],
|
||||||
|
'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'],
|
||||||
|
'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);
|
|
@ -1,18 +1,37 @@
|
||||||
<?php
|
<?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']);
|
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ini_get('opcache.enable')) {
|
// Reset OPCache
|
||||||
echo json_encode(['status' => 'error', 'message' => 'OPCache is disabled']);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = opcache_reset();
|
$result = opcache_reset();
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
echo json_encode(['status' => 'error', 'message' => 'Failed to reset OPCache']);
|
echo json_encode(['status' => 'error', 'message' => 'Failed to reset OPCache']);
|
||||||
exit(1);
|
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);
|
|
@ -1,40 +1,104 @@
|
||||||
<?php
|
<?php
|
||||||
|
// Get the action from command line arguments
|
||||||
$action = $argv[1] ?? 'status';
|
$action = $argv[1] ?? 'status';
|
||||||
$ini_file = '/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini';
|
|
||||||
|
|
||||||
if (!file_exists($ini_file)) {
|
// Check if OPCache is installed
|
||||||
echo json_encode(['status' => 'error', 'message' => 'OPCache configuration file not found']);
|
if (!function_exists('opcache_get_status')) {
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$ini_content = file_get_contents($ini_file);
|
// Function to get current OPCache status
|
||||||
if ($ini_content === false) {
|
function get_opcache_status() {
|
||||||
echo json_encode(['status' => 'error', 'message' => 'Cannot read OPCache configuration file']);
|
$status = opcache_get_status(false);
|
||||||
exit(1);
|
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) {
|
switch ($action) {
|
||||||
case 'enable':
|
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;
|
break;
|
||||||
|
|
||||||
case 'disable':
|
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;
|
break;
|
||||||
|
|
||||||
|
case 'status':
|
||||||
default:
|
default:
|
||||||
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
// Get current status
|
||||||
exit(1);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_put_contents($ini_file, $new_content) === false) {
|
// Get status after action
|
||||||
echo json_encode(['status' => 'error', 'message' => 'Cannot write to OPCache configuration file']);
|
$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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restart LiteSpeed to apply changes
|
echo json_encode([
|
||||||
exec('service lsws restart', $output, $return_var);
|
'status' => 'success',
|
||||||
if ($return_var !== 0) {
|
'message' => $message,
|
||||||
echo json_encode(['status' => 'warning', 'message' => 'OPCache ' . $action . 'd but LiteSpeed restart failed']);
|
'data' => [
|
||||||
exit(1);
|
'enabled' => $config['directives']['opcache.enable'],
|
||||||
}
|
'memory_usage' => [
|
||||||
|
'used' => $status['memory_usage']['used_memory'],
|
||||||
echo json_encode(['status' => 'success', 'message' => 'OPCache ' . $action . 'd successfully']);
|
'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);
|
Loading…
Reference in New Issue