#!/bin/bash # # Install and configure Redis server for WordPress on LLSMP/AlmaLinux # - Installs redis via dnf # - Configures Unix socket at /var/run/redis/redis.sock # - Sets litespeed user as member of redis group for socket access # - Enables and starts redis service # - Configures LiteSpeed Cache Plugin Object Cache via WP-CLI # set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' 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; } REDIS_SOCKET="/var/run/redis/redis.sock" REDIS_CONF="/etc/redis/redis.conf" REDIS_CONF_ALT="/etc/redis.conf" WP_ROOT="/var/www/webroot/ROOT" WEB_USER="litespeed" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # ── 1. Detect active Redis conf path ──────────────────────────────────────── get_redis_conf() { if [[ -f "$REDIS_CONF" ]]; then echo "$REDIS_CONF" elif [[ -f "$REDIS_CONF_ALT" ]]; then echo "$REDIS_CONF_ALT" else echo "" fi } # ── 2. Install Redis if not present ───────────────────────────────────────── install_redis() { if command -v redis-server >/dev/null 2>&1; then info "Redis server binary already present at $(which redis-server)" return 0 fi info "Installing Redis via dnf..." dnf install -y redis || error_exit "Failed to install Redis. Check dnf connectivity." success "Redis installed successfully." } # ── 3. Configure Redis for Unix socket ────────────────────────────────────── configure_redis() { local conf conf=$(get_redis_conf) if [[ -z "$conf" ]]; then error_exit "Redis config file not found at $REDIS_CONF or $REDIS_CONF_ALT" fi info "Configuring Redis at $conf..." # Backup original config (idempotent - only once) if [[ ! -f "${conf}.orig" ]]; then cp "$conf" "${conf}.orig" info "Original config backed up to ${conf}.orig" fi # Disable TCP listener (bind to 127.0.0.1 only, no external exposure) sed -i 's/^bind .*/bind 127.0.0.1/' "$conf" # Enable Unix socket if grep -q '^unixsocket ' "$conf"; then sed -i "s|^unixsocket .*|unixsocket $REDIS_SOCKET|" "$conf" else echo "unixsocket $REDIS_SOCKET" >> "$conf" fi # Set socket permissions so litespeed group can access it if grep -q '^unixsocketperm ' "$conf"; then sed -i 's/^unixsocketperm .*/unixsocketperm 775/' "$conf" else echo "unixsocketperm 775" >> "$conf" fi # Sensible memory limit (256MB default, adjustable) if ! grep -q '^maxmemory ' "$conf"; then echo "maxmemory 256mb" >> "$conf" echo "maxmemory-policy allkeys-lru" >> "$conf" fi # Ensure socket directory exists with correct ownership mkdir -p "$(dirname "$REDIS_SOCKET")" chown redis:redis "$(dirname "$REDIS_SOCKET")" success "Redis configuration applied." } # ── 4. Grant litespeed user access to redis socket ────────────────────────── configure_socket_access() { info "Granting $WEB_USER user access to Redis socket group..." if id "$WEB_USER" >/dev/null 2>&1; then usermod -aG redis "$WEB_USER" || warning "Could not add $WEB_USER to redis group" success "$WEB_USER added to redis group." else warning "User $WEB_USER not found, skipping group membership." fi } # ── 5. Enable and start Redis service ─────────────────────────────────────── start_redis() { # Detect actual service name (redis, redis6, redis7, etc.) local svc svc=$(systemctl list-unit-files --type=service | grep -oP '[\w@-]*redis[\w@-]*\.service' | head -1 | sed 's/\.service//') if [[ -z "$svc" ]]; then error_exit "No Redis systemd service unit found after installation. Something went wrong." fi info "Enabling and starting $svc..." systemctl enable "$svc" systemctl restart "$svc" # Wait up to 10s for socket to appear local retries=10 while [[ $retries -gt 0 ]]; do if [[ -S "$REDIS_SOCKET" ]]; then break fi sleep 1 retries=$((retries - 1)) done if [[ ! -S "$REDIS_SOCKET" ]]; then error_exit "Redis started but socket not found at $REDIS_SOCKET after 10 seconds. Check Redis logs: journalctl -u $svc" fi # Verify connectivity local ping_result ping_result=$(redis-cli -s "$REDIS_SOCKET" ping 2>/dev/null || echo "FAILED") if [[ "$ping_result" != "PONG" ]]; then error_exit "Redis socket exists but PING failed. Check Redis logs: journalctl -u $svc" fi success "Redis is running and responding on $REDIS_SOCKET" } # ── 6. Configure LiteSpeed Cache Plugin Object Cache ──────────────────────── configure_wp_object_cache() { local plugin_script="$SCRIPT_DIR/configure_litespeed_plugin_object_cache.sh" if [[ ! -f "$plugin_script" ]]; then warning "configure_litespeed_plugin_object_cache.sh not found at $plugin_script - skipping WP object cache config." return 0 fi if [[ ! -f "$WP_ROOT/wp-config.php" ]]; then warning "WordPress not found at $WP_ROOT - skipping WP object cache config." return 0 fi info "Configuring LiteSpeed Cache Plugin Object Cache with Redis socket..." if bash "$plugin_script" --enable --connection-type=socket --redis-socket="$REDIS_SOCKET" --wp-root="$WP_ROOT"; then success "WordPress LiteSpeed Cache Plugin Object Cache configured successfully." else warning "Failed to configure WordPress Object Cache. You can run it manually: bash $plugin_script --enable --wp-root=$WP_ROOT" fi } # ── Main ───────────────────────────────────────────────────────────────────── info "======== Redis Install & Configure for WordPress (LLSMP) ========" install_redis configure_redis configure_socket_access start_redis configure_wp_object_cache success "======== Redis setup complete ========" info "Socket: $REDIS_SOCKET" info "Config: $(get_redis_conf)" info "Test: redis-cli -s $REDIS_SOCKET ping"