#!/bin/bash # Apache Deployment Script for Bike Service Application # Make sure to run this script from the project root directory set -e # Exit on any error echo "🚀 Starting Apache deployment..." # Check if we're in the right directory if [ ! -f "backend/artisan" ] || [ ! -f "frontend/package.json" ]; then echo "❌ Error: Please run this script from the project root directory" exit 1 fi # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running on Ubuntu server if [[ "$OSTYPE" != "linux-gnu"* ]]; then print_warning "This script is designed for Ubuntu servers. Some commands may not work on your system." fi # Check if running as root if [[ $EUID -eq 0 ]]; then print_error "Please don't run this script as root. Use a regular user with sudo privileges." exit 1 fi # Check if required commands exist check_command() { if ! command -v $1 &> /dev/null; then print_error "$1 is not installed. Please install it first." exit 1 fi } print_status "Checking required commands..." check_command "php" check_command "composer" check_command "npm" check_command "git" check_command "mysql" # Get current branch CURRENT_BRANCH=$(git branch --show-current) print_status "Current branch: $CURRENT_BRANCH" # Ask for confirmation read -p "Do you want to deploy from branch '$CURRENT_BRANCH'? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_warning "Deployment cancelled." exit 0 fi # Pull latest changes print_status "Pulling latest changes from git..." git pull origin $CURRENT_BRANCH # Backend deployment print_status "Deploying Laravel backend..." cd backend # Install/update dependencies print_status "Installing Composer dependencies..." composer install --no-dev --optimize-autoloader # Check if .env exists, if not copy from example if [ ! -f ".env" ]; then print_warning ".env file not found, copying from .env.example..." if [ -f ".env.example" ]; then cp .env.example .env print_warning "Please configure your .env file before continuing!" print_warning "Make sure to set up your MySQL database configuration." read -p "Press Enter after configuring .env file..." else print_error ".env.example file not found!" exit 1 fi fi # Generate key if not set if ! grep -q "APP_KEY=base64:" .env; then print_status "Generating application key..." php artisan key:generate fi # Check database connection print_status "Testing database connection..." if php artisan migrate:status > /dev/null 2>&1; then print_status "Database connection successful!" else print_error "Database connection failed! Please check your .env configuration." print_warning "Make sure MySQL is running and your database credentials are correct." exit 1 fi # Run migrations print_status "Running database migrations..." php artisan migrate --force # Clear and rebuild caches print_status "Clearing and rebuilding caches..." php artisan config:clear php artisan route:clear php artisan view:clear php artisan cache:clear php artisan config:cache php artisan route:cache php artisan view:cache # Set production environment if ! grep -q "APP_ENV=production" .env; then print_status "Setting production environment..." sed -i 's/APP_ENV=.*/APP_ENV=production/' .env fi if ! grep -q "APP_DEBUG=false" .env; then print_status "Disabling debug mode..." sed -i 's/APP_DEBUG=.*/APP_DEBUG=false/' .env fi cd .. # Frontend deployment print_status "Deploying Vue.js frontend..." cd frontend # Install/update dependencies print_status "Installing npm dependencies..." npm install # Build for production print_status "Building frontend for production..." npm run build # Copy built files to Laravel public directory print_status "Copying built files to Laravel public directory..." if [ -d "dist" ]; then cp -r dist/* ../backend/public/ print_status "Frontend files copied successfully" else print_error "dist directory not found! Build may have failed." exit 1 fi cd .. # Set proper permissions print_status "Setting proper permissions..." if command -v sudo &> /dev/null; then sudo chown -R www-data:www-data backend/storage backend/bootstrap/cache sudo chmod -R 775 backend/storage backend/bootstrap/cache print_status "Permissions set successfully" else print_warning "sudo not available, skipping permission changes" fi # Restart services (if running on server) if command -v sudo &> /dev/null; then print_status "Restarting services..." # Check if Apache is running and restart it if sudo systemctl is-active --quiet apache2; then sudo systemctl restart apache2 print_status "Apache restarted" else print_warning "Apache is not running. Please start it manually: sudo systemctl start apache2" fi # Check if MySQL is running if sudo systemctl is-active --quiet mysql; then print_status "MySQL is running" else print_warning "MySQL is not running. Please start it manually: sudo systemctl start mysql" fi # Check if queue worker service exists and restart it if sudo systemctl list-units --type=service | grep -q "bikeservice-queue"; then sudo systemctl restart bikeservice-queue print_status "Queue worker restarted" else print_warning "Queue worker service not found. You may need to set it up manually." fi else print_warning "sudo not available, skipping service restarts" fi # Final checks print_status "Running final checks..." # Check if Laravel is working cd backend if php artisan --version &> /dev/null; then print_status "Laravel is working correctly" else print_error "Laravel is not working correctly" exit 1 fi # Check if frontend files are accessible if [ -f "public/index.html" ]; then print_status "Frontend files are in place" else print_error "Frontend files are missing" exit 1 fi # Check Apache configuration if command -v sudo &> /dev/null && command -v apache2ctl &> /dev/null; then print_status "Testing Apache configuration..." if sudo apache2ctl configtest > /dev/null 2>&1; then print_status "Apache configuration is valid" else print_warning "Apache configuration has issues. Run 'sudo apache2ctl configtest' for details." fi fi cd .. echo echo -e "${GREEN}✅ Apache deployment completed successfully!${NC}" echo echo "Next steps:" echo "1. Test your application in the browser" echo "2. Check application logs if there are any issues" echo "3. Verify all features are working correctly" echo "4. Set up SSL certificate if needed" echo echo "Useful commands:" echo "- Check Laravel logs: tail -f backend/storage/logs/laravel.log" echo "- Check Apache error logs: sudo tail -f /var/log/apache2/error.log" echo "- Check Apache access logs: sudo tail -f /var/log/apache2/access.log" echo "- Test Apache config: sudo apache2ctl configtest" echo "- Restart Apache: sudo systemctl restart apache2" echo "- Check MySQL status: sudo systemctl status mysql" echo echo "Database information:" echo "- Make sure MySQL is running: sudo systemctl start mysql" echo "- Check database connection: mysql -u your_username -p your_database" echo echo "Apache configuration:" echo "- Virtual host: /etc/apache2/sites-available/bikeservice.conf" echo "- Enable site: sudo a2ensite bikeservice.conf" echo "- Disable default: sudo a2dissite 000-default.conf"