#!/bin/bash # Automatic 502 Error Fix Script # Tries common fixes for Bad Gateway errors set -e echo "๐Ÿ”ง LuckyChit 502 Auto-Fix" echo "=========================" echo "" # Function to check if backend is responding check_backend() { RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/health 2>/dev/null) [ "$RESPONSE" = "200" ] } # Function to check if frontend is responding check_frontend() { RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080 2>/dev/null) [ "$RESPONSE" = "200" ] } echo "๐Ÿ“Š Step 1/6: Checking current status..." pm2 status echo "" echo "๐Ÿ”„ Step 2/6: Attempting PM2 restart..." pm2 restart all sleep 3 echo "" echo "๐Ÿงช Step 3/6: Testing services..." # Test backend if check_backend; then echo "โœ… Backend is responding" else echo "โš ๏ธ Backend not responding, trying to start..." cd /home/luckychit/apps/chitfund/backend pm2 delete luckychit-api 2>/dev/null || true pm2 start src/server.js --name luckychit-api sleep 3 if check_backend; then echo "โœ… Backend started successfully" else echo "โŒ Backend still not responding" echo "Check logs: pm2 logs luckychit-api" fi fi # Test frontend if check_frontend; then echo "โœ… Frontend is responding" else echo "โš ๏ธ Frontend not responding, trying to start..." cd /home/luckychit/apps/chitfund/luckychit pm2 delete luckychit-frontend 2>/dev/null || true pm2 serve build/web 8080 --name luckychit-frontend --spa sleep 3 if check_frontend; then echo "โœ… Frontend started successfully" else echo "โŒ Frontend still not responding" echo "May need to rebuild: flutter build web --release" fi fi echo "" echo "๐Ÿ’พ Step 4/6: Saving PM2 configuration..." pm2 save echo "" echo "๐Ÿ”ฅ Step 5/6: Checking firewall..." sudo ufw status | grep -E '(3000|8080)' || { echo "โš ๏ธ Firewall rules may be missing, adding..." sudo ufw allow 3000/tcp sudo ufw allow 8080/tcp sudo ufw reload echo "โœ… Firewall rules added" } echo "" echo "๐Ÿ“Š Step 6/6: Final status check..." pm2 status echo "" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" echo "๐Ÿงช Testing Services" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" # Test backend if check_backend; then echo "โœ… Backend: http://localhost:3000/health" curl -s http://localhost:3000/health | head -n 3 else echo "โŒ Backend: Not responding" fi echo "" # Test frontend if check_frontend; then echo "โœ… Frontend: http://localhost:8080" else echo "โŒ Frontend: Not responding" fi echo "" # Test external access EXTERNAL=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:3000/health 2>/dev/null) if [ "$EXTERNAL" = "200" ]; then echo "โœ… External access: http://192.168.8.148:3000" else echo "โŒ External access: Not accessible (may be firewall)" fi echo "" echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" # Final verdict if check_backend && check_frontend; then echo "โœ… SUCCESS! Both services are running" echo "" echo "๐ŸŒ Your site should now be accessible at:" echo " https://chitfund.deepteklabs.com" echo "" echo "๐Ÿ’ก If still showing 502, wait 1-2 minutes for Cloudflare to detect" echo " Or purge Cloudflare cache in dashboard" else echo "โš ๏ธ WARNING: Some services are still down" echo "" echo "๐Ÿ” Next steps:" echo " 1. Check logs: pm2 logs" echo " 2. Check database: sudo systemctl status postgresql" echo " 3. Run full diagnostic: ./diagnose-502.sh" echo " 4. See detailed guide: FIX_502_ERROR.md" fi echo ""