Commit c1cce64e authored by Administrator's avatar Administrator

Update 3 files via Son of Anton

parent 90e262a9
...@@ -3,20 +3,25 @@ ...@@ -3,20 +3,25 @@
DocumentRoot /var/www/html/public DocumentRoot /var/www/html/public
<Directory /var/www/html/public> <Directory /var/www/html/public>
Options -Indexes +FollowSymLinks
AllowOverride All AllowOverride All
Require all granted Require all granted
Options -Indexes +FollowSymLinks
# Fallback to index.php for all non-file requests # Ensure mod_rewrite works
FallbackResource /index.php <IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
</Directory> </Directory>
# Block access to sensitive directories # Block access to non-public directories
<DirectoryMatch "^/var/www/html/(app|config|database|storage|cron|docker)"> <DirectoryMatch "^/var/www/html/(app|config|database|storage|cron|docker)">
Require all denied Require all denied
</DirectoryMatch> </DirectoryMatch>
# Block .env and .git access # Block dotfiles
<FilesMatch "^\."> <FilesMatch "^\.">
Require all denied Require all denied
</FilesMatch> </FilesMatch>
......
...@@ -35,31 +35,66 @@ chown www-data:www-data /var/www/html/.env ...@@ -35,31 +35,66 @@ chown www-data:www-data /var/www/html/.env
chmod 640 /var/www/html/.env chmod 640 /var/www/html/.env
echo "✅ .env written" echo "✅ .env written"
# ── Wait for MySQL ── # ── Fix permissions ──
chown -R www-data:www-data /var/www/html/storage 2>/dev/null || true
chmod -R 775 /var/www/html/storage 2>/dev/null || true
# ══════════════════════════════════════
# START APACHE IMMEDIATELY IN BACKGROUND
# so CapRover health check passes while
# we run migrations/seeds
# ══════════════════════════════════════
echo "Starting Apache in background (health check will pass)..."
apache2-foreground &
APACHE_PID=$!
# Give Apache a moment to bind port 80
sleep 2
# Verify Apache is running
if ! kill -0 $APACHE_PID 2>/dev/null; then
echo "❌ Apache failed to start!"
exit 1
fi
echo "✅ Apache is running (PID: $APACHE_PID)"
# ══════════════════════════════════════
# WAIT FOR MYSQL (non-blocking — Apache is already up)
# ══════════════════════════════════════
echo "Waiting for MySQL..." echo "Waiting for MySQL..."
TRIES=0 TRIES=0
MAX_TRIES=60 MAX_TRIES=60
MYSQL_READY=false
until mysqladmin ping -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" --silent 2>/dev/null; do until mysqladmin ping -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" --silent 2>/dev/null; do
TRIES=$((TRIES + 1)) TRIES=$((TRIES + 1))
if [ $TRIES -ge $MAX_TRIES ]; then if [ $TRIES -ge $MAX_TRIES ]; then
echo "❌ MySQL not reachable after 2 minutes!" echo "⚠️ MySQL not reachable after 2 minutes — skipping migrations"
echo "Starting Apache anyway — app will show DB error" break
exec apache2-foreground
fi fi
sleep 2 sleep 2
done done
echo "✅ MySQL is alive"
# ── Create database ── if [ $TRIES -lt $MAX_TRIES ]; then
echo "Creating database if needed..." MYSQL_READY=true
mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" -e " echo "✅ MySQL is alive"
fi
# ══════════════════════════════════════
# DATABASE SETUP (only if MySQL is reachable)
# ══════════════════════════════════════
if [ "$MYSQL_READY" = true ]; then
# ── Create database ──
echo "Creating database if needed..."
mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" -e "
CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\`
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
" 2>&1 || echo "⚠️ Could not create database (may already exist)" " 2>&1 || echo "⚠️ Could not create database (may already exist)"
# ── Create tracking tables (schema.sql has them but migrations don't) ── # ── Create infrastructure tables ──
echo "Creating infrastructure tables..." echo "Creating infrastructure tables..."
mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -e " mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -e "
CREATE TABLE IF NOT EXISTS seeds ( CREATE TABLE IF NOT EXISTS seeds (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
seed VARCHAR(255) NOT NULL, seed VARCHAR(255) NOT NULL,
...@@ -94,64 +129,66 @@ mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME} ...@@ -94,64 +129,66 @@ mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}
INDEX idx_cron_job_name (job_name), INDEX idx_cron_job_name (job_name),
INDEX idx_cron_job_started (started_at) INDEX idx_cron_job_started (started_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
" 2>&1 || echo "⚠️ Infrastructure tables may already exist" " 2>&1 || echo "⚠️ Infrastructure tables may already exist"
echo "✅ Infrastructure tables ready" echo "✅ Infrastructure tables ready"
# ══════════════════════════════════════ # ══════════════════════════════════════
# RUN MIGRATIONS # RUN MIGRATIONS
# ══════════════════════════════════════ # ══════════════════════════════════════
echo "" echo ""
echo "==========================================" echo "=========================================="
echo " RUNNING MIGRATIONS" echo " RUNNING MIGRATIONS"
echo "==========================================" echo "=========================================="
cd /var/www/html cd /var/www/html
# Count migration files MIGRATION_COUNT=$(ls -1 database/migrations/Phase_*.php 2>/dev/null | wc -l)
MIGRATION_COUNT=$(ls -1 database/migrations/Phase_*.php 2>/dev/null | wc -l) echo "Found ${MIGRATION_COUNT} migration files"
echo "Found ${MIGRATION_COUNT} migration files"
php cli.php migrate 2>&1 || { php cli.php migrate 2>&1 || {
echo "⚠️ Migration command returned error" echo "⚠️ Migration command returned error"
echo "Checking table count..."
TC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${DB_NAME}';" 2>/dev/null || echo "0") TC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${DB_NAME}';" 2>/dev/null || echo "0")
echo "Tables: ${TC}" echo "Tables in DB: ${TC}"
} }
# ══════════════════════════════════════ # ══════════════════════════════════════
# RUN SEEDS # RUN SEEDS
# ══════════════════════════════════════ # ══════════════════════════════════════
echo "" echo ""
echo "==========================================" echo "=========================================="
echo " RUNNING SEEDS" echo " RUNNING SEEDS"
echo "==========================================" echo "=========================================="
SEED_COUNT=$(ls -1 database/seeds/Phase_*.php 2>/dev/null | wc -l) SEED_COUNT=$(ls -1 database/seeds/Phase_*.php 2>/dev/null | wc -l)
echo "Found ${SEED_COUNT} seed files" echo "Found ${SEED_COUNT} seed files"
php cli.php seed 2>&1 || { php cli.php seed 2>&1 || {
echo "⚠️ Seed command returned error (some may have already run)" echo "⚠️ Seed command returned error (some may have already run)"
} }
# ══════════════════════════════════════ # ══════════════════════════════════════
# VERIFY # VERIFY
# ══════════════════════════════════════ # ══════════════════════════════════════
echo "" echo ""
echo "==========================================" echo "=========================================="
echo " DEPLOYMENT VERIFICATION" echo " DEPLOYMENT VERIFICATION"
echo "==========================================" echo "=========================================="
TC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${DB_NAME}';" 2>/dev/null || echo "?") TC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${DB_NAME}';" 2>/dev/null || echo "?")
echo "Tables: ${TC}" echo "Tables: ${TC}"
EC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM employees;" 2>/dev/null || echo "0") EC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM employees;" 2>/dev/null || echo "0")
echo "Employees: ${EC}" echo "Employees: ${EC}"
RC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM roles;" 2>/dev/null || echo "0") RC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM roles;" 2>/dev/null || echo "0")
echo "Roles: ${RC}" echo "Roles: ${RC}"
BC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM branches;" 2>/dev/null || echo "0") BC=$(mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -N -e "SELECT COUNT(*) FROM branches;" 2>/dev/null || echo "0")
echo "Branches: ${BC}" echo "Branches: ${BC}"
else
echo "⚠️ Skipped migrations/seeds — MySQL unavailable"
fi
echo "" echo ""
echo "==========================================" echo "=========================================="
...@@ -160,10 +197,8 @@ echo " ⚠️ Password change forced on first login" ...@@ -160,10 +197,8 @@ echo " ⚠️ Password change forced on first login"
echo "==========================================" echo "=========================================="
echo "" echo ""
# ── Fix permissions one last time ── # ══════════════════════════════════════
chown -R www-data:www-data /var/www/html/storage 2>/dev/null || true # WAIT FOR APACHE (foreground — keeps container alive)
chmod -R 775 /var/www/html/storage 2>/dev/null || true # ══════════════════════════════════════
echo "Apache is serving on port 80. Waiting for process..."
# ── Start Apache ── wait $APACHE_PID
echo "Starting Apache..." \ No newline at end of file
exec apache2-foreground
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment