Commit c1cce64e authored by Administrator's avatar Administrator

Update 3 files via Son of Anton

parent 90e262a9
{ {
"schemaVersion": 2, "schemaVersion": 2,
"dockerfilePath": "./Dockerfile" "dockerfilePath": "./Dockerfile"
} }
\ No newline at end of file
...@@ -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,123 +35,160 @@ chown www-data:www-data /var/www/html/.env ...@@ -35,123 +35,160 @@ 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 ──
echo "Waiting for MySQL..." chown -R www-data:www-data /var/www/html/storage 2>/dev/null || true
TRIES=0 chmod -R 775 /var/www/html/storage 2>/dev/null || true
MAX_TRIES=60
until mysqladmin ping -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" --silent 2>/dev/null; do
TRIES=$((TRIES + 1))
if [ $TRIES -ge $MAX_TRIES ]; then
echo "❌ MySQL not reachable after 2 minutes!"
echo "Starting Apache anyway — app will show DB error"
exec apache2-foreground
fi
sleep 2
done
echo "✅ MySQL is alive"
# ── 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}\`
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
" 2>&1 || echo "⚠️ Could not create database (may already exist)"
# ── Create tracking tables (schema.sql has them but migrations don't) ──
echo "Creating infrastructure tables..."
mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -e "
CREATE TABLE IF NOT EXISTS seeds (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
seed VARCHAR(255) NOT NULL,
executed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uq_seeds_name (seed)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS async_event_queue (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
event_name VARCHAR(200) NOT NULL,
event_data_json JSON NULL,
status VARCHAR(30) NOT NULL DEFAULT 'pending',
attempts INT UNSIGNED NOT NULL DEFAULT 0,
max_attempts INT UNSIGNED NOT NULL DEFAULT 3,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
processed_at TIMESTAMP NULL DEFAULT NULL,
failed_at TIMESTAMP NULL DEFAULT NULL,
error_message TEXT NULL,
INDEX idx_async_events_status (status),
INDEX idx_async_events_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cron_job_log (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
job_name VARCHAR(200) NOT NULL,
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
finished_at TIMESTAMP NULL DEFAULT NULL,
status VARCHAR(30) NOT NULL DEFAULT 'running',
records_processed INT UNSIGNED NOT NULL DEFAULT 0,
error_message TEXT NULL,
execution_time_ms INT UNSIGNED NULL,
INDEX idx_cron_job_name (job_name),
INDEX idx_cron_job_started (started_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
" 2>&1 || echo "⚠️ Infrastructure tables may already exist"
echo "✅ Infrastructure tables ready"
# ══════════════════════════════════════ # ══════════════════════════════════════
# RUN MIGRATIONS # START APACHE IMMEDIATELY IN BACKGROUND
# so CapRover health check passes while
# we run migrations/seeds
# ══════════════════════════════════════ # ══════════════════════════════════════
echo "" echo "Starting Apache in background (health check will pass)..."
echo "==========================================" apache2-foreground &
echo " RUNNING MIGRATIONS" APACHE_PID=$!
echo "=========================================="
cd /var/www/html # Give Apache a moment to bind port 80
sleep 2
# Count migration files # Verify Apache is running
MIGRATION_COUNT=$(ls -1 database/migrations/Phase_*.php 2>/dev/null | wc -l) if ! kill -0 $APACHE_PID 2>/dev/null; then
echo "Found ${MIGRATION_COUNT} migration files" echo "❌ Apache failed to start!"
exit 1
php cli.php migrate 2>&1 || { fi
echo "⚠️ Migration command returned error" echo "✅ Apache is running (PID: $APACHE_PID)"
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")
echo "Tables: ${TC}"
}
# ══════════════════════════════════════ # ══════════════════════════════════════
# RUN SEEDS # WAIT FOR MYSQL (non-blocking — Apache is already up)
# ══════════════════════════════════════ # ══════════════════════════════════════
echo "" echo "Waiting for MySQL..."
echo "==========================================" TRIES=0
echo " RUNNING SEEDS" MAX_TRIES=60
echo "==========================================" MYSQL_READY=false
SEED_COUNT=$(ls -1 database/seeds/Phase_*.php 2>/dev/null | wc -l) until mysqladmin ping -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" --silent 2>/dev/null; do
echo "Found ${SEED_COUNT} seed files" TRIES=$((TRIES + 1))
if [ $TRIES -ge $MAX_TRIES ]; then
echo "⚠️ MySQL not reachable after 2 minutes — skipping migrations"
break
fi
sleep 2
done
php cli.php seed 2>&1 || { if [ $TRIES -lt $MAX_TRIES ]; then
echo "⚠️ Seed command returned error (some may have already run)" MYSQL_READY=true
} echo "✅ MySQL is alive"
fi
# ══════════════════════════════════════ # ══════════════════════════════════════
# VERIFY # DATABASE SETUP (only if MySQL is reachable)
# ══════════════════════════════════════ # ══════════════════════════════════════
echo "" if [ "$MYSQL_READY" = true ]; then
echo "=========================================="
echo " DEPLOYMENT VERIFICATION" # ── Create database ──
echo "==========================================" 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}\`
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
" 2>&1 || echo "⚠️ Could not create database (may already exist)"
# ── Create infrastructure tables ──
echo "Creating infrastructure tables..."
mysql -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" -e "
CREATE TABLE IF NOT EXISTS seeds (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
seed VARCHAR(255) NOT NULL,
executed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uq_seeds_name (seed)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS async_event_queue (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
event_name VARCHAR(200) NOT NULL,
event_data_json JSON NULL,
status VARCHAR(30) NOT NULL DEFAULT 'pending',
attempts INT UNSIGNED NOT NULL DEFAULT 0,
max_attempts INT UNSIGNED NOT NULL DEFAULT 3,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
processed_at TIMESTAMP NULL DEFAULT NULL,
failed_at TIMESTAMP NULL DEFAULT NULL,
error_message TEXT NULL,
INDEX idx_async_events_status (status),
INDEX idx_async_events_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS cron_job_log (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
job_name VARCHAR(200) NOT NULL,
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
finished_at TIMESTAMP NULL DEFAULT NULL,
status VARCHAR(30) NOT NULL DEFAULT 'running',
records_processed INT UNSIGNED NOT NULL DEFAULT 0,
error_message TEXT NULL,
execution_time_ms INT UNSIGNED NULL,
INDEX idx_cron_job_name (job_name),
INDEX idx_cron_job_started (started_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
" 2>&1 || echo "⚠️ Infrastructure tables may already exist"
echo "✅ Infrastructure tables ready"
# ══════════════════════════════════════
# RUN MIGRATIONS
# ══════════════════════════════════════
echo ""
echo "=========================================="
echo " RUNNING MIGRATIONS"
echo "=========================================="
cd /var/www/html
MIGRATION_COUNT=$(ls -1 database/migrations/Phase_*.php 2>/dev/null | wc -l)
echo "Found ${MIGRATION_COUNT} migration files"
php cli.php migrate 2>&1 || {
echo "⚠️ Migration command returned error"
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 in DB: ${TC}"
}
# ══════════════════════════════════════
# RUN SEEDS
# ══════════════════════════════════════
echo ""
echo "=========================================="
echo " RUNNING SEEDS"
echo "=========================================="
SEED_COUNT=$(ls -1 database/seeds/Phase_*.php 2>/dev/null | wc -l)
echo "Found ${SEED_COUNT} seed files"
php cli.php seed 2>&1 || {
echo "⚠️ Seed command returned error (some may have already run)"
}
# ══════════════════════════════════════
# VERIFY
# ══════════════════════════════════════
echo ""
echo "=========================================="
echo " DEPLOYMENT VERIFICATION"
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}"
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 "?") 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 "Tables: ${TC}" echo "Employees: ${EC}"
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") 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 "Employees: ${EC}" echo "Roles: ${RC}"
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") 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 "Roles: ${RC}" echo "Branches: ${BC}"
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") else
echo "Branches: ${BC}" 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