Commit c1cce64e authored by Administrator's avatar Administrator

Update 3 files via Son of Anton

parent 90e262a9
{
"schemaVersion": 2,
"dockerfilePath": "./Dockerfile"
"schemaVersion": 2,
"dockerfilePath": "./Dockerfile"
}
\ No newline at end of file
......@@ -3,20 +3,25 @@
DocumentRoot /var/www/html/public
<Directory /var/www/html/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
Options -Indexes +FollowSymLinks
# Fallback to index.php for all non-file requests
FallbackResource /index.php
# Ensure mod_rewrite works
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
</Directory>
# Block access to sensitive directories
# Block access to non-public directories
<DirectoryMatch "^/var/www/html/(app|config|database|storage|cron|docker)">
Require all denied
</DirectoryMatch>
# Block .env and .git access
# Block dotfiles
<FilesMatch "^\.">
Require all denied
</FilesMatch>
......
......@@ -35,123 +35,160 @@ chown www-data:www-data /var/www/html/.env
chmod 640 /var/www/html/.env
echo "✅ .env written"
# ── Wait for MySQL ──
echo "Waiting for MySQL..."
TRIES=0
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"
# ── 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
# ══════════════════════════════════════
# RUN MIGRATIONS
# START APACHE IMMEDIATELY IN BACKGROUND
# so CapRover health check passes while
# we run migrations/seeds
# ══════════════════════════════════════
echo ""
echo "=========================================="
echo " RUNNING MIGRATIONS"
echo "=========================================="
echo "Starting Apache in background (health check will pass)..."
apache2-foreground &
APACHE_PID=$!
cd /var/www/html
# Give Apache a moment to bind port 80
sleep 2
# Count migration files
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"
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}"
}
# 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)"
# ══════════════════════════════════════
# RUN SEEDS
# WAIT FOR MYSQL (non-blocking — Apache is already up)
# ══════════════════════════════════════
echo ""
echo "=========================================="
echo " RUNNING SEEDS"
echo "=========================================="
echo "Waiting for MySQL..."
TRIES=0
MAX_TRIES=60
MYSQL_READY=false
SEED_COUNT=$(ls -1 database/seeds/Phase_*.php 2>/dev/null | wc -l)
echo "Found ${SEED_COUNT} seed files"
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 — skipping migrations"
break
fi
sleep 2
done
php cli.php seed 2>&1 || {
echo "⚠️ Seed command returned error (some may have already run)"
}
if [ $TRIES -lt $MAX_TRIES ]; then
MYSQL_READY=true
echo "✅ MySQL is alive"
fi
# ══════════════════════════════════════
# VERIFY
# DATABASE SETUP (only if MySQL is reachable)
# ══════════════════════════════════════
echo ""
echo "=========================================="
echo " DEPLOYMENT VERIFICATION"
echo "=========================================="
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}\`
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 "?")
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")
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")
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")
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")
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")
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")
echo "Branches: ${BC}"
else
echo "⚠️ Skipped migrations/seeds — MySQL unavailable"
fi
echo ""
echo "=========================================="
......@@ -160,10 +197,8 @@ echo " ⚠️ Password change forced on first login"
echo "=========================================="
echo ""
# ── Fix permissions one last time ──
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 ──
echo "Starting Apache..."
exec apache2-foreground
\ No newline at end of file
# ══════════════════════════════════════
# WAIT FOR APACHE (foreground — keeps container alive)
# ══════════════════════════════════════
echo "Apache is serving on port 80. Waiting for process..."
wait $APACHE_PID
\ 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