267 lines
9.5 KiB
Bash
Executable File
267 lines
9.5 KiB
Bash
Executable File
#!/bin/sh
|
|
set -e
|
|
|
|
echo "========================================"
|
|
echo "Starting container setup..."
|
|
echo "========================================"
|
|
|
|
# Show current user and directory
|
|
echo "Current user: $(whoami)"
|
|
echo "Current directory: $(pwd)"
|
|
|
|
# Set debug based on environment
|
|
if [ "$APP_ENV" = "production" ]; then
|
|
export APP_DEBUG=false
|
|
export LOG_LEVEL=error
|
|
else
|
|
export APP_DEBUG=true
|
|
export LOG_LEVEL=debug
|
|
fi
|
|
|
|
# Create ALL required Laravel directories
|
|
echo "Creating storage directories..."
|
|
mkdir -p storage/framework/cache
|
|
mkdir -p storage/framework/sessions
|
|
mkdir -p storage/framework/views
|
|
mkdir -p storage/framework/testing
|
|
mkdir -p storage/logs
|
|
mkdir -p bootstrap/cache
|
|
|
|
# Also create the view cache directory (sometimes needed)
|
|
mkdir -p storage/framework/views
|
|
|
|
# Set permissions
|
|
echo "Setting permissions..."
|
|
chmod -R 777 storage
|
|
chmod -R 777 bootstrap/cache
|
|
|
|
# Create cache file if it doesn't exist
|
|
touch storage/framework/cache/.gitignore
|
|
touch storage/framework/sessions/.gitignore
|
|
touch storage/framework/views/.gitignore
|
|
touch storage/logs/.gitignore
|
|
|
|
# Verify directories exist
|
|
echo "Verifying directories..."
|
|
ls -la storage/framework/
|
|
ls -la bootstrap/
|
|
|
|
# Check if .env exists, create if not
|
|
if [ ! -f .env ]; then
|
|
echo "Creating .env file..."
|
|
if [ -f .env.example ]; then
|
|
cp .env.example .env
|
|
else
|
|
cat > .env << 'EOF'
|
|
APP_NAME=Laravel
|
|
APP_ENV=local
|
|
APP_KEY=
|
|
APP_DEBUG=true
|
|
APP_URL=http://localhost
|
|
LOG_CHANNEL=stack
|
|
EOF
|
|
fi
|
|
fi
|
|
|
|
# LOAD environment variables from .env for use in this script
|
|
if [ -f .env ]; then
|
|
echo "Loading environment variables from .env..."
|
|
# Read .env file line by line and export each variable
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
# Remove carriage return if present (Windows style)
|
|
line=$(echo "$line" | sed 's/\r$//')
|
|
# Skip comments and empty lines
|
|
case "$line" in
|
|
"#"*) continue ;;
|
|
"") continue ;;
|
|
esac
|
|
# Remove quotes from value and export
|
|
export "$(echo "$line" | sed 's/["'\'']//g')" 2>/dev/null || true
|
|
done < .env
|
|
fi
|
|
|
|
|
|
# Generate key if not present (only if APP_KEY is empty or missing)
|
|
echo "Checking .env for APP_KEY..."
|
|
if ! grep -E "APP_KEY=.{10,}" .env; then
|
|
echo "APP_KEY is empty or missing. Generating..."
|
|
# If the file doesn't have APP_KEY= at all, add it so artisan can replace it
|
|
if ! grep -q "APP_KEY=" .env; then
|
|
echo "Appending APP_KEY= placeholder..."
|
|
echo "" >> .env
|
|
echo "APP_KEY=" >> .env
|
|
fi
|
|
php artisan key:generate --no-interaction --force || echo "❌ Key generation failed!"
|
|
fi
|
|
|
|
echo "Verifying APP_KEY after generation:"
|
|
grep "APP_KEY" .env
|
|
|
|
# Ensure vendor directory matches composer.lock exactly, syncing missing packages
|
|
echo "Verifying and synchronizing Composer dependencies..."
|
|
|
|
# Check if scribe is in composer.json but not installed
|
|
if grep -q "knuckleswtf/scribe" composer.json; then
|
|
if ! composer show --locked knuckleswtf/scribe --no-ansi 2>/dev/null | grep -q "name"; then
|
|
echo "📦 knuckleswtf/scribe found in composer.json but not in lock file. Installing..."
|
|
composer require --dev knuckleswtf/scribe --no-interaction --no-progress --update-with-dependencies
|
|
else
|
|
echo "✅ knuckleswtf/scribe is already in lock file"
|
|
composer install --no-interaction --optimize-autoloader
|
|
fi
|
|
else
|
|
# Normal install
|
|
composer install --no-interaction --optimize-autoloader
|
|
fi
|
|
|
|
# If install fails, try update
|
|
if [ $? -ne 0 ]; then
|
|
echo "⚠️ Composer install failed, attempting update..."
|
|
composer update --no-interaction --optimize-autoloader --with-all-dependencies
|
|
fi
|
|
|
|
# Install and publish Scribe assets
|
|
echo "Setting up Scribe/OpenAPI documentation..."
|
|
|
|
# Publish Scribe config (if not already published)
|
|
if [ ! -f config/scribe.php ]; then
|
|
echo "Publishing Scribe configuration..."
|
|
php artisan vendor:publish --tag=scribe-config --force
|
|
fi
|
|
|
|
# Skip Scribe generation if docs already exist to save time on restart
|
|
if [ -d resources/views/scribe ] && [ "$FORCE_REGENERATE_DOCS" != "true" ]; then
|
|
echo "✅ Scribe docs already exist, skipping generation..."
|
|
SKIP_SCRIBE=true
|
|
fi
|
|
|
|
|
|
# Generate OpenAPI/Swagger documentation
|
|
if [ "$APP_ENV" != "production" ] || [ "$AUTO_GENERATE_API_DOCS" = "true" ]; then
|
|
echo "Generating API documentation..."
|
|
|
|
# Generate Scribe documentation
|
|
if [ "$SKIP_SCRIBE" != "true" ]; then
|
|
echo "Generating Scribe documentation..."
|
|
php artisan scribe:generate --no-interaction || echo "⚠️ Scribe generation failed, check your API routes"
|
|
fi
|
|
|
|
# For OpenAPI/Swagger UI specifically
|
|
if [ -f public/docs/openapi.yaml ] || [ -f public/docs/collection.json ]; then
|
|
echo "✅ Documentation generated successfully"
|
|
echo "📚 Swagger UI available at: /docs (if configured)"
|
|
echo "📄 OpenAPI spec available at: /docs/openapi.yaml"
|
|
else
|
|
echo "⚠️ Documentation files not found after generation"
|
|
fi
|
|
fi
|
|
|
|
# Create symbolic link for Scribe assets if needed
|
|
if [ -d vendor/knuckleswtf/scribe/dist ]; then
|
|
echo "Creating Scribe assets symlink..."
|
|
mkdir -p public/vendor/scribe
|
|
cp -r vendor/knuckleswtf/scribe/dist/* public/vendor/scribe/ 2>/dev/null || \
|
|
ln -sf ../vendor/knuckleswtf/scribe/dist public/vendor/scribe 2>/dev/null || true
|
|
fi
|
|
|
|
# FIX: Ensure storage and bootstrap are writable (these are small)
|
|
echo "Setting correct ownership for storage and bootstrap..."
|
|
chown -R www-data:www-data storage bootstrap/cache
|
|
chmod -R 775 storage bootstrap/cache
|
|
|
|
# Note: We skip recursive chown on vendor because it's very slow on mounted volumes
|
|
# and usually not necessary for runtime.
|
|
|
|
|
|
# Verify vendor integrity at PHP level
|
|
echo "Checking vendor visibility and READABILITY for PHP..."
|
|
ls -la /var/www/html/vendor/laravel/prompts/src/helpers.php
|
|
php -r 'if (file_exists("/var/www/html/vendor/laravel/prompts/src/helpers.php")) { echo "✅ file_exists true\n"; }'
|
|
php -r 'require "/var/www/html/vendor/laravel/prompts/src/helpers.php"; echo "✅ PHP successfully REQUIRED helpers.php\n";'
|
|
|
|
# Run production optimizations for production environment
|
|
if [ "$APP_ENV" = "production" ] || [ "$MODE" = "production" ]; then
|
|
echo "Running production optimizations..."
|
|
php artisan config:cache
|
|
php artisan view:cache
|
|
php artisan event:cache
|
|
else
|
|
# Clear all caches to ensure fresh state for non-production
|
|
echo "Clearing caches..."
|
|
php artisan config:clear || true
|
|
php artisan view:clear || true
|
|
php artisan route:clear || true
|
|
php artisan cache:clear || true
|
|
php artisan optimize:clear || true
|
|
fi
|
|
|
|
# Verify Vite manifest exists
|
|
if [ -f public/build/manifest.json ]; then
|
|
echo "✅ Vite manifest found at public/build/manifest.json"
|
|
else
|
|
echo "⚠️ Vite manifest not found! Attempting to build assets..."
|
|
if command -v npm >/dev/null 2>&1; then
|
|
if [ ! -d node_modules ]; then
|
|
echo "📦 node_modules missing. Installing..."
|
|
npm install --legacy-peer-deps --no-audit || echo "❌ npm install failed!"
|
|
fi
|
|
npm run build || echo "❌ npm run build failed!"
|
|
else
|
|
echo "❌ npm not found, cannot build assets automatically."
|
|
fi
|
|
|
|
if [ ! -f public/build/manifest.json ]; then
|
|
echo "⚠️ Still no Vite manifest! Assets may not load correctly."
|
|
echo "Current contents of public/build:"
|
|
ls -la public/build/ 2>/dev/null || echo "public/build directory does not exist"
|
|
fi
|
|
fi
|
|
|
|
# Optional: Run migrations if needed (with safety for production)
|
|
if [ "$AUTO_MIGRATE" = "true" ] || [ "$APP_ENV" != "production" ]; then
|
|
echo "Running migrations..."
|
|
php artisan migrate --force
|
|
|
|
# Optional: Seed if needed, but we have converted.sql
|
|
# php artisan db:seed --force
|
|
|
|
# Check if database has data (e.g., check users table)
|
|
# We use PGPASSWORD to avoid interactive prompt
|
|
export PGPASSWORD=$DB_PASSWORD
|
|
echo "Checking if database needs data import..."
|
|
USER_COUNT=$(psql -h $DB_HOST -U $DB_USERNAME -d $DB_DATABASE -t -c "SELECT count(*) FROM users;" 2>/dev/null | tr -d '[:space:]' || echo "0")
|
|
|
|
if [ "$USER_COUNT" = "0" ] || [ -z "$USER_COUNT" ]; then
|
|
if [ -f converted.sql ]; then
|
|
echo "📦 Database is empty. Importing data from converted.sql..."
|
|
psql -h $DB_HOST -U $DB_USERNAME -d $DB_DATABASE -f converted.sql > /dev/null
|
|
echo "✅ Data import complete."
|
|
else
|
|
echo "⚠️ converted.sql not found, skipping data import."
|
|
fi
|
|
else
|
|
echo "✅ Database already has data ($USER_COUNT users)."
|
|
fi
|
|
unset PGPASSWORD
|
|
fi
|
|
|
|
echo "========================================"
|
|
echo "Setup complete. Starting application..."
|
|
echo "========================================"
|
|
|
|
# Start the application based on environment
|
|
if [ "$APP_ENV" = "production" ] || [ "$MODE" = "production" ]; then
|
|
echo "Starting PHP built-in server on port 8000 (Production mode)..."
|
|
echo "Document root: /var/www/html/public"
|
|
echo "Workers: Single-threaded PHP server (use Nginx for production scaling)"
|
|
# Use direct php -S for production (optimized)
|
|
exec php -d opcache.enable=1 -d opcache.memory_consumption=128 -d opcache.max_accelerated_files=10000 -S 0.0.0.0:8000 -t public
|
|
elif [ "$MODE" = "staging" ]; then
|
|
echo "Starting PHP built-in server on port 8000 (Staging mode)..."
|
|
# Use direct php -S to eliminate any artisan wrapper issues
|
|
exec php -d opcache.enable=0 -S 0.0.0.0:8000 -t public
|
|
else
|
|
echo "Starting PHP-FPM on port 9000 (Development mode)..."
|
|
# Ensure PHP-FPM is properly configured
|
|
exec php-fpm -F
|
|
fi |