mb-admin/scripts/manage_litespeed_objcache.php

187 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2025-07-28 16:51:33 +00:00
<?php
/**
* LiteSpeed Object Cache Management Helper
* Provides PHP interface for managing LiteSpeed Object Cache with Redis
*/
$action = $argv[1] ?? 'status';
$connection_type = $argv[2] ?? 'socket';
$redis_host = $argv[3] ?? '127.0.0.1';
$redis_port = $argv[4] ?? '6379';
// Configuration paths
define('LSWS_CONF_DIR', '/usr/local/lsws/conf');
define('HTTPD_CONF', LSWS_CONF_DIR . '/httpd_config.conf');
define('REDIS_SOCKET', '/var/run/redis/redis.sock');
function run_command($command) {
$output = [];
$return_var = 0;
exec($command . ' 2>&1', $output, $return_var);
return ['output' => $output, 'return_var' => $return_var];
}
function test_redis_connection($connection_type, $host = '127.0.0.1', $port = '6379') {
if ($connection_type === 'socket') {
if (!file_exists(REDIS_SOCKET)) {
return ['success' => false, 'message' => 'Redis socket not found: ' . REDIS_SOCKET];
}
$result = run_command('redis-cli -s ' . REDIS_SOCKET . ' ping');
} else {
$result = run_command("redis-cli -h $host -p $port ping");
}
$success = ($result['return_var'] === 0 && isset($result['output'][0]) && trim($result['output'][0]) === 'PONG');
return [
'success' => $success,
'message' => $success ? 'Redis connection successful' : 'Redis connection failed',
'output' => $result['output']
];
}
function get_object_cache_status() {
if (!file_exists(HTTPD_CONF)) {
return [
'enabled' => false,
'configured' => false,
'message' => 'LiteSpeed configuration file not found'
];
}
$config_content = file_get_contents(HTTPD_CONF);
$has_objcache = strpos($config_content, 'objCache') !== false;
$cache_enabled = false;
// Check if cache is enabled
if (preg_match('/enableCache\s+(\d+)/', $config_content, $matches)) {
$cache_enabled = (int)$matches[1] === 1;
}
$redis_config = [];
if ($has_objcache) {
// Extract Redis configuration
if (preg_match('/type\s+redis/', $config_content)) {
$redis_config['type'] = 'redis';
}
if (preg_match('/addr\s+([^\s\n]+)/', $config_content, $matches)) {
$redis_config['addr'] = $matches[1];
}
if (preg_match('/defaultTTL\s+(\d+)/', $config_content, $matches)) {
$redis_config['ttl'] = $matches[1];
}
}
return [
'enabled' => $cache_enabled,
'configured' => $has_objcache,
'redis_config' => $redis_config,
'message' => $has_objcache ? 'Object Cache is configured' : 'Object Cache not configured'
];
}
function generate_cache_config($enable = true, $connection_type = 'socket', $host = '127.0.0.1', $port = '6379') {
if (!$enable) {
return "
cache {
enableCache 0
storage {
cacheStorePath /tmp/lshttpd/cache/
}
}
";
}
$redis_addr = ($connection_type === 'socket') ? 'unix:' . REDIS_SOCKET : "$host:$port";
return "
cache {
enableCache 1
qsCache 1
reqCookieCache 1
respCookieCache 1
ignoreReqCacheCtrl 1
ignoreRespCacheCtrl 0
enablePrivateCache 0
privateExpireInSeconds 3600
storage {
cacheStorePath /tmp/lshttpd/cache/
# Redis Object Cache Configuration
objCache {
type redis
addr $redis_addr
defaultTTL 60
}
}
}
";
}
switch ($action) {
case 'status':
$status = get_object_cache_status();
echo json_encode([
'status' => 'success',
'data' => $status,
'message' => $status['message']
], JSON_PRETTY_PRINT);
break;
case 'test-redis':
$test = test_redis_connection($connection_type, $redis_host, $redis_port);
echo json_encode([
'status' => $test['success'] ? 'success' : 'error',
'message' => $test['message'],
'connection_type' => $connection_type,
'redis_host' => $connection_type === 'tcp' ? $redis_host : 'socket',
'redis_port' => $connection_type === 'tcp' ? $redis_port : 'N/A'
], JSON_PRETTY_PRINT);
break;
case 'enable':
// Test Redis connection first
$test = test_redis_connection($connection_type, $redis_host, $redis_port);
if (!$test['success']) {
echo json_encode([
'status' => 'error',
'message' => 'Cannot enable Object Cache: ' . $test['message']
]);
exit(1);
}
// Generate configuration
$config = generate_cache_config(true, $connection_type, $redis_host, $redis_port);
echo json_encode([
'status' => 'success',
'message' => 'Object Cache configuration generated successfully',
'config_preview' => $config,
'next_step' => 'Run the bash script to apply configuration'
], JSON_PRETTY_PRINT);
break;
case 'disable':
$config = generate_cache_config(false);
echo json_encode([
'status' => 'success',
'message' => 'Object Cache disable configuration generated',
'config_preview' => $config,
'next_step' => 'Run the bash script to apply configuration'
], JSON_PRETTY_PRINT);
break;
default:
echo json_encode([
'status' => 'error',
'message' => 'Invalid action. Available actions: status, test-redis, enable, disable',
'usage' => 'php manage_litespeed_objcache.php [action] [connection_type] [redis_host] [redis_port]'
]);
exit(1);
}
?>