mb-admin/scripts/configure_litespeed_plugin_...

304 lines
11 KiB
Bash
Raw Normal View History

2025-07-28 17:08:01 +00:00
#!/bin/bash
#
# Configure LiteSpeed Cache WordPress Plugin Object Cache Settings
# This configures the Object Cache settings shown in the WordPress admin panel
# Uses LiteSpeed's custom WP-CLI commands: wp litespeed-option
#
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
WP_ROOT="/var/www/webroot/ROOT"
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 Cache Plugin 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 " --wp-root=PATH WordPress root directory (default: /var/www/webroot/ROOT)\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 --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
;;
--wp-root=*)
WP_ROOT="${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 WP-CLI is available
check_wp_cli() {
if ! command -v wp >/dev/null 2>&1; then
error_exit "WP-CLI not found. Please install WP-CLI to configure LiteSpeed Cache plugin settings."
fi
# Check if we're in WordPress directory
if [[ ! -f "$WP_ROOT/wp-config.php" ]]; then
error_exit "WordPress not found at: $WP_ROOT"
fi
}
# Function to check if LiteSpeed Cache plugin is active
check_litespeed_plugin() {
if ! wp plugin is-active litespeed-cache --path="$WP_ROOT" 2>/dev/null; then
error_exit "LiteSpeed Cache plugin is not active. Please install and activate the plugin first."
fi
# Test if LiteSpeed WP-CLI commands are available
if ! wp litespeed-option all --path="$WP_ROOT" >/dev/null 2>&1; then
error_exit "LiteSpeed WP-CLI commands not available. Please ensure LiteSpeed Cache plugin is properly installed."
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 configure LiteSpeed Cache Plugin Object Cache
configure_plugin_object_cache() {
local enable_cache="$1"
info "Configuring LiteSpeed Cache Plugin Object Cache settings..."
if [[ "$enable_cache" == "true" ]]; then
# Enable Object Cache
info "Enabling Object Cache in LiteSpeed Cache Plugin..."
# Set Object Cache to ON (1 = enabled)
wp litespeed-option set object 1 --path="$WP_ROOT" || error_exit "Failed to enable Object Cache"
# Set Object Cache Method to Redis (1 = Redis, 0 = Memcached)
wp litespeed-option set object-kind 1 --path="$WP_ROOT" || error_exit "Failed to set Object Cache method to Redis"
if [[ "$CONNECTION_TYPE" == "socket" ]]; then
# Configure for socket connection
info "Configuring Object Cache for socket connection..."
# Set Redis host to socket path
wp litespeed-option set object-host "$REDIS_SOCKET" --path="$WP_ROOT" || error_exit "Failed to set Redis socket path"
# Set port to 0 for socket connections
wp litespeed-option set object-port 0 --path="$WP_ROOT" || error_exit "Failed to set Redis port for socket"
else
# Configure for TCP connection
info "Configuring Object Cache for TCP connection..."
# Set Redis host
wp litespeed-option set object-host "$REDIS_HOST" --path="$WP_ROOT" || error_exit "Failed to set Redis host"
# Set Redis port
wp litespeed-option set object-port "$REDIS_PORT" --path="$WP_ROOT" || error_exit "Failed to set Redis port"
fi
# Set default database (usually 0)
wp litespeed-option set object-db_id 0 --path="$WP_ROOT" || error_exit "Failed to set Redis database ID"
# Set default TTL (Time To Live) - 360 seconds
wp litespeed-option set object-life 360 --path="$WP_ROOT" || error_exit "Failed to set Object Cache TTL"
# Enable persistent connection (1 = enabled)
wp litespeed-option set object-persistent 1 --path="$WP_ROOT" || error_exit "Failed to enable persistent connection"
success "LiteSpeed Cache Plugin Object Cache configured successfully"
else
# Disable Object Cache
info "Disabling Object Cache in LiteSpeed Cache Plugin..."
wp litespeed-option set object 0 --path="$WP_ROOT" || error_exit "Failed to disable Object Cache"
success "LiteSpeed Cache Plugin Object Cache disabled successfully"
fi
}
# Function to show Object Cache status
show_status() {
info "Checking LiteSpeed Cache Plugin Object Cache status..."
# Get current settings using LiteSpeed's WP-CLI commands
local object_enabled
local object_kind
local object_host
local object_port
local object_db
local object_ttl
local object_persistent
object_enabled=$(wp litespeed-option get object --path="$WP_ROOT" 2>/dev/null || echo "0")
object_kind=$(wp litespeed-option get object-kind --path="$WP_ROOT" 2>/dev/null || echo "0")
object_host=$(wp litespeed-option get object-host --path="$WP_ROOT" 2>/dev/null || echo "")
object_port=$(wp litespeed-option get object-port --path="$WP_ROOT" 2>/dev/null || echo "")
object_db=$(wp litespeed-option get object-db_id --path="$WP_ROOT" 2>/dev/null || echo "0")
object_ttl=$(wp litespeed-option get object-life --path="$WP_ROOT" 2>/dev/null || echo "360")
object_persistent=$(wp litespeed-option get object-persistent --path="$WP_ROOT" 2>/dev/null || echo "0")
printf "\n${YELLOW}LiteSpeed Cache Plugin Object Cache Status:${NC}\n"
printf "Object Cache: %s\n" "$(if [[ "$object_enabled" == "1" ]]; then echo "${GREEN}ON${NC}"; else echo "${RED}OFF${NC}"; fi)"
if [[ "$object_enabled" == "1" ]]; then
printf "Method: %s\n" "$(if [[ "$object_kind" == "1" ]]; then echo "${GREEN}Redis${NC}"; else echo "${YELLOW}Memcached${NC}"; fi)"
printf "Host: %s\n" "${GREEN}${object_host}${NC}"
printf "Port: %s\n" "${GREEN}${object_port}${NC}"
printf "Database: %s\n" "${GREEN}${object_db}${NC}"
printf "TTL: %s seconds\n" "${GREEN}${object_ttl}${NC}"
printf "Persistent: %s\n" "$(if [[ "$object_persistent" == "1" ]]; then echo "${GREEN}ON${NC}"; else echo "${RED}OFF${NC}"; fi)"
# Test Redis connection if configured
if [[ "$object_kind" == "1" && -n "$object_host" ]]; then
printf "Redis Connection: "
if [[ "$object_port" == "0" || "$object_host" == *".sock" ]]; then
# Socket connection
if [[ -S "$object_host" ]] && redis-cli -s "$object_host" ping >/dev/null 2>&1; then
printf "${GREEN}Connected${NC}\n"
else
printf "${RED}Failed${NC}\n"
fi
else
# TCP connection
if redis-cli -h "$object_host" -p "$object_port" ping >/dev/null 2>&1; then
printf "${GREEN}Connected${NC}\n"
else
printf "${RED}Failed${NC}\n"
fi
fi
fi
fi
printf "\n"
}
# Main execution
case "$ACTION" in
"enable")
check_wp_cli
check_litespeed_plugin
check_redis_connection
configure_plugin_object_cache "true"
show_status
success "LiteSpeed Cache Plugin Object Cache enabled successfully!"
;;
"disable")
check_wp_cli
check_litespeed_plugin
configure_plugin_object_cache "false"
show_status
success "LiteSpeed Cache Plugin Object Cache disabled successfully!"
;;
"status")
check_wp_cli
check_litespeed_plugin
show_status
;;
"")
error_exit "No action specified. Use --enable, --disable, or --status"
;;
*)
error_exit "Invalid action: $ACTION"
;;
esac
exit 0