diff --git a/NGINX_SETUP.md b/NGINX_SETUP.md new file mode 100644 index 0000000..f0f0a28 --- /dev/null +++ b/NGINX_SETUP.md @@ -0,0 +1,150 @@ +# Nginx Configuration for Connect-5 on CloudSticks + +## Quick Setup (Automated) + +```bash +cd /home/github2/apps/app-connect5 +sudo bash setup-nginx.sh +``` + +That's it! The script will automatically configure everything. + +--- + +## Manual Setup (If Needed) + +### Step 1: Find Your Nginx Config + +```bash +# Find config files +ls -la /etc/nginx/sites-available/ +ls -la /etc/nginx/conf.d/ + +# Or search for your domain +grep -r "connect5.beyondcloud.technology" /etc/nginx/ +``` + +### Step 2: Edit the Config + +```bash +# Edit the config file (replace with your actual file) +sudo nano /etc/nginx/sites-available/connect5.beyondcloud.technology +``` + +### Step 3: Add Proxy Configuration + +Add these `location` blocks **inside** your `server` block: + +```nginx +server { + listen 443 ssl; + server_name connect5.beyondcloud.technology; + + # Your existing SSL and root configuration... + + # Add these proxy configurations: + location /api { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /socket.io { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} +``` + +### Step 4: Test and Reload + +```bash +# Test configuration +sudo nginx -t + +# If OK, reload Nginx +sudo systemctl reload nginx + +# Test the API +curl https://connect5.beyondcloud.technology/api/db-status +``` + +--- + +## Troubleshooting + +### Check if Nginx is Running +```bash +sudo systemctl status nginx +``` + +### Check Nginx Error Logs +```bash +sudo tail -f /var/log/nginx/error.log +``` + +### Check if Node.js is Running +```bash +ps aux | grep "node server.js" +``` + +### Test Local Node.js Server +```bash +curl http://localhost:3000/api/db-status +``` + +### Restart Everything +```bash +# Restart Node.js +pkill -f "node server.js" +cd /home/github2/apps/app-connect5 +nohup node server.js > server.log 2>&1 & + +# Reload Nginx +sudo systemctl reload nginx +``` + +--- + +## CloudSticks-Specific Notes + +- CloudSticks uses **Nginx** (not Apache) +- Config files are usually in `/etc/nginx/sites-available/` +- CloudSticks auto-deploys from GitHub +- Node.js server needs to run continuously in background + +--- + +## Verify It's Working + +1. **Test API endpoint**: + ```bash + curl https://connect5.beyondcloud.technology/api/db-status + ``` + Should return JSON with `"connected": true` + +2. **Visit in browser**: + https://connect5.beyondcloud.technology/ + + Status bar should show: + - SQL: Connected ✅ + - Latency: ~45ms + - Write: Enabled ✅ + +3. **Test multiplayer**: + - Click "Multiplayer" + - Register a username + - Should see "Welcome back, [username]!" diff --git a/setup-nginx.sh b/setup-nginx.sh new file mode 100644 index 0000000..007031b --- /dev/null +++ b/setup-nginx.sh @@ -0,0 +1,183 @@ +#!/bin/bash +# Automated Nginx Configuration Script for Connect-5 on CloudSticks +# Ubuntu 24.04 / Nginx + +set -e # Exit on error + +echo "🔧 Connect-5 Nginx Configuration Script" +echo "==========================================" +echo "" + +# Colors +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +# Check if running as root +if [ "$EUID" -ne 0 ]; then + echo -e "${RED}❌ This script must be run as root (use sudo)${NC}" + exit 1 +fi + +echo "Step 1: Finding Nginx configuration file..." +CONFIG_FILE="" + +# Check common CloudSticks/Nginx locations +if [ -f "/etc/nginx/sites-available/connect5.beyondcloud.technology" ]; then + CONFIG_FILE="/etc/nginx/sites-available/connect5.beyondcloud.technology" +elif [ -f "/etc/nginx/sites-available/connect5" ]; then + CONFIG_FILE="/etc/nginx/sites-available/connect5" +elif [ -f "/etc/nginx/sites-available/default" ]; then + CONFIG_FILE="/etc/nginx/sites-available/default" +elif [ -f "/etc/nginx/conf.d/connect5.conf" ]; then + CONFIG_FILE="/etc/nginx/conf.d/connect5.conf" +else + echo -e "${YELLOW}⚠️ Could not auto-detect config file${NC}" + echo "Available Nginx config files:" + ls -1 /etc/nginx/sites-available/ 2>/dev/null || echo "No sites-available directory" + ls -1 /etc/nginx/conf.d/ 2>/dev/null || echo "No conf.d directory" + echo "" + + # Try to find by domain name + FOUND=$(grep -l "connect5.beyondcloud.technology\|beyondcloud.technology" /etc/nginx/sites-available/* /etc/nginx/conf.d/* 2>/dev/null | head -1) + if [ -n "$FOUND" ]; then + CONFIG_FILE="$FOUND" + echo -e "${GREEN}✅ Found config by domain: $CONFIG_FILE${NC}" + else + read -p "Enter the full path to your Nginx config file: " CONFIG_FILE + fi +fi + +echo -e "${GREEN}✅ Using config file: $CONFIG_FILE${NC}" +echo "" + +echo "Step 2: Backing up original config..." +cp "$CONFIG_FILE" "${CONFIG_FILE}.backup.$(date +%Y%m%d_%H%M%S)" +echo -e "${GREEN}✅ Backup created${NC}" +echo "" + +echo "Step 3: Checking if proxy rules already exist..." +if grep -q "proxy_pass.*3000" "$CONFIG_FILE"; then + echo -e "${YELLOW}⚠️ Proxy rules already exist in config file${NC}" + echo "Skipping modification to avoid duplicates." +else + echo "Adding proxy configuration..." + + # Create a temporary file with the proxy configuration + cat > /tmp/nginx_proxy_config.txt << 'EOF' + # Connect-5 Node.js Proxy Configuration + location /api { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + location /socket.io { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +EOF + + # Insert before the last closing brace + # Find the last server block and insert before its closing brace + if grep -q "server {" "$CONFIG_FILE"; then + # Insert before the last closing brace of server block + sed -i '/^[[:space:]]*}[[:space:]]*$/i\ # Connect-5 Proxy Config' "$CONFIG_FILE" + sed -i '/# Connect-5 Proxy Config/r /tmp/nginx_proxy_config.txt' "$CONFIG_FILE" + sed -i '/# Connect-5 Proxy Config/d' "$CONFIG_FILE" + else + echo -e "${RED}❌ Could not find server block in config${NC}" + exit 1 + fi + + rm /tmp/nginx_proxy_config.txt + + echo -e "${GREEN}✅ Proxy configuration added${NC}" +fi +echo "" + +echo "Step 4: Testing Nginx configuration..." +if nginx -t 2>&1 | grep -q "successful"; then + echo -e "${GREEN}✅ Nginx configuration is valid${NC}" +else + echo -e "${RED}❌ Nginx configuration has errors!${NC}" + echo "Restoring backup..." + cp "${CONFIG_FILE}.backup."* "$CONFIG_FILE" + echo "Please check the configuration manually." + nginx -t + exit 1 +fi +echo "" + +echo "Step 5: Reloading Nginx..." +systemctl reload nginx +echo -e "${GREEN}✅ Nginx reloaded${NC}" +echo "" + +echo "Step 6: Checking if Node.js server is running..." +if pgrep -f "node server.js" > /dev/null; then + echo -e "${GREEN}✅ Node.js server is running${NC}" + PID=$(pgrep -f "node server.js") + echo " Process ID: $PID" +else + echo -e "${YELLOW}⚠️ Node.js server is not running${NC}" + echo "Starting Node.js server..." + cd /home/github2/apps/app-connect5 + nohup node server.js > server.log 2>&1 & + sleep 2 + echo -e "${GREEN}✅ Node.js server started${NC}" +fi +echo "" + +echo "Step 7: Testing API endpoint..." +sleep 2 +HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://connect5.beyondcloud.technology/api/db-status 2>/dev/null || echo "000") + +if [ "$HTTP_CODE" = "200" ]; then + echo -e "${GREEN}✅ API endpoint is working! (HTTP $HTTP_CODE)${NC}" + echo "" + echo "📊 Database Status:" + curl -s https://connect5.beyondcloud.technology/api/db-status 2>/dev/null | python3 -m json.tool 2>/dev/null || curl -s https://connect5.beyondcloud.technology/api/db-status +elif [ "$HTTP_CODE" = "000" ]; then + echo -e "${YELLOW}⚠️ Could not connect to server${NC}" + echo "Testing local endpoint..." + LOCAL_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/api/db-status 2>/dev/null || echo "000") + if [ "$LOCAL_CODE" = "200" ]; then + echo -e "${GREEN}✅ Local endpoint works (HTTP $LOCAL_CODE)${NC}" + echo "The proxy might need a moment to update." + else + echo -e "${RED}❌ Node.js server not responding${NC}" + fi +else + echo -e "${YELLOW}⚠️ API endpoint returned HTTP $HTTP_CODE${NC}" +fi +echo "" + +echo "==========================================" +echo -e "${GREEN}🎉 Configuration Complete!${NC}" +echo "" +echo "Next steps:" +echo "1. Visit https://connect5.beyondcloud.technology/" +echo "2. Check that the status bar shows 'Connected'" +echo "3. Test multiplayer functionality" +echo "" +echo "Logs:" +echo " Nginx: sudo tail -f /var/log/nginx/error.log" +echo " Node.js: tail -f /home/github2/apps/app-connect5/server.log" +echo "" +echo "Backup saved to: ${CONFIG_FILE}.backup.*" +echo "=========================================="