diff --git a/OBJECT_CACHE_FIX_SUMMARY.md b/OBJECT_CACHE_FIX_SUMMARY.md deleted file mode 100644 index 50324ad..0000000 --- a/OBJECT_CACHE_FIX_SUMMARY.md +++ /dev/null @@ -1,109 +0,0 @@ -# LiteSpeed Cache Plugin Object Cache Configuration - CRITICAL FIX - -## 🚨 MAJOR ISSUE DISCOVERED AND FIXED - -### ❌ Previous Implementation (INCORRECT): -```bash -# WRONG - Using standard WordPress options -wp option update litespeed.conf.object 1 -wp option update litespeed.conf.object-kind 1 -wp option update litespeed.conf.object-host "/var/run/redis/redis.sock" -``` - -### ✅ Corrected Implementation (CORRECT): -```bash -# CORRECT - Using LiteSpeed's custom WP-CLI commands -wp litespeed-option set object 1 -wp litespeed-option set object-kind 1 -wp litespeed-option set object-host "/var/run/redis/redis.sock" -``` - -## 🔍 Root Cause of the Problem - -The LiteSpeed Cache WordPress plugin **does NOT use** standard WordPress options stored in `wp_options` table with `litespeed.conf.*` keys. Instead, it has its own **custom WP-CLI commands** that must be used: - -- `wp litespeed-option set ` -- `wp litespeed-option get ` -- `wp litespeed-option all` - -## 📋 Changes Made - -### 1. **Fixed Script: `scripts/configure_litespeed_plugin_object_cache.sh`** - -**Changed ALL commands from:** -```bash -wp option update litespeed.conf.object-* VALUE -wp option get litespeed.conf.object-* -``` - -**To:** -```bash -wp litespeed-option set object-* VALUE -wp litespeed-option get object-* -``` - -**Key Option Names (without `litespeed.conf.` prefix):** -- `object` - Enable/disable Object Cache (0/1) -- `object-kind` - Cache method (1=Redis, 0=Memcached) -- `object-host` - Redis host/socket path -- `object-port` - Redis port (0 for socket) -- `object-db_id` - Redis database ID -- `object-life` - TTL in seconds -- `object-persistent` - Persistent connection (0/1) - -### 2. **Added Validation** -```bash -# 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 -``` - -### 3. **Enhanced Status Display** -- Added `object-persistent` status display -- Improved connection testing logic -- Better error handling - -## 🎯 Impact - -### Before Fix: -- **100% FAILURE** - Commands would fail silently or with errors -- Object Cache would **NEVER** be configured -- Users would see no Object Cache settings in WordPress admin - -### After Fix: -- **✅ WORKING** - Commands execute successfully -- Object Cache properly configured with Redis -- Settings visible and functional in WordPress admin panel -- Automatic configuration during WordPress installation works correctly - -## 🔗 Official Documentation Reference - -Based on [LiteSpeed WordPress CLI Documentation](https://docs.litespeedtech.com/lscache/lscwp/cli/): - -> **Option Commands** -> Commands having to do with options all begin with `litespeed-option`. -> -> **Set a Particular Option** -> - Command: `litespeed-option set` -> - Parameters: ``: the option key to update, ``: the value to assign - -## ✅ Verification - -The fix has been validated against: -1. ✅ **Official LiteSpeed documentation** -2. ✅ **Bash syntax check** (`bash -n` passes) -3. ✅ **Integration points** (install script, JPS actions) -4. ✅ **Error handling** and connection testing -5. ✅ **Status reporting** functionality - -## 🎉 Result - -The LiteSpeed Cache Plugin Object Cache configuration now works correctly and will: -- ✅ Enable Object Cache in the plugin -- ✅ Configure Redis connection (socket or TCP) -- ✅ Set appropriate TTL and database settings -- ✅ Display correct status information -- ✅ Integrate seamlessly with WordPress installation process - -**This fix transforms a completely non-functional feature into a fully working Object Cache configuration system!** \ No newline at end of file diff --git a/mbadmin.jps b/mbadmin.jps index fa5d6a3..e24e2e5 100644 --- a/mbadmin.jps +++ b/mbadmin.jps @@ -26,6 +26,9 @@ onInstall: - if [ ! -f clear_opcache.php ]; then echo "Failed to download clear_opcache.php"; exit 1; fi - curl -OL https://deploy.mightybox.io/tony/mb-admin/raw/branch/main/scripts/update_opcache_settings.sh - if [ ! -f update_opcache_settings.sh ]; then echo "Failed to download update_opcache_settings.sh"; exit 1; fi + # Download WP search-replace wrapper script with verification + - curl -OL https://deploy.mightybox.io/tony/mb-admin/raw/branch/main/scripts/wp-search-replace.sh + - if [ ! -f wp-search-replace.sh ]; then echo "Failed to download wp-search-replace.sh"; exit 1; fi # Download LiteSpeed scripts with verification - curl -OL https://deploy.mightybox.io/tony/mb-admin/raw/branch/main/scripts/check_litespeed.php - if [ ! -f check_litespeed.php ]; then echo "Failed to download check_litespeed.php"; exit 1; fi @@ -520,7 +523,7 @@ actions: user: litespeed commands: - cd /var/www/webroot/ROOT/ - - /home/litespeed/bin/wp search-replace '${settings.old_url}' '${settings.new_url}' --all-tables + - bash /home/litespeed/mbmanager/scripts/wp-search-replace.sh '${settings.old_url}' '${settings.new_url}' --all-tables - /home/litespeed/bin/wp transient delete --all - /home/litespeed/bin/wp cache flush - return: diff --git a/scripts/dbPreparation.sh b/scripts/dbPreparation.sh index 1be18ac..d41cba4 100644 --- a/scripts/dbPreparation.sh +++ b/scripts/dbPreparation.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Source the utility script +source "$(dirname "$0")/utils.sh" + # Automatically generate a new secure password for the root user new_root_password=$(openssl rand -base64 12) @@ -86,26 +89,28 @@ fi # Backup the wp-config.php file before making changes WP_CONFIG="/var/www/webroot/ROOT/wp-config.php" -sudo cp $WP_CONFIG $WP_CONFIG.bak -# Escape special characters for sed -escaped_db_name=$(printf '%s\n' "$DB_NAME" | sed 's:[\/&]:\\&:g') -escaped_db_user=$(printf '%s\n' "$DB_USER" | sed 's:[\/&]:\\&:g') -escaped_db_password=$(printf '%s\n' "$DB_PASSWORD" | sed 's:[\/&]:\\&:g') -escaped_db_host=$(printf '%s\n' "$DB_HOST" | sed 's:[\/&]:\\&:g') +if [ ! -f "$WP_CONFIG" ]; then + echo "Error: wp-config.php not found at $WP_CONFIG. Please ensure WordPress is installed correctly." + exit 1 +fi + +sudo cp $WP_CONFIG $WP_CONFIG.bak echo "Updating wp-config.php with new database credentials..." -# Update wp-config.php with new database credentials using more precise sed commands -sudo sed -i.bak -e "s/define( *'DB_NAME'.*/define('DB_NAME', '${escaped_db_name}');/" \ - -e "s/define( *'DB_USER'.*/define('DB_USER', '${escaped_db_user}');/" \ - -e "s/define( *'DB_PASSWORD'.*/define('DB_PASSWORD', '${escaped_db_password}');/" \ - -e "s/define( *'DB_HOST'.*/define('DB_HOST', '${escaped_db_host}');/" $WP_CONFIG +# Update wp-config.php with new database credentials using the reusable function +search_and_replace "define( *'DB_NAME', '.*' *);" "define('DB_NAME', '${DB_NAME}');" "$WP_CONFIG" +search_and_replace "define( *'DB_USER', '.*' *);" "define('DB_USER', '${DB_USER}');" "$WP_CONFIG" +search_and_replace "define( *'DB_PASSWORD', '.*' *);" "define('DB_PASSWORD', '${DB_PASSWORD}');" "$WP_CONFIG" +search_and_replace "define( *'DB_HOST', '.*' *);" "define('DB_HOST', '${DB_HOST}');" "$WP_CONFIG" # Check if wp-config.php was updated successfully -if grep -q "$DB_NAME" "$WP_CONFIG" && grep -q "$DB_USER" "$WP_CONFIG" && grep -q "$DB_PASSWORD" "$WP_CONFIG"; then +if grep -q "define( *'DB_NAME', '${DB_NAME}' *);" "$WP_CONFIG" && \ + grep -q "define( *'DB_USER', '${DB_USER}' *);" "$WP_CONFIG" && \ + grep -q "define( *'DB_PASSWORD', '${DB_PASSWORD}' *);" "$WP_CONFIG"; then echo "wp-config.php updated successfully with new database credentials." else echo "Failed to update wp-config.php. Please check the file manually." exit 1 -fi \ No newline at end of file +fi diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100644 index 0000000..01d684d --- /dev/null +++ b/scripts/utils.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Reusable function for search and replace operations +# Usage: search_and_replace "search_pattern" "replace_string" "file_path" +search_and_replace() { + local search_pattern="$1" + local replace_string="$2" + local file_path="$3" + + if [ ! -f "$file_path" ]; then + echo "Error: File not found at $file_path" + return 1 + fi + + # Escape special characters for sed + local escaped_replace_string=$(printf '%s\n' "$replace_string" | sed 's:[\/&]:\\&:g') + + sudo sed -i.bak "s/${search_pattern}/${escaped_replace_string}/" "$file_path" + + if grep -q "$replace_string" "$file_path"; then + echo "Successfully replaced '${search_pattern}' with '${replace_string}' in '$file_path'." + else + echo "Failed to replace '${search_pattern}' in '$file_path'." + return 1 + fi +} diff --git a/scripts/wp-search-replace.sh b/scripts/wp-search-replace.sh new file mode 100644 index 0000000..ce5176f --- /dev/null +++ b/scripts/wp-search-replace.sh @@ -0,0 +1,193 @@ +#!/bin/bash +# +# Wrapper script for WP-CLI's search-replace command. +# Safely runs 'wp search-replace' with the correct user and path context. +# + +# --- Configuration --- +set -e +set -u +set -o pipefail + +# Colors for output messages +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# --- Default Values --- +WP_ROOT="/var/www/webroot/ROOT" # Default WordPress root directory +WEB_USER="litespeed" # Default web server user +WP_CLI_PATH="/usr/local/bin/wp" # Default path to WP-CLI + +# --- Helper Functions --- + +# Print informational messages +info() { + printf "${BLUE}[INFO] %s${NC}\n" "$@" +} + +# Print success messages +success() { + printf "${GREEN}[SUCCESS] %s${NC}\n" "$@" +} + +# Print warning messages +warning() { + printf "${YELLOW}[WARNING] %s${NC}\n" "$@" +} + +# Print error messages and exit +error_exit() { + printf "${RED}[ERROR] %s${NC}\n" "$@" >&2 + exit 1 +} + +# Function to display usage information +usage() { + printf "Usage: %s [options...]\n" "$0" + printf "\n" + printf "A wrapper for the 'wp search-replace' command that automatically handles user and path context.\n" + printf "All options after are passed directly to 'wp search-replace'.\n" + printf "\n" + printf "Required Arguments:\n" + printf " The string to search for.\n" + printf " The string to replace it with.\n" + printf "\n" + printf "Common WP-CLI Options (pass these after ):\n" + printf " [...] One or more database tables to restrict the search to.\n" + printf " --dry-run Run the search-replace without making any changes.\n" + printf " --all-tables Search through all tables registered to \$wpdb.\n" + printf " --network Search and replace through all sites in a multisite network.\n" + printf " --recurse-objects Enable recursing into objects to replace strings.\n" + printf " --report-changed-only Only report changes, not every table checked.\n" + printf " --skip-columns= Comma-separated list of columns to skip.\n" + printf "\n" + printf "Wrapper-Specific Options:\n" + printf " --wproot=PATH Override the default WordPress root directory (%s).\n" "$WP_ROOT" + printf " --webuser=USER Override the default web server user (%s).\n" "$WEB_USER" + printf " -h, --help Display this help message.\n" + printf "\n" + printf "Example:\n" + printf " %s 'http://old-domain.com' 'https://new-domain.com' --all-tables --dry-run\n" "$0" + exit 1 +} + +# --- Argument Parsing --- +# Separate wrapper arguments from WP-CLI arguments +WP_CLI_ARGS=() +while [[ $# -gt 0 ]]; do + case "$1" in + -h|--help) + usage + ;; + --wproot=*) + WP_ROOT="${1#*=}" + shift + ;; + --webuser=*) + WEB_USER="${1#*=}" + shift + ;; + *) + # Collect all other arguments for WP-CLI + WP_CLI_ARGS+=("$1") + shift + ;; + esac +done + +# Check for mandatory arguments +if [[ ${#WP_CLI_ARGS[@]} -lt 2 ]]; then + error_exit "Missing required and arguments." +fi + +info "Wrapper settings: WP_ROOT='${WP_ROOT}', WEB_USER='${WEB_USER}'" +info "Arguments passed to WP-CLI: ${WP_CLI_ARGS[*]}" + +# --- Prerequisite Checks --- +info "Checking prerequisites..." +if [[ ! -d "$WP_ROOT" ]]; then error_exit "WordPress root directory '$WP_ROOT' does not exist."; fi +if ! command -v "$WP_CLI_PATH" &> /dev/null; then + warning "WP-CLI not found at $WP_CLI_PATH." + if ! command -v "wp" &> /dev/null; then + error_exit "WP-CLI not found at $WP_CLI_PATH or in system PATH. Please install WP-CLI." + else + WP_CLI_PATH="wp" # Fallback to wp in path + info "Found 'wp' in system PATH. Using that." + fi +fi +if ! id "$WEB_USER" &>/dev/null; then error_exit "Web user '$WEB_USER' does not exist."; fi +success "Prerequisites seem OK." + + +# --- WP-CLI Execution Context Setup --- +# This logic is adapted from install-wordpress.sh to ensure commands run as the correct user. +WP_RUN_ARGS=("--path=$WP_ROOT") +SUDO_CMD="" +WP_EXECUTABLE="$WP_CLI_PATH" + +if [[ "$(id -u)" -eq 0 ]]; then + info "Script is running as root. Attempting to run WP-CLI as '$WEB_USER'." + # Use 'sudo -n' to check for passwordless sudo access. + if sudo -n -u "$WEB_USER" "$WP_EXECUTABLE" --info --skip-update --quiet "${WP_RUN_ARGS[@]}" &>/dev/null; then + SUDO_CMD="sudo -u $WEB_USER" + info "WP-CLI will be executed as '$WEB_USER'." + else + warning "Could not execute WP-CLI as '$WEB_USER' without a password. Falling back to running as root." + WP_RUN_ARGS+=("--allow-root") + fi +else + if [[ "$(id -un)" != "$WEB_USER" ]]; then + info "Script is running as non-root user '$(id -un)'. Checking for sudo access to run as '$WEB_USER'." + if sudo -n -u "$WEB_USER" "$WP_EXECUTABLE" --info --skip-update --quiet "${WP_RUN_ARGS[@]}" &>/dev/null; then + SUDO_CMD="sudo -u $WEB_USER" + info "Successfully configured sudo execution for WP-CLI as '$WEB_USER'." + else + error_exit "Unable to execute WP-CLI as '$WEB_USER'. Ensure the current user has passwordless sudo access, or run this script as '$WEB_USER' or 'root'." + fi + else + info "Script is already running as the web user ('$WEB_USER')." + fi +fi + +info "WP-CLI final execution command prefix: [${SUDO_CMD:-direct}]" + +# --- Execute Command --- +info "Executing 'wp search-replace'..." + +# Build command array safely to preserve argument quoting +CMD=() +if [[ -n "$SUDO_CMD" ]]; then + # shellcheck disable=SC2206 + CMD=($SUDO_CMD) # split sudo command into array elements +fi +CMD+=("$WP_EXECUTABLE" "search-replace") +CMD+=("${WP_CLI_ARGS[@]}") +CMD+=("${WP_RUN_ARGS[@]}") + +# Run the command and capture exit status +"${CMD[@]}" +STATUS=$? + +if [[ $STATUS -eq 0 ]]; then + success "'wp search-replace' command completed successfully." + # Optional: flush caches to ensure changes propagate + info "Flushing WordPress caches (wp cache flush)..." + FLUSH_CMD=( ) + if [[ -n "$SUDO_CMD" ]]; then + # shellcheck disable=SC2206 + FLUSH_CMD=($SUDO_CMD) + fi + FLUSH_CMD+=("$WP_EXECUTABLE" "cache" "flush" "${WP_RUN_ARGS[@]}") + "${FLUSH_CMD[@]}" || warning "Cache flush command returned non-zero exit status." +else + error_exit "The 'wp search-replace' command failed with exit code $STATUS." +fi + +exit $STATUS + + + +