Files
alrahma_sunday_school/deploy_alrahma.sh
T
root f8f00c70c8
Tests / PHPUnit (push) Failing after 35s
update php
2026-07-29 19:03:57 -04:00

231 lines
7.0 KiB
Bash

#!/bin/bash
# ===============================
# Al Rahma Sunday School Deployment Script
# ===============================
# ----- Domain and App Info -----
DM_NAME="home.alrahmaisgl.org"
domain_app="alrahma"
# ----- Database credentials -----
DB_HOST="localhost"
DB_NAME="u280815660_school"
DB_USER="u280815660_melabidi"
DB_PASS=">tNxlRzP/W8"
# ----- Directories -----
ZIP_FILE="$1"
BASE_DIR="/home/u280815660/domains"
DEPLOY_DIR="$BASE_DIR/$domain_app"
APP_DIR="$BASE_DIR/$DM_NAME/$domain_app"
PUBLIC_DIR="$BASE_DIR/$DM_NAME/public_html"
BACKUP_DIR="$BASE_DIR/archive"
SECRETS_DIR="$BASE_DIR/deploy_secrets"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
# ----- Runtime binaries -----
# Hostinger/CloudLinux exposes PHP 8.5 here. The default CLI `php` may still be
# PHP 8.2, which cannot install this project's PHP 8.5 lock file.
PHP_BIN="${PHP_BIN:-/opt/alt/php85/usr/bin/php}"
COMPOSER_BIN="${COMPOSER_BIN:-$(command -v composer2 || command -v composer || true)}"
# ===============================
# 1. VALIDATE INPUT
# ===============================
if [ -z "$ZIP_FILE" ] || [ ! -f "$ZIP_FILE" ]; then
echo "? Please provide the ZIP file: ./deploy_home.sh alrahma_deploy.zip"
exit 1
fi
echo "?? Starting deployment of $ZIP_FILE..."
if [ ! -x "$PHP_BIN" ]; then
echo "? PHP 8.5 binary was not found or is not executable: $PHP_BIN"
echo " Set PHP_BIN=/path/to/php85 and rerun the deployment."
exit 1
fi
if [ -z "$COMPOSER_BIN" ] || [ ! -e "$COMPOSER_BIN" ]; then
echo "? composer2/composer was not found in PATH."
echo " Set COMPOSER_BIN=/path/to/composer2 and rerun the deployment."
exit 1
fi
echo "?? Using PHP: $("$PHP_BIN" -v | head -n 1)"
echo "?? Using Composer: $COMPOSER_BIN"
# ===============================
# 2. BACKUP EXISTING SITE
# ===============================
echo "??? Backing up current site..."
mkdir -p "$BACKUP_DIR"
cp -r "$PUBLIC_DIR" "$BACKUP_DIR/public_html_$TIMESTAMP"
cp -r "$APP_DIR" "$BACKUP_DIR/$domain_app_$TIMESTAMP"
# ===============================
# 3. BACKUP PERSISTENT FILES
# ===============================
echo "?? Backing up persistent files..."
mkdir -p "$SECRETS_DIR"
PERSIST_BACKUP="$BASE_DIR/persist_backup_$TIMESTAMP"
mkdir -p "$PERSIST_BACKUP"
# writable/
if [ -d "$APP_DIR/writable" ]; then
echo " ? Backing up writable/"
cp -r "$APP_DIR/writable" "$PERSIST_BACKUP/writable"
fi
# .env
if [ -f "$APP_DIR/.env" ]; then
echo " ? Backing up .env"
cp "$APP_DIR/.env" "$PERSIST_BACKUP/.env"
fi
# public/.htaccess backup is kept for rollback only. The new release file is
# deployed below so stale CSP/rewrite rules do not survive deployments.
if [ -f "$PUBLIC_DIR/.htaccess" ]; then
echo " ? Backing up public .htaccess"
cp "$PUBLIC_DIR/.htaccess" "$PERSIST_BACKUP/.htaccess"
fi
# public/index.php backup is kept for rollback only. The new release file is
# deployed below and then patched to point at the sibling app directory.
if [ -f "$PUBLIC_DIR/index.php" ]; then
echo " ? Backing up public index.php"
cp "$PUBLIC_DIR/index.php" "$PERSIST_BACKUP/index.php"
fi
# ===============================
# 4. CLEAN & EXTRACT NEW DEPLOY
# ===============================
echo "?? Cleaning old deployment..."
rm -rf "$DEPLOY_DIR"
unzip "$ZIP_FILE" -d "$BASE_DIR"
# ===============================
# 5. DEPLOY APP FILES
# ===============================
echo "?? Deploying new app files..."
rm -rf "$APP_DIR"
mkdir -p "$APP_DIR"
cp -r "$DEPLOY_DIR"/. "$APP_DIR"
# ===============================
# 6. RESTORE PERSISTENT FILES
# ===============================
echo "?? Restoring persistent files..."
# writable/
if [ -d "$PERSIST_BACKUP/writable" ]; then
echo " ? Restoring writable/"
rm -rf "$APP_DIR/writable"
cp -r "$PERSIST_BACKUP/writable" "$APP_DIR/writable"
fi
# .env
if [ -f "$PERSIST_BACKUP/.env" ]; then
echo " ? Restoring .env"
cp "$PERSIST_BACKUP/.env" "$APP_DIR/.env"
fi
# ===============================
# 7. DEPLOY PUBLIC FILES
# ===============================
echo "?? Deploying public files..."
mkdir -p "$PUBLIC_DIR"
find "$PUBLIC_DIR" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
cp -a "$APP_DIR/public/." "$PUBLIC_DIR/"
# ===============================
# 8. FIX index.php PATH
# ===============================
echo "?? Fixing index.php path..."
sed -i "s|require FCPATH . '../app/Config/Paths.php';|require FCPATH . '../alrahma/app/Config/Paths.php';|" "$PUBLIC_DIR/index.php"
# ===============================
# 9. UPDATE BASE URL & DB CONFIG
# ===============================
APP_CONFIG="$APP_DIR/app/Config/App.php"
DB_CONFIG="$APP_DIR/app/Config/Database.php"
echo "?? Updating configuration files..."
# Base URL
sed -i "s|public string \$baseURL = .*|public string \$baseURL = 'https://$DM_NAME/';|" "$APP_CONFIG"
# Database.php
sed -i "s|'hostname' => .*|'hostname' => '$DB_HOST',|" "$DB_CONFIG"
sed -i "s|'username' => .*|'username' => '$DB_USER',|" "$DB_CONFIG"
sed -i "s|'password' => .*|'password' => '$DB_PASS',|" "$DB_CONFIG"
sed -i "s|'database' => .*|'database' => '$DB_NAME',|" "$DB_CONFIG"
# ===============================
# 10. UPDATE db_connection.php
# ===============================
DB_CONN_FILE="$APP_DIR/app/db_connection.php"
if [ -f "$DB_CONN_FILE" ]; then
echo "??? Updating db_connection.php..."
sed -i "s|\$host = '.*';|\$host = '$DB_HOST';|" "$DB_CONN_FILE"
sed -i "s|\$dbname = '.*';|\$dbname = '$DB_NAME';|" "$DB_CONN_FILE"
sed -i "s|\$username = '.*';|\$username = '$DB_USER';|" "$DB_CONN_FILE"
sed -i "s|\$password = '.*';|\$password = '$DB_PASS';|" "$DB_CONN_FILE"
fi
# ===============================
# 11. FIX FPDF PATHS
# ===============================
echo "?? Fixing FPDF paths..."
grep -rl "ThirdParty\\\\fpdf\\\\fpdf.php" "$APP_DIR/app" | while read -r file; do
sed -i "s|ThirdParty\\\\fpdf\\\\fpdf.php|ThirdParty/fpdf/fpdf.php|g" "$file"
echo "? Fixed path in: $file"
done
# ===============================
# 12. COMPOSER INSTALL
# ===============================
echo "?? Installing production Composer dependencies with PHP 8.5..."
cd "$APP_DIR" || exit
export PATH="$(dirname "$PHP_BIN"):$PATH"
export COMPOSER_ALLOW_SUPERUSER=1
if "$PHP_BIN" "$COMPOSER_BIN" --version >/dev/null 2>&1; then
"$PHP_BIN" "$COMPOSER_BIN" install \
--no-dev \
--prefer-dist \
--optimize-autoloader \
--classmap-authoritative \
--no-interaction \
--no-progress
else
"$COMPOSER_BIN" install \
--no-dev \
--prefer-dist \
--optimize-autoloader \
--classmap-authoritative \
--no-interaction \
--no-progress
fi
"$PHP_BIN" spark --version
# ===============================
# 13. RESTORE PERMISSIONS
# ===============================
echo "?? Setting permissions..."
chmod 644 "$PUBLIC_DIR/.htaccess"
chmod 644 "$PUBLIC_DIR/index.php"
chmod -R 755 "$APP_DIR"
chmod 644 "$APP_DIR/app/db_connection.php"
chmod -R 777 "$APP_DIR/writable"
# ===============================
# ? DONE
# ===============================
echo "? Deployment completed successfully at $TIMESTAMP"
echo "?? Backup stored in: $BACKUP_DIR"
echo "?? Persistent files restored from: $PERSIST_BACKUP"
echo "?? Writable and .env restored; public .htaccess and index.php deployed from the new release."