fix: WP-CLI path detection - prioritize /usr/local/bin/wp, fix sudo -u path loss across all scripts

main
Anthony 2026-02-28 16:08:21 +08:00
parent 4c54fa106f
commit 20af8ad386
4 changed files with 32 additions and 17 deletions

View File

@ -314,15 +314,20 @@ step_wp_object_cache() {
return 0
fi
# Guard: WP-CLI available
if ! command -v wp >/dev/null 2>&1; then
warning "WP-CLI not found — skipping WP object cache config. Install WP-CLI first."
# Guard: WP-CLI available — resolve full path so sudo -u retains it
local wp_bin
for _wp in "/usr/local/bin/wp" "/usr/bin/wp" "/home/litespeed/bin/wp"; do
if [ -x "$_wp" ]; then wp_bin="$_wp"; break; fi
done
[ -z "${wp_bin:-}" ] && wp_bin=$(command -v wp 2>/dev/null || echo "")
if [ -z "${wp_bin:-}" ]; then
warning "WP-CLI not found — skipping WP object cache config. Expected at /usr/local/bin/wp"
return 0
fi
# Guard: LiteSpeed Cache plugin active (run as litespeed to get correct WP context)
# Guard: LiteSpeed Cache plugin active (run as litespeed with full wp path)
local plugin_status
plugin_status=$(sudo -u "$WEB_USER" -s wp plugin is-active litespeed-cache --path="$WP_ROOT" 2>/dev/null && echo "active" || echo "inactive")
plugin_status=$(sudo -u "$WEB_USER" "$wp_bin" plugin is-active litespeed-cache --path="$WP_ROOT" 2>/dev/null && echo "active" || echo "inactive")
if [[ "$plugin_status" != "active" ]]; then
warning "LiteSpeed Cache plugin is not active in WordPress — skipping object cache config."
warning "Activate it at: WordPress Admin > Plugins > LiteSpeed Cache"

View File

@ -5,12 +5,17 @@
# Find WP-CLI location
WP_CLI_PATH=""
if [ -f "/home/litespeed/bin/wp" ]; then
WP_CLI_PATH="/home/litespeed/bin/wp"
elif command -v wp >/dev/null 2>&1; then
WP_CLI_PATH="wp"
else
echo '{"error": "WP-CLI not found. Please ensure WP-CLI is installed and accessible."}'
for _wp_candidate in "/usr/local/bin/wp" "/usr/bin/wp" "/home/litespeed/bin/wp"; do
if [ -x "$_wp_candidate" ]; then
WP_CLI_PATH="$_wp_candidate"
break
fi
done
if [ -z "$WP_CLI_PATH" ]; then
WP_CLI_PATH=$(command -v wp 2>/dev/null || echo "")
fi
if [ -z "$WP_CLI_PATH" ]; then
echo '{"error": "WP-CLI not found. Expected at /usr/local/bin/wp — run: which wp"}'
exit 1
fi

View File

@ -1,14 +1,19 @@
#!/bin/bash
# Check if WP-CLI is available
if [ ! -f "/home/litespeed/bin/wp" ]; then
echo "WP-CLI not found"
# Find WP-CLI binary
WP_CLI_PATH=""
for _wp in "/usr/local/bin/wp" "/usr/bin/wp" "/home/litespeed/bin/wp"; do
if [ -x "$_wp" ]; then WP_CLI_PATH="$_wp"; break; fi
done
[ -z "$WP_CLI_PATH" ] && WP_CLI_PATH=$(command -v wp 2>/dev/null || echo "")
if [ -z "$WP_CLI_PATH" ]; then
echo "WP-CLI not found. Expected at /usr/local/bin/wp"
exit 1
fi
# Function to run WP-CLI commands
run_wp_command() {
/home/litespeed/bin/wp --path=/var/www/webroot/ROOT "$@"
"$WP_CLI_PATH" --path=/var/www/webroot/ROOT "$@"
}
# Check for correct number of arguments

View File

@ -61,7 +61,7 @@ function get_lsws_version() {
// Find WP-CLI binary
function find_wp_cli() {
$candidates = ['/home/litespeed/bin/wp', '/usr/local/bin/wp', '/usr/bin/wp'];
$candidates = ['/usr/local/bin/wp', '/usr/bin/wp', '/home/litespeed/bin/wp'];
foreach ($candidates as $path) {
if (file_exists($path) && is_executable($path)) {
return $path;
@ -118,7 +118,7 @@ function toggle_cache($enable = true) {
$wp_root = find_wp_root();
if (!$wp) {
return ['ok' => false, 'error' => 'WP-CLI not found. Check /home/litespeed/bin/wp or /usr/local/bin/wp'];
return ['ok' => false, 'error' => 'WP-CLI not found. Expected at /usr/local/bin/wp — run: which wp'];
}
if (!$wp_root) {
return ['ok' => false, 'error' => 'WordPress not found at /var/www/webroot/ROOT'];