#!/bin/bash # # Configure LiteSpeed Object Cache with Redis # This script configures LiteSpeed's built-in Object Cache to use Redis # For MightyBox.io CloudScripting addon # set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Helper Functions info() { printf "${BLUE}[INFO] %s${NC}\n" "$@" } success() { printf "${GREEN}[SUCCESS] %s${NC}\n" "$@" } warning() { printf "${YELLOW}[WARNING] %s${NC}\n" "$@" } error_exit() { printf "${RED}[ERROR] %s${NC}\n" "$@" >&2 exit 1 } # Configuration variables LSWS_CONF_DIR="/usr/local/lsws/conf" HTTPD_CONF="$LSWS_CONF_DIR/httpd_config.conf" REDIS_SOCKET="/var/run/redis/redis.sock" REDIS_HOST="127.0.0.1" REDIS_PORT="6379" CONNECTION_TYPE="socket" # Default to socket for better performance usage() { printf "Usage: %s [OPTIONS]\n" "$0" printf "\n" printf "Configure LiteSpeed Object Cache with Redis\n" printf "\n" printf "Options:\n" printf " --connection-type=TYPE Connection type: 'socket' or 'tcp' (default: socket)\n" printf " --redis-host=HOST Redis host (default: 127.0.0.1, used with tcp)\n" printf " --redis-port=PORT Redis port (default: 6379, used with tcp)\n" printf " --redis-socket=PATH Redis socket path (default: /var/run/redis/redis.sock)\n" printf " --enable Enable Object Cache\n" printf " --disable Disable Object Cache\n" printf " --status Show current Object Cache status\n" printf " -h, --help Display this help message\n" printf "\n" printf "Examples:\n" printf " %s --enable # Enable with socket connection\n" "$0" printf " %s --enable --connection-type=tcp # Enable with TCP connection\n" "$0" printf " %s --enable --redis-host=192.168.1.100 # Enable with remote Redis\n" "$0" printf " %s --disable # Disable Object Cache\n" "$0" printf " %s --status # Show current status\n" "$0" exit 1 } # Parse command line arguments ACTION="" while [[ $# -gt 0 ]]; do case $1 in --connection-type=*) CONNECTION_TYPE="${1#*=}" shift ;; --redis-host=*) REDIS_HOST="${1#*=}" shift ;; --redis-port=*) REDIS_PORT="${1#*=}" shift ;; --redis-socket=*) REDIS_SOCKET="${1#*=}" shift ;; --enable) ACTION="enable" shift ;; --disable) ACTION="disable" shift ;; --status) ACTION="status" shift ;; -h|--help) usage ;; *) error_exit "Unknown option: $1" ;; esac done # Validate connection type if [[ "$CONNECTION_TYPE" != "socket" && "$CONNECTION_TYPE" != "tcp" ]]; then error_exit "Invalid connection type: $CONNECTION_TYPE. Must be 'socket' or 'tcp'" fi # Function to check if Redis is running check_redis_connection() { local test_result if [[ "$CONNECTION_TYPE" == "socket" ]]; then if [[ ! -S "$REDIS_SOCKET" ]]; then error_exit "Redis socket not found at: $REDIS_SOCKET" fi test_result=$(redis-cli -s "$REDIS_SOCKET" ping 2>/dev/null || echo "FAILED") else test_result=$(redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" ping 2>/dev/null || echo "FAILED") fi if [[ "$test_result" != "PONG" ]]; then error_exit "Cannot connect to Redis. Please ensure Redis is running and accessible." fi success "Redis connection test successful" } # Function to backup LiteSpeed configuration backup_config() { local backup_file="$HTTPD_CONF.backup.$(date +%Y%m%d_%H%M%S)" if [[ -f "$HTTPD_CONF" ]]; then cp "$HTTPD_CONF" "$backup_file" info "Configuration backed up to: $backup_file" fi } # Function to configure LiteSpeed Object Cache configure_object_cache() { local enable_cache="$1" info "Configuring LiteSpeed Object Cache..." # Check if LiteSpeed config exists if [[ ! -f "$HTTPD_CONF" ]]; then error_exit "LiteSpeed configuration file not found: $HTTPD_CONF" fi # Backup configuration backup_config # Create temporary configuration local temp_config=$(mktemp) local cache_config="" if [[ "$enable_cache" == "true" ]]; then if [[ "$CONNECTION_TYPE" == "socket" ]]; then cache_config=" 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 $(if [[ "$CONNECTION_TYPE" == "socket" ]]; then echo "unix:$REDIS_SOCKET"; else echo "$REDIS_HOST:$REDIS_PORT"; fi) defaultTTL 60 } } } " else cache_config=" 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_HOST:$REDIS_PORT defaultTTL 60 } } } " fi else cache_config=" cache { enableCache 0 storage { cacheStorePath /tmp/lshttpd/cache/ } } " fi # Remove existing cache configuration and add new one awk ' BEGIN { in_cache_block = 0; brace_count = 0 } /^cache\s*{/ { in_cache_block = 1; brace_count = 1; next } in_cache_block == 1 { for (i = 1; i <= length($0); i++) { char = substr($0, i, 1) if (char == "{") brace_count++ if (char == "}") brace_count-- } if (brace_count == 0) { in_cache_block = 0 } next } { print } ' "$HTTPD_CONF" > "$temp_config" # Add new cache configuration echo "$cache_config" >> "$temp_config" # Replace original configuration mv "$temp_config" "$HTTPD_CONF" # Set proper permissions chown lsadm:lsadm "$HTTPD_CONF" chmod 644 "$HTTPD_CONF" success "LiteSpeed Object Cache configuration updated" } # Function to restart LiteSpeed restart_litespeed() { info "Restarting LiteSpeed Web Server..." if systemctl restart lsws; then success "LiteSpeed Web Server restarted successfully" sleep 3 # Give LiteSpeed time to initialize else error_exit "Failed to restart LiteSpeed Web Server" fi } # Function to show Object Cache status show_status() { info "Checking LiteSpeed Object Cache status..." # Check if cache configuration exists in httpd_config.conf if grep -q "objCache" "$HTTPD_CONF" 2>/dev/null; then local cache_enabled=$(grep -A 20 "^cache" "$HTTPD_CONF" | grep "enableCache" | awk '{print $2}' | head -1) local cache_type=$(grep -A 50 "objCache" "$HTTPD_CONF" | grep "type" | awk '{print $2}' | head -1) local cache_addr=$(grep -A 50 "objCache" "$HTTPD_CONF" | grep "addr" | awk '{print $2}' | head -1) printf "\n${YELLOW}LiteSpeed Object Cache Status:${NC}\n" printf "Cache Enabled: %s\n" "${cache_enabled:-"Not configured"}" printf "Cache Type: %s\n" "${cache_type:-"Not configured"}" printf "Redis Address: %s\n" "${cache_addr:-"Not configured"}" # Test Redis connection if configured if [[ "$cache_type" == "redis" && -n "$cache_addr" ]]; then printf "Redis Connection: " if [[ "$cache_addr" == unix:* ]]; then local socket_path="${cache_addr#unix:}" if redis-cli -s "$socket_path" ping >/dev/null 2>&1; then printf "${GREEN}Connected${NC}\n" else printf "${RED}Failed${NC}\n" fi else local host_port=(${cache_addr//:/ }) if redis-cli -h "${host_port[0]}" -p "${host_port[1]}" ping >/dev/null 2>&1; then printf "${GREEN}Connected${NC}\n" else printf "${RED}Failed${NC}\n" fi fi fi else printf "\n${YELLOW}LiteSpeed Object Cache Status:${NC}\n" printf "Status: ${RED}Not configured${NC}\n" fi # Check LiteSpeed service status printf "\nLiteSpeed Service: " if systemctl is-active lsws >/dev/null 2>&1; then printf "${GREEN}Running${NC}\n" else printf "${RED}Not running${NC}\n" fi } # Main execution case "$ACTION" in "enable") info "Enabling LiteSpeed Object Cache with Redis..." check_redis_connection configure_object_cache "true" restart_litespeed show_status success "LiteSpeed Object Cache with Redis enabled successfully!" ;; "disable") info "Disabling LiteSpeed Object Cache..." configure_object_cache "false" restart_litespeed show_status success "LiteSpeed Object Cache disabled successfully!" ;; "status") show_status ;; "") error_exit "No action specified. Use --enable, --disable, or --status" ;; *) error_exit "Invalid action: $ACTION" ;; esac exit 0