Skip to content

Instantly share code, notes, and snippets.

@bouroo
Last active August 24, 2024 06:00
Show Gist options
  • Save bouroo/feeb540c6c48dff9e30785fa0f9c6853 to your computer and use it in GitHub Desktop.
Save bouroo/feeb540c6c48dff9e30785fa0f9c6853 to your computer and use it in GitHub Desktop.
updates the LiteSpeed Cache plugin for all WordPress installations on a DirectAdmin server
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Script Name: update-litespeed-cache.sh
# Description: This script updates the LiteSpeed Cache plugin for all WordPress
# installations on a DirectAdmin server. It checks for the WP-CLI
# tool and installs it if not found, then updates the specified plugin
# for each user's domains.
#
# Author: Kawin Viriyaprasopsook
# Email: kawin.v@kkumail.com
# Date: 2024-08-24
# Version: 1.1
# ------------------------------------------------------------------------------
# Function to install WP-CLI if not installed
install_wp_cli() {
echo "WP-CLI not found. Installing WP-CLI..."
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
echo "WP-CLI installed successfully."
}
# Check if WP-CLI is installed and get its path
WP_CLI_PATH=$(command -v wp)
if [ -z "$WP_CLI_PATH" ]; then
install_wp_cli
WP_CLI_PATH="/usr/local/bin/wp" # Set the path after installation
else
echo "WP-CLI is already installed at $WP_CLI_PATH."
fi
# Define the path to the DirectAdmin user homes
DA_USERS_DIR="/home"
# Loop through each user directory in DirectAdmin
find "$DA_USERS_DIR" -type d -maxdepth 1 -print0 | while IFS= read -r -d $'\0' user; do
# Define the path to the WordPress installation
WP_PATH="$user/domains"
# Check if the user has any domains and loop through them
find "$WP_PATH" -type d -maxdepth 1 -print0 | while IFS= read -r -d $'\0' domain; do
# Define the path to the WordPress installation
WP_INSTALL_DIR="$WP_PATH/$domain/public_html"
# Check if the wp-config.php file exists
if [ -f "$WP_INSTALL_DIR/wp-config.php" ]; then
echo "Updating LiteSpeed Cache plugin for $domain..."
# Navigate to the WordPress installation directory
cd "$WP_INSTALL_DIR" || continue
# Update the LiteSpeed Cache plugin as the owner user
sudo -u $(basename "$user") "$WP_CLI_PATH" plugin update litespeed-cache
if [ $? -eq 0 ]; then
echo "Successfully updated LiteSpeed Cache for $domain."
else
echo "Failed to update LiteSpeed Cache for $domain."
fi
else
echo "No WordPress installation found for $domain."
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment