Deploying WordPress from LocalWP to AWS Lightsail: Manual & Automated Script
This guide explains how to deploy a WordPress site from LocalWP to AWS Lightsail, either manually or using a robust Bash deployment script.
π Before You Begin
ποΈ Set Up AWS Lightsail Instance
- Go to Amazon Lightsail.
- Create a new instance:
- Choose Linux/Unix platform.
- Choose the WordPress (Bitnami) blueprint.
- Select a plan (the $5.00/mo plan is fine for testing).
- After creation:
- Note down the public IP.
- Download your .pem SSH key.
- Enable port 80 (HTTP) and 443 (HTTPS) under the Networking tab.
π§ͺ Start Site in LocalWP
- Open LocalWP.
- Start your site (click the green "Start Site" button).
- Click the βSite Shellβ button β this opens a shell where WP-CLI is available and your site is active.
β Part 1: Manual Deployment β Step-by-Step
The manual process uses shell commands to move your site to AWS Lightsail.
π 1. Prepare Your SSH Key
chmod 400 ~/Downloads/LightsailDefaultKey-eu-central-1.pem
πΎ 2. Export the Local Database
Inside the Site Shell from LocalWP:
wp db export ~/Local\ Sites/buzybuzz/app/public/local-site.sql --add-drop-table
π¦ 3. Zip the wp-content Folder
cd "~/Local Sites/buzybuzz/app/public"
zip -r wp-content.zip wp-content
βοΈ 4. Upload Files to Lightsail
scp -i ~/Downloads/LightsailDefaultKey-eu-central-1.pem wp-content.zip bitnami@63.178.84.97:~
scp -i ~/Downloads/LightsailDefaultKey-eu-central-1.pem local-site.sql bitnami@63.178.84.97:~
π₯ 5. SSH Into Lightsail
ssh -i ~/Downloads/LightsailDefaultKey-eu-central-1.pem bitnami@63.178.84.97
π§Ή 6. Remove Old wp-content
cd /home/bitnami/stack/wordpress
rm -rf wp-content/*
π§― 7. Unzip wp-content
unzip ~/wp-content.zip -d .
π 8. Set File Permissions
sudo chown -R bitnami:daemon wp-content
sudo find wp-content -type d -exec chmod 775 {} \;
sudo find wp-content -type f -exec chmod 664 {} \;
π 9. Get Database Password
grep DB_PASSWORD wp-config.php
π£ 10. Drop and Recreate DB
/opt/bitnami/mariadb/bin/mariadb -u bn_wordpress -p
In the MariaDB shell:
DROP DATABASE IF EXISTS bitnami_wordpress;
CREATE DATABASE bitnami_wordpress;
EXIT;
π₯ 11. Import SQL Dump
/opt/bitnami/mariadb/bin/mariadb -u bn_wordpress -p bitnami_wordpress < ~/local-site.sql
π 12. Replace URLs
sudo wp search-replace "https://buzybuzz.local" "http://63.178.84.97" --all-tables
sudo wp search-replace "http://buzybuzz.local" "http://63.178.84.97" --all-tables
sudo wp search-replace "buzybuzz.local" "63.178.84.97" --all-tables
πΏ 13. Flush Cache and Permalinks
sudo wp cache flush
sudo wp rewrite flush
β Visit your deployed site at:
π§Ύ Part 2: Automated Deployment Script
This script automates all the steps above with a few variables to update.
π Deployment Script
# !/bin/bash
set -euo pipefail
trap 'echo -e "\nβ Error occurred during deployment. Exiting."; exit 1' ERR
# === CONFIGURATION ===
PEM_PATH="$HOME/Downloads/LightsailDefaultKey-eu-central-1.pem"
LOCAL_PATH="$HOME/Local Sites/buzybuzz/app/public"
REMOTE_USER=bitnami
REMOTE_IP=63.178.84.97
SITE_NAME="buzybuzz"
LOCAL_DOMAIN="http://$SITE_NAME.local"
REMOTE_DOMAIN="http://$REMOTE_IP"
REMOTE_WP_DIR="/home/bitnami/stack/wordpress"
REMOTE_HOME="/home/bitnami"
SQL_DUMP="$LOCAL_PATH/local-site.sql"
ZIP_FILE="$LOCAL_PATH/wp-content.zip"
# === Ensure PEM key has correct permissions ===
chmod 400 "$PEM_PATH"
echo "π€ Exporting DB..."
cd "$LOCAL_PATH" && wp db export "$SQL_DUMP" --add-drop-table
# STEP 4: Zip wp-content
echo "ποΈ Zipping wp-content..."
cd "$LOCAL_PATH"
zip -r -q "wp-content.zip" "wp-content"
# === Validate local files ===
echo "β
Validating SQL and zip files..."
[[ -f "$ZIP_FILE" ]] || { echo "β Missing: $ZIP_FILE"; exit 1; }
[[ -f "$SQL_DUMP" ]] || { echo "β Missing: $SQL_DUMP"; exit 1; }
# === Upload files to server ===
echo "π€ Uploading to server..."
scp -i "$PEM_PATH" "$ZIP_FILE" "$SQL_DUMP" "$REMOTE_USER@$REMOTE_IP:$REMOTE_HOME"
# === Run remote deployment ===
echo "π Running remote deployment..."
ssh -i "$PEM_PATH" "$REMOTE_USER@$REMOTE_IP" \
REMOTE_WP_DIR="$REMOTE_WP_DIR" \
REMOTE_HOME="$REMOTE_HOME" \
SITE_NAME="$SITE_NAME" \
REMOTE_IP="$REMOTE_IP" \
REMOTE_DOMAIN="$REMOTE_DOMAIN" \
'bash -s' <<'EOF'
set -euo pipefail
trap 'echo -e "\nβ Remote deployment failed. Exiting."; exit 1' ERR
echo "π§Ή Removing existing wp-content..."
cd "$REMOTE_WP_DIR"
rm -rf wp-content/*
echo "π¦ Unzipping wp-content..."
unzip -o "$REMOTE_HOME/wp-content.zip" -d "$REMOTE_WP_DIR"
echo "π Setting permissions on wp-content..."
sudo chown -R bitnami:daemon wp-content
sudo find wp-content -type d -exec chmod 775 {} \;
sudo find wp-content -type f -exec chmod 664 {} \;
echo "π Reading DB password from wp-config.php..."
PASS=$(grep DB_PASSWORD "$REMOTE_WP_DIR/wp-config.php" | cut -d "'" -f 4)
if [[ -z "$PASS" ]]; then
echo "β Could not extract DB_PASSWORD from wp-config.php"
exit 1
fi
echo "π Dropping and recreating database..."
/opt/bitnami/mariadb/bin/mariadb -u bn_wordpress -p"$PASS" -e "DROP DATABASE IF EXISTS bitnami_wordpress; CREATE DATABASE bitnami_wordpress;"
echo "π
Importing database..."
/opt/bitnami/mariadb/bin/mariadb -u bn_wordpress -p"$PASS" bitnami_wordpress < "$REMOTE_HOME/local-site.sql"
echo "π Replacing URLs..."
sudo wp search-replace "https://$SITE_NAME.local" "$REMOTE_DOMAIN" --all-tables
sudo wp search-replace "http://$SITE_NAME.local" "$REMOTE_DOMAIN" --all-tables
sudo wp search-replace "$SITE_NAME.local" "$REMOTE_IP" --all-tables
echo "π§Ό Flushing cache and rewrites..."
sudo wp cache flush
sudo wp rewrite flush
echo "β
Remote deployment complete!"
EOF
β Summary
- ποΈ Make sure your Lightsail instance is correctly set up with Bitnami WordPress and open ports.
- π§ͺ Ensure your LocalWP site is running and Site Shell is open before running any export commands.
- π οΈ The manual method helps you understand each step.
- π The automated script streamlines the entire process with built-in validation and error handling.