#!/bin/bash # Configuration APP_DIR="/var/www/your-website" BRANCH="main" WEB_SERVER_SERVICE="nginx" # or apache2 # Navigate to the application directory cd "$APP_DIR" || { echo "Error: Application directory $APP_DIR not found."; exit 1; } # Pull latest changes from Git echo "Pulling latest changes from Git branch '$BRANCH'... " git pull origin "$BRANCH" || { echo "Error: Git pull failed."; exit 1; } # Install/update dependencies (example for Node.js and PHP) if [ -f "package.json" ]; then echo "Installing Node.js dependencies... " npm install --production || { echo "Error: npm install failed."; exit 1; } fi if [ -f "composer.json" ]; then echo "Installing PHP dependencies with Composer... " composer install --no-dev --optimize-autoloader || { echo "Error: Composer install failed."; exit 1; } fi # Run database migrations (example for Laravel/PHP) if [ -f "artisan" ]; then echo "Running database migrations... " php artisan migrate --force || { echo "Error: Database migrations failed."; exit 1; } php artisan cache:clear php artisan config:cache php artisan view:cache fi # Clear cache (example for general web apps) # You might need specific commands for your framework # rm -rf /var/www/your-website/cache/* # Restart web server (adjust service name as needed) echo "Restarting $WEB_SERVER_SERVICE service... " sudo systemctl restart "$WEB_SERVER_SERVICE" || { echo "Error: Failed to restart $WEB_SERVER_SERVICE."; exit 1; } echo "Deployment successful for $APP_DIR on branch $BRANCH!"