mb-admin/scripts/manage_litespeed.php

248 lines
8.5 KiB
PHP

<?php
// LiteSpeed Web Server Management for LLSMP
// No WordPress or WP-CLI required
$action = $argv[1] ?? 'status';
$value = $argv[2] ?? '';
// Function to execute system commands safely
function run_command($command) {
$output = [];
$return_var = 0;
exec($command . ' 2>&1', $output, $return_var);
return ['output' => $output, 'return_var' => $return_var];
}
// Function to restart LiteSpeed Web Server
function restart_lsws() {
$result = run_command('systemctl restart lsws');
return $result['return_var'] === 0;
}
// Function to reload LiteSpeed Web Server
function reload_lsws() {
$result = run_command('systemctl reload lsws');
return $result['return_var'] === 0;
}
// Function to stop LiteSpeed Web Server
function stop_lsws() {
$result = run_command('systemctl stop lsws');
return $result['return_var'] === 0;
}
// Function to start LiteSpeed Web Server
function start_lsws() {
$result = run_command('systemctl start lsws');
return $result['return_var'] === 0;
}
// Function to check LiteSpeed status
function get_lsws_status() {
$result = run_command('systemctl is-active lsws');
return [
'running' => $result['return_var'] === 0,
'status' => $result['output'][0] ?? 'unknown'
];
}
// Function to get LiteSpeed version
function get_lsws_version() {
$result = run_command('/var/www/bin/lshttpd -v');
if ($result['return_var'] === 0 && !empty($result['output'])) {
foreach ($result['output'] as $line) {
if (preg_match('/LiteSpeed.*?(\d+\.\d+(?:\.\d+)?)/i', $line, $matches)) {
return $matches[1];
}
}
}
return 'unknown';
}
// Find WP-CLI binary
function find_wp_cli() {
$candidates = ['/usr/local/bin/wp', '/usr/bin/wp', '/home/litespeed/bin/wp'];
foreach ($candidates as $path) {
if (file_exists($path) && is_executable($path)) {
return $path;
}
}
$result = run_command('command -v wp');
if ($result['return_var'] === 0 && !empty($result['output'][0])) {
return trim($result['output'][0]);
}
return null;
}
// Find WordPress root
function find_wp_root() {
$candidates = ['/var/www/webroot/ROOT', '/var/www/webroot/ROOT/'];
foreach ($candidates as $path) {
if (file_exists($path . '/wp-config.php')) {
return $path;
}
}
return null;
}
// Run a WP-CLI command as litespeed user
function run_wp_cli($wp, $wp_root, $args) {
$cmd = "sudo -u litespeed " . escapeshellarg($wp) . " --path=" . escapeshellarg($wp_root) . " " . $args . " 2>&1";
return run_command($cmd);
}
// Function to purge cache: WP-CLI flush first, lsws restart as fallback
function purge_cache() {
$wp = find_wp_cli();
$wp_root = find_wp_root();
if ($wp && $wp_root) {
$result = run_wp_cli($wp, $wp_root, 'litespeed-purge all');
if ($result['return_var'] === 0) {
return ['ok' => true, 'method' => 'wp-cli litespeed-purge all'];
}
// Fallback: flush cache via lscmctl if available
$lsc = run_command('/usr/local/lsws/bin/lscmctl purge --all 2>&1');
if ($lsc['return_var'] === 0) {
return ['ok' => true, 'method' => 'lscmctl purge --all'];
}
}
// Last resort: restart lsws clears in-memory cache
$restarted = restart_lsws();
return ['ok' => $restarted, 'method' => 'lsws restart'];
}
// Enable or disable LiteSpeed Cache via WP-CLI plugin option
function toggle_cache($enable = true) {
$wp = find_wp_cli();
$wp_root = find_wp_root();
if (!$wp) {
return ['ok' => false, 'error' => 'WP-CLI not found. Expected at /usr/local/bin/wp — run: which wp'];
}
if (!$wp_root) {
return ['ok' => false, 'error' => 'WordPress not found at /var/www/webroot/ROOT'];
}
// Check plugin active first
$check = run_wp_cli($wp, $wp_root, 'plugin is-active litespeed-cache');
if ($check['return_var'] !== 0) {
return ['ok' => false, 'error' => 'LiteSpeed Cache plugin is not active in WordPress'];
}
$value = $enable ? '1' : '0';
$result = run_wp_cli($wp, $wp_root, "litespeed-option set cache {$value}");
if ($result['return_var'] !== 0) {
$detail = implode(' ', $result['output']);
return ['ok' => false, 'error' => "litespeed-option set cache {$value} failed: {$detail}"];
}
// Reload lsws to apply
reload_lsws();
return ['ok' => true, 'error' => null];
}
switch ($action) {
case 'start':
if (start_lsws()) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Web Server started']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to start LiteSpeed Web Server']);
}
break;
case 'stop':
if (stop_lsws()) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Web Server stopped']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to stop LiteSpeed Web Server']);
}
break;
case 'restart':
if (restart_lsws()) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Web Server restarted']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to restart LiteSpeed Web Server']);
}
break;
case 'reload':
if (reload_lsws()) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Web Server configuration reloaded']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to reload LiteSpeed Web Server']);
}
break;
case 'status':
$status = get_lsws_status();
echo json_encode([
'status' => 'success',
'server' => [
'running' => $status['running'],
'service_status' => $status['status'],
'version' => get_lsws_version()
]
]);
break;
case 'purge':
$purge = purge_cache();
if ($purge['ok']) {
echo json_encode(['status' => 'success', 'message' => 'Cache purged successfully', 'method' => $purge['method']]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to purge cache via all methods']);
}
break;
case 'enable-cache':
$toggle = toggle_cache(true);
if ($toggle['ok']) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Cache enabled and server reloaded']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to enable cache: ' . $toggle['error']]);
}
break;
case 'disable-cache':
$toggle = toggle_cache(false);
if ($toggle['ok']) {
echo json_encode(['status' => 'success', 'message' => 'LiteSpeed Cache disabled and server reloaded']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to disable cache: ' . $toggle['error']]);
}
break;
case 'set-ttl':
if (empty($value)) {
echo json_encode(['status' => 'error', 'message' => 'TTL value required']);
exit(1);
}
// For Virtuozzo LLSMP, we would need to modify the server configuration
// This is a placeholder - actual implementation depends on how Virtuozzo manages LiteSpeed config
echo json_encode(['status' => 'success', 'message' => "TTL would be set to {$value} seconds (placeholder - needs Virtuozzo-specific implementation)"]);
break;
case 'set-exclusions':
if (empty($value)) {
echo json_encode(['status' => 'error', 'message' => 'Exclusion paths required']);
exit(1);
}
// For Virtuozzo LLSMP, we would need to modify the server configuration
// This is a placeholder - actual implementation depends on how Virtuozzo manages LiteSpeed config
echo json_encode(['status' => 'success', 'message' => "Cache exclusions would be set to: {$value} (placeholder - needs Virtuozzo-specific implementation)"]);
break;
case 'version':
$version = get_lsws_version();
echo json_encode(['status' => 'success', 'version' => $version, 'message' => "LiteSpeed Web Server version {$version}"]);
break;
default:
echo json_encode([
'status' => 'error',
'message' => 'Invalid action. Available actions: start, stop, restart, reload, status, purge, enable-cache, disable-cache, set-ttl, set-exclusions, version'
]);
exit(1);
}