#!/bin/bash # TrustOS Local Hosting & Cloudflare Tunnel Setup # This script sets up TrustOS to be accessible locally and via Cloudflare tunnel set -e echo "🚀 TrustOS Local Hosting Setup" echo "==============================" echo "" # Get machine IP MACHINE_IP=$(ip addr show | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}' | cut -d'/' -f1 | head -1) echo "Machine IP: $MACHINE_IP" echo "" # Detect OS if command -v apt-get &> /dev/null; then echo "✓ Ubuntu/Debian detected" INSTALL_CMD="apt-get install -y" OS="debian" elif command -v yum &> /dev/null; then echo "✓ CentOS/RHEL detected" INSTALL_CMD="yum install -y" OS="rhel" else echo "✗ Unsupported OS" exit 1 fi # Install Nginx if not present if ! command -v nginx &> /dev/null; then echo "Installing Nginx..." $INSTALL_CMD nginx fi # Install Cloudflared if not present if ! command -v cloudflared &> /dev/null; then echo "Installing Cloudflare Tunnel..." curl -s -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o /usr/local/bin/cloudflared chmod +x /usr/local/bin/cloudflared fi # Create Nginx config echo "Creating Nginx configuration..." mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled cat > /etc/nginx/sites-available/trustos << 'NGINX_CONFIG' # TrustOS Backend API upstream trustos_backend { server localhost:8000; } # TrustOS Frontend upstream trustos_frontend { server localhost:3000; } # API Server server { listen 80; server_name _; client_max_body_size 100M; # API endpoints location /api { proxy_pass http://trustos_backend; proxy_set_header Host $host; 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; # CORS headers add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always; if ($request_method = 'OPTIONS') { return 204; } } # Health checks location /health { proxy_pass http://trustos_backend; } location /docs { proxy_pass http://trustos_backend; } # Frontend location / { proxy_pass http://trustos_frontend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } NGINX_CONFIG # Enable site ln -sf /etc/nginx/sites-available/trustos /etc/nginx/sites-enabled/trustos 2>/dev/null || true # Test and start Nginx echo "Testing Nginx configuration..." nginx -t systemctl restart nginx echo "✓ Nginx configured and running" echo "" # Create Cloudflare tunnel setup echo "Setting up Cloudflare Tunnel..." echo "" echo "⚠️ IMPORTANT: To complete Cloudflare tunnel setup:" echo "1. Run: cloudflared tunnel login" echo "2. Follow the browser prompt to authenticate" echo "3. Then run: cloudflared tunnel create trustos" echo "4. Then run: cloudflared tunnel route dns trustos " echo "" cat > /root/trustos/start_tunnel.sh << 'TUNNEL_SCRIPT' #!/bin/bash # Start Cloudflare tunnel cloudflared tunnel run trustos --url http://localhost:80 TUNNEL_SCRIPT chmod +x /root/trustos/start_tunnel.sh # Create systemd service for tunnel (optional) cat > /etc/systemd/system/trustos-tunnel.service << 'SERVICE_CONFIG' [Unit] Description=TrustOS Cloudflare Tunnel After=network.target [Service] Type=simple User=root ExecStart=/usr/local/bin/cloudflared tunnel run trustos --url http://localhost:80 Restart=always RestartSec=5 [Install] WantedBy=multi-user.target SERVICE_CONFIG echo "✓ Cloudflare tunnel service created" echo "" # Display access information echo "════════════════════════════════════════════════" echo "🎉 TRUSTOS LOCAL HOSTING SETUP COMPLETE" echo "════════════════════════════════════════════════" echo "" echo "📍 LOCAL ACCESS:" echo " Frontend: http://$MACHINE_IP" echo " Backend API: http://$MACHINE_IP/api" echo " API Docs: http://$MACHINE_IP/docs" echo "" echo "🌐 CLOUDFLARE TUNNEL:" echo " To set up tunnel, run:" echo " 1. cloudflared tunnel login" echo " 2. cloudflared tunnel create trustos" echo " 3. cloudflared tunnel route dns trustos your-domain.com" echo " 4. systemctl start trustos-tunnel (or run: /root/trustos/start_tunnel.sh)" echo "" echo "📊 MONITORING:" echo " View Nginx logs: tail -f /var/log/nginx/access.log" echo " View Tunnel status: cloudflared tunnel info trustos" echo "" echo "✅ Services Status:" systemctl status nginx --no-pager -l 3 echo "" curl -s http://localhost:8000/health | jq . && echo "✓ Backend API is healthy" || echo "⚠️ Backend not responding yet" echo "" echo "════════════════════════════════════════════════"