Fix root folder issue

main
Anthony 2025-07-24 23:56:28 +08:00
parent d56286d5b9
commit 56fe5cc051
1 changed files with 67 additions and 1 deletions

View File

@ -35,6 +35,7 @@ DB_ROOT_PASS="" # Require user to provide this for security
WEB_USER="litespeed" # Web server user
WEB_GROUP="litespeed" # Web server group
WP_CLI_PATH="/usr/local/bin/wp" # Path to WP-CLI executable
NEEDS_OWNERSHIP_CORRECTION="false" # Flag to track if ownership correction is needed
# --- Helper Functions ---
@ -99,6 +100,16 @@ generate_password() {
openssl rand -base64 16
}
# Function to correct ownership after WP-CLI operations if needed
correct_ownership_if_needed() {
if [[ "$NEEDS_OWNERSHIP_CORRECTION" == "true" ]]; then
info "Correcting file ownership after WP-CLI operation..."
if ! sudo chown -R "${WEB_USER}:${WEB_GROUP}" "$WP_ROOT" 2>/dev/null; then
warning "Failed to correct some file ownership. Some files may still be owned by root."
fi
fi
}
# Function to clean up temporary files
# Define WP_CLI_CONFIG_PATH early so cleanup function knows about it
WP_CLI_CONFIG_PATH="/tmp/wp-cli-config-$RANDOM.yml"
@ -238,8 +249,10 @@ if [[ "$(id -u)" -eq 0 ]]; then
SUDO_CMD="sudo -u $WEB_USER"
info "WP-CLI will be executed via sudo as '$WEB_USER'."
else
warning "Failed to execute WP-CLI as '$WEB_USER' without password. Falling back to running as root with --allow-root. Resulting files may be owned by root."
warning "Failed to execute WP-CLI as '$WEB_USER' without password. Falling back to running as root with --allow-root. Files will be corrected to proper ownership after creation."
WP_RUN_ARGS+=("--allow-root")
# Set flag to indicate we need ownership correction after WP-CLI operations
NEEDS_OWNERSHIP_CORRECTION="true"
fi
else
# Script is NOT running as root.
@ -446,6 +459,7 @@ if [[ ! -f "index.php" || ! -f "wp-includes/version.php" || ! -d "wp-admin" ]];
error_exit "Failed to download WordPress core files using WP-CLI."
fi
success "WordPress core downloaded."
correct_ownership_if_needed
else
info "WordPress core files seem to exist. Skipping download."
fi
@ -600,10 +614,12 @@ if ! $SUDO_CMD $WP_EXECUTABLE core is-installed "${WP_RUN_ARGS[@]}"; then
"${WP_RUN_ARGS[@]}"; then
error_exit "WordPress core installation failed using WP-CLI."
fi
correct_ownership_if_needed
info "Removing default plugins (Akismet, Hello Dolly)..."
# Use determined $SUDO_CMD, $WP_EXECUTABLE, and $WP_RUN_ARGS
$SUDO_CMD $WP_EXECUTABLE plugin delete akismet hello "${WP_RUN_ARGS[@]}" --quiet || warning "Could not delete default plugins (might not exist)."
correct_ownership_if_needed
# Install LiteSpeed Cache plugin (without activating yet)
info "Installing LiteSpeed Cache plugin..."
@ -636,6 +652,7 @@ if ! $SUDO_CMD $WP_EXECUTABLE core is-installed "${WP_RUN_ARGS[@]}"; then
info "Activating LiteSpeed Cache plugin..."
if $SUDO_CMD $WP_EXECUTABLE plugin activate litespeed-cache "${WP_RUN_ARGS[@]}"; then
success "LiteSpeed Cache plugin activated successfully."
correct_ownership_if_needed
else
warning "Failed to activate LiteSpeed Cache plugin after installation."
fi
@ -679,6 +696,7 @@ if ! $SUDO_CMD $WP_EXECUTABLE core is-installed "${WP_RUN_ARGS[@]}"; then
$SUDO_CMD $WP_EXECUTABLE theme activate "$FIRST_THEME" "${WP_RUN_ARGS[@]}" || warning "Could not activate $FIRST_THEME theme"
fi
fi
correct_ownership_if_needed
# Create .htaccess file for WordPress permalink functionality
info "Creating .htaccess file for URL rewriting..."
@ -766,6 +784,7 @@ EOF
info "Configuring WordPress permalink structure..."
$SUDO_CMD $WP_EXECUTABLE rewrite structure '/%postname%/' "${WP_RUN_ARGS[@]}" || warning "Could not set permalink structure"
$SUDO_CMD $WP_EXECUTABLE rewrite flush "${WP_RUN_ARGS[@]}" || warning "Could not flush rewrite rules"
correct_ownership_if_needed
# WP-CLI operations above might have recreated or modified .htaccess as the user executing WP-CLI.
# To enforce consistent ownership, reset it to the designated web user/group.
@ -971,6 +990,53 @@ fi
# --- Final Summary ---
success "WordPress setup process completed!"
# --- Final Ownership Correction ---
info "Performing final ownership correction to ensure all files are owned by ${WEB_USER}:${WEB_GROUP}..."
cd "$WP_ROOT" || error_exit "Failed to change directory to $WP_ROOT for final ownership correction"
# Comprehensive ownership fix for all WordPress files and directories
if ! sudo chown -R "${WEB_USER}:${WEB_GROUP}" "$WP_ROOT"; then
warning "Failed to set ownership on some files during final correction. Some files may still be owned by root."
else
success "Final ownership correction completed successfully."
fi
# Specifically ensure critical files are properly owned
critical_files=("wp-config.php" ".htaccess" "index.php")
for file in "${critical_files[@]}"; do
if [[ -f "$file" ]]; then
sudo chown "${WEB_USER}:${WEB_GROUP}" "$file" || warning "Failed to set ownership on $file"
fi
done
# Ensure LiteSpeed Cache directories have correct ownership if they exist
if [[ -d "wp-content/litespeed" ]]; then
info "Correcting LiteSpeed Cache directory ownership..."
sudo chown -R "${WEB_USER}:${WEB_GROUP}" "wp-content/litespeed" || warning "Failed to set ownership on LiteSpeed Cache directory"
fi
# Ensure uploads directory has correct ownership if it exists
if [[ -d "wp-content/uploads" ]]; then
info "Correcting uploads directory ownership..."
sudo chown -R "${WEB_USER}:${WEB_GROUP}" "wp-content/uploads" || warning "Failed to set ownership on uploads directory"
fi
# Final verification - show file ownership status
info "Verifying file ownership in WordPress root directory..."
if command_exists ls; then
info "Current file ownership in $WP_ROOT:"
ls -la "$WP_ROOT" | head -20 # Show first 20 files
# Count files with wrong ownership
wrong_ownership_count=$(find "$WP_ROOT" -maxdepth 2 \( ! -user "$WEB_USER" -o ! -group "$WEB_GROUP" \) -type f 2>/dev/null | wc -l)
if [[ "$wrong_ownership_count" -gt 0 ]]; then
warning "$wrong_ownership_count files still have incorrect ownership. You may need to run: sudo chown -R ${WEB_USER}:${WEB_GROUP} $WP_ROOT"
else
success "All files now have correct ownership (${WEB_USER}:${WEB_GROUP})"
fi
fi
printf "\n--- ${YELLOW}Installation Summary${NC} ---\n"
printf "Site URL: ${GREEN}https://%s${NC}\n" "$DOMAIN"
printf "WP Root: ${GREEN}%s${NC}\n" "$WP_ROOT"