2025-06-13 17:17:12 +00:00
< ? php
2025-06-17 15:39:38 +00:00
// LiteSpeed Web Server Management for LLSMP
// No WordPress or WP-CLI required
2025-06-13 17:17:12 +00:00
$action = $argv [ 1 ] ? ? 'status' ;
$value = $argv [ 2 ] ? ? '' ;
2025-06-17 15:39:38 +00:00
// Function to execute system commands safely
function run_command ( $command ) {
2025-06-13 17:17:12 +00:00
$output = [];
$return_var = 0 ;
2025-06-17 15:39:38 +00:00
exec ( $command . ' 2>&1' , $output , $return_var );
2025-06-13 17:17:12 +00:00
return [ 'output' => $output , 'return_var' => $return_var ];
}
2025-06-17 15:39:38 +00:00
// 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'
];
}
2025-06-17 16:26:19 +00:00
// 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' ;
}
2026-02-28 08:01:44 +00:00
// Find WP-CLI binary
function find_wp_cli () {
2026-02-28 08:08:21 +00:00
$candidates = [ '/usr/local/bin/wp' , '/usr/bin/wp' , '/home/litespeed/bin/wp' ];
2026-02-28 08:01:44 +00:00
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
2025-06-17 15:39:38 +00:00
function purge_cache () {
2026-02-28 08:01:44 +00:00
$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' ];
2025-06-17 15:39:38 +00:00
}
2026-02-28 08:01:44 +00:00
// Enable or disable LiteSpeed Cache via WP-CLI plugin option
2025-06-17 15:39:38 +00:00
function toggle_cache ( $enable = true ) {
2026-02-28 08:01:44 +00:00
$wp = find_wp_cli ();
$wp_root = find_wp_root ();
if ( ! $wp ) {
2026-02-28 08:08:21 +00:00
return [ 'ok' => false , 'error' => 'WP-CLI not found. Expected at /usr/local/bin/wp — run: which wp' ];
2026-02-28 08:01:44 +00:00
}
if ( ! $wp_root ) {
return [ 'ok' => false , 'error' => 'WordPress not found at /var/www/webroot/ROOT' ];
2025-06-17 15:39:38 +00:00
}
2026-02-28 08:01:44 +00:00
// 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 ];
2025-06-17 15:39:38 +00:00
}
2025-06-13 17:17:12 +00:00
switch ( $action ) {
2025-06-17 15:39:38 +00:00
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' ]);
}
2025-06-13 17:17:12 +00:00
break ;
2025-06-17 15:39:38 +00:00
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' ],
2025-06-17 16:26:19 +00:00
'service_status' => $status [ 'status' ],
'version' => get_lsws_version ()
2025-06-17 15:39:38 +00:00
]
]);
2025-06-13 17:17:12 +00:00
break ;
case 'purge' :
2026-02-28 08:01:44 +00:00
$purge = purge_cache ();
if ( $purge [ 'ok' ]) {
echo json_encode ([ 'status' => 'success' , 'message' => 'Cache purged successfully' , 'method' => $purge [ 'method' ]]);
2025-06-17 15:39:38 +00:00
} else {
2026-02-28 08:01:44 +00:00
echo json_encode ([ 'status' => 'error' , 'message' => 'Failed to purge cache via all methods' ]);
2025-06-17 15:39:38 +00:00
}
2025-06-13 17:17:12 +00:00
break ;
2025-06-17 15:39:38 +00:00
case 'enable-cache' :
2026-02-28 08:01:44 +00:00
$toggle = toggle_cache ( true );
if ( $toggle [ 'ok' ]) {
echo json_encode ([ 'status' => 'success' , 'message' => 'LiteSpeed Cache enabled and server reloaded' ]);
2025-06-17 15:39:38 +00:00
} else {
2026-02-28 08:01:44 +00:00
echo json_encode ([ 'status' => 'error' , 'message' => 'Failed to enable cache: ' . $toggle [ 'error' ]]);
2025-06-13 17:17:12 +00:00
}
break ;
2025-06-17 15:39:38 +00:00
case 'disable-cache' :
2026-02-28 08:01:44 +00:00
$toggle = toggle_cache ( false );
if ( $toggle [ 'ok' ]) {
echo json_encode ([ 'status' => 'success' , 'message' => 'LiteSpeed Cache disabled and server reloaded' ]);
2025-06-17 15:39:38 +00:00
} else {
2026-02-28 08:01:44 +00:00
echo json_encode ([ 'status' => 'error' , 'message' => 'Failed to disable cache: ' . $toggle [ 'error' ]]);
2025-06-13 17:17:12 +00:00
}
break ;
2025-06-17 16:26:19 +00:00
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 ;
2025-06-13 17:17:12 +00:00
default :
2025-06-17 15:39:38 +00:00
echo json_encode ([
'status' => 'error' ,
2025-06-17 16:26:19 +00:00
'message' => 'Invalid action. Available actions: start, stop, restart, reload, status, purge, enable-cache, disable-cache, set-ttl, set-exclusions, version'
2025-06-17 15:39:38 +00:00
]);
2025-06-13 17:17:12 +00:00
exit ( 1 );
}