From a2b82b866376332597967a6099b6aa0b076c3f32 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sat, 12 Jul 2025 00:51:13 +0800 Subject: [PATCH] Fix wordpress installation 404 error --- scripts/install-wordpress.sh | 46 ++++++++++++++++++----------- scripts/templates/README.md | 34 +++++++++++++++++++++ scripts/templates/htaccess.template | 45 ++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 18 deletions(-) create mode 100644 scripts/templates/README.md create mode 100644 scripts/templates/htaccess.template diff --git a/scripts/install-wordpress.sh b/scripts/install-wordpress.sh index eba250e..421d8ed 100644 --- a/scripts/install-wordpress.sh +++ b/scripts/install-wordpress.sh @@ -685,7 +685,20 @@ if ! $SUDO_CMD $WP_EXECUTABLE core is-installed "${WP_RUN_ARGS[@]}"; then # Create .htaccess file for WordPress permalink functionality info "Creating .htaccess file for URL rewriting..." if [[ ! -f ".htaccess" ]]; then - cat > .htaccess <<'EOF' || warning "Failed to create .htaccess file" + # Define the path to the .htaccess template file + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + HTACCESS_TEMPLATE="$SCRIPT_DIR/templates/htaccess.template" + + # Create temporary .htaccess file + TEMP_HTACCESS=$(mktemp) || error_exit "Failed to create temporary file for .htaccess" + + # Check if the template file exists and use it, otherwise use inline fallback + if [[ -f "$HTACCESS_TEMPLATE" ]]; then + info "Using external .htaccess template: $HTACCESS_TEMPLATE" + cp "$HTACCESS_TEMPLATE" "$TEMP_HTACCESS" || error_exit "Failed to copy .htaccess template to temporary file" + else + warning "External template not found at $HTACCESS_TEMPLATE, using inline template" + cat > "$TEMP_HTACCESS" <<'EOF' || error_exit "Failed to write .htaccess content to temporary file" # BEGIN LSCACHE ## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ## @@ -701,10 +714,6 @@ RewriteCond %{QUERY_STRING} action=async_litespeed RewriteRule .* - [E=noabort:1] ### marker ASYNC end ### -### marker CACHE RESOURCE start ### -RewriteRule wp-content/.*/[^/]*(responsive|css|js|dynamic|loader|fonts)\.php - [E=cache-control:max-age=3600] -### marker CACHE RESOURCE end ### - ### marker DROPQS start ### CacheKeyModify -qs:fbclid CacheKeyModify -qs:gclid @@ -735,20 +744,21 @@ RewriteRule . /index.php [L] # END WordPress -# BEGIN LiteSpeed -# The directives (lines) between "BEGIN LiteSpeed" and "END LiteSpeed" are -# dynamically generated, and should only be modified via WordPress filters. -# Any changes to the directives between these markers will be overwritten. - -SetEnv noabort 1 - -# END LiteSpeed EOF - # Set appropriate permissions for .htaccess - if [[ -f ".htaccess" ]]; then - sudo chmod 644 .htaccess - sudo chown "${WEB_USER}:${WEB_GROUP}" .htaccess - success ".htaccess file created and configured for LiteSpeed." + fi + + # Move the temporary file to the final location with proper ownership + sudo mv "$TEMP_HTACCESS" ".htaccess" || error_exit "Failed to move .htaccess file to final location" + + # Set appropriate permissions and ownership for .htaccess + sudo chmod 644 .htaccess || error_exit "Failed to set permissions on .htaccess" + sudo chown "${WEB_USER}:${WEB_GROUP}" .htaccess || error_exit "Failed to set ownership on .htaccess" + + # Verify the ownership was set correctly + if [[ "$(stat -c '%U:%G' .htaccess)" == "${WEB_USER}:${WEB_GROUP}" ]]; then + success ".htaccess file created and configured for LiteSpeed with proper ownership (${WEB_USER}:${WEB_GROUP})." + else + warning ".htaccess file created but ownership verification failed. Current ownership: $(stat -c '%U:%G' .htaccess)" fi else info ".htaccess file already exists. Skipping creation." diff --git a/scripts/templates/README.md b/scripts/templates/README.md new file mode 100644 index 0000000..bcbaaf4 --- /dev/null +++ b/scripts/templates/README.md @@ -0,0 +1,34 @@ +# WordPress Installation Templates + +This directory contains template files used by the WordPress installation script. + +## Files + +### `htaccess.template` +This file contains the default `.htaccess` configuration that will be applied to new WordPress installations. + +**Features:** +- LiteSpeed Cache plugin configuration +- Standard WordPress permalink structure +- LiteSpeed-specific optimizations +- Cache optimization directives + +**Usage:** +The `install-wordpress.sh` script will automatically use this template file if it exists. If the template file is missing, the script will fall back to using an inline version. + +**Customization:** +You can modify this template file to customize the default `.htaccess` configuration for your WordPress installations: + +1. Edit `htaccess.template` with your preferred configuration +2. Run the WordPress installation script normally +3. The script will use your customized template + +**Template Location:** +The script looks for the template at: `scripts/templates/htaccess.template` + +## Template System Benefits + +- **Maintainability**: Easier to update and modify .htaccess configurations +- **Version Control**: Track changes to .htaccess configurations +- **Consistency**: Ensure all WordPress installations use the same base configuration +- **Fallback**: Script continues to work even if template files are missing \ No newline at end of file diff --git a/scripts/templates/htaccess.template b/scripts/templates/htaccess.template new file mode 100644 index 0000000..438929d --- /dev/null +++ b/scripts/templates/htaccess.template @@ -0,0 +1,45 @@ +# BEGIN LSCACHE +## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ## + +RewriteEngine on +CacheLookup on +RewriteRule .* - [E=Cache-Control:no-autoflush] +RewriteRule litespeed/debug/.*\.log$ - [F,L] +RewriteRule \.litespeed_conf\.dat - [F,L] + +### marker ASYNC start ### +RewriteCond %{REQUEST_URI} /wp-admin/admin-ajax\.php +RewriteCond %{QUERY_STRING} action=async_litespeed +RewriteRule .* - [E=noabort:1] +### marker ASYNC end ### + +### marker DROPQS start ### +CacheKeyModify -qs:fbclid +CacheKeyModify -qs:gclid +CacheKeyModify -qs:utm* +CacheKeyModify -qs:_ga +### marker DROPQS end ### + + +## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ## +# END LSCACHE +# BEGIN NON_LSCACHE +## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ## +## LITESPEED WP CACHE PLUGIN - Do not edit the contents of this block! ## +# END NON_LSCACHE + +# BEGIN WordPress +# The directives (lines) between "BEGIN WordPress" and "END WordPress" are +# dynamically generated, and should only be modified via WordPress filters. +# Any changes to the directives between these markers will be overwritten. + +RewriteEngine On +RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] +RewriteBase / +RewriteRule ^index\.php$ - [L] +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule . /index.php [L] + + +# END WordPress \ No newline at end of file