Fix CloudSticks setup to handle missing nginx command

- Auto-detects nginx binary location
- Falls back to systemctl/service commands
- Better error handling
- More detailed status reporting
This commit is contained in:
2025-12-21 17:07:01 +11:00
parent 14598ac343
commit c3d4c9cb4b

View File

@@ -1,11 +1,10 @@
#!/bin/bash #!/bin/bash
# Simple CloudSticks Nginx Proxy Setup # CloudSticks Nginx Setup - Handles custom nginx paths
# Adds proxy config to http block
set -e set -e
echo "🔧 CloudSticks Nginx Proxy Setup (Simple Method)" echo "🔧 CloudSticks Nginx Proxy Setup"
echo "=================================================" echo "================================="
echo "" echo ""
GREEN='\033[0;32m' GREEN='\033[0;32m'
@@ -25,28 +24,22 @@ cp "$CONFIG_FILE" "${CONFIG_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
echo -e "${GREEN}✅ Backup created${NC}" echo -e "${GREEN}✅ Backup created${NC}"
echo "" echo ""
echo "Step 2: Adding proxy configuration..." echo "Step 2: Creating proxy configuration..."
mkdir -p /etc/nginx-cs/conf.d
# Create a separate config file for Connect-5
cat > /etc/nginx-cs/conf.d/connect5-proxy.conf << 'EOF' cat > /etc/nginx-cs/conf.d/connect5-proxy.conf << 'EOF'
# Connect-5 Proxy Configuration # Connect-5 Proxy Configuration
server { server {
listen 443 ssl; listen 443 ssl;
server_name connect5.beyondcloud.technology; server_name connect5.beyondcloud.technology;
# SSL certificates (CloudSticks should handle these)
# ssl_certificate and ssl_certificate_key are managed by CloudSticks
# Root directory
root /home/github2/apps/app-connect5; root /home/github2/apps/app-connect5;
index index.html; index index.html;
# Serve static files
location / { location / {
try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.html;
} }
# Proxy API requests to Node.js
location /api { location /api {
proxy_pass http://localhost:3000; proxy_pass http://localhost:3000;
proxy_http_version 1.1; proxy_http_version 1.1;
@@ -59,7 +52,6 @@ server {
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
} }
# Proxy Socket.io WebSocket requests
location /socket.io { location /socket.io {
proxy_pass http://localhost:3000; proxy_pass http://localhost:3000;
proxy_http_version 1.1; proxy_http_version 1.1;
@@ -74,58 +66,98 @@ server {
} }
EOF EOF
echo -e "${GREEN}✅ Proxy config created at /etc/nginx-cs/conf.d/connect5-proxy.conf${NC}" echo -e "${GREEN}✅ Proxy config created${NC}"
echo "" echo ""
echo "Step 3: Testing Nginx configuration..." echo "Step 3: Finding nginx command..."
if nginx -t 2>&1 | grep -q "successful\|syntax is ok"; then NGINX_CMD=""
echo -e "${GREEN}✅ Config valid${NC}" if command -v nginx &> /dev/null; then
NGINX_CMD="nginx"
elif [ -f "/usr/sbin/nginx" ]; then
NGINX_CMD="/usr/sbin/nginx"
elif [ -f "/usr/local/sbin/nginx" ]; then
NGINX_CMD="/usr/local/sbin/nginx"
elif [ -f "/opt/cloudsticks/nginx/sbin/nginx" ]; then
NGINX_CMD="/opt/cloudsticks/nginx/sbin/nginx"
else else
echo -e "${RED}❌ Config error${NC}" echo -e "${YELLOW}⚠️ Could not find nginx command${NC}"
nginx -t echo "Trying to reload via systemctl..."
echo "" fi
echo "Restoring backup..."
rm /etc/nginx-cs/conf.d/connect5-proxy.conf 2>/dev/null || true if [ -n "$NGINX_CMD" ]; then
exit 1 echo "Testing config with: $NGINX_CMD"
if $NGINX_CMD -t 2>&1 | grep -q "successful\|syntax is ok"; then
echo -e "${GREEN}✅ Config valid${NC}"
else
echo -e "${RED}❌ Config error${NC}"
$NGINX_CMD -t
rm /etc/nginx-cs/conf.d/connect5-proxy.conf
exit 1
fi
fi fi
echo "" echo ""
echo "Step 4: Reloading Nginx..." echo "Step 4: Reloading Nginx..."
systemctl reload nginx 2>/dev/null || service nginx reload 2>/dev/null || nginx -s reload if systemctl reload nginx 2>/dev/null; then
echo -e "${GREEN}Nginx reloaded${NC}" echo -e "${GREEN}Reloaded via systemctl${NC}"
elif service nginx reload 2>/dev/null; then
echo -e "${GREEN}✅ Reloaded via service${NC}"
elif [ -n "$NGINX_CMD" ]; then
$NGINX_CMD -s reload 2>/dev/null && echo -e "${GREEN}✅ Reloaded via nginx -s${NC}" || echo -e "${YELLOW}⚠️ Could not reload${NC}"
else
echo -e "${YELLOW}⚠️ Please restart nginx manually${NC}"
echo "Try: systemctl restart nginx"
fi
echo "" echo ""
echo "Step 5: Checking Node.js..." echo "Step 5: Checking Node.js..."
if pgrep -f "node server.js" > /dev/null; then if pgrep -f "node server.js" > /dev/null; then
echo -e "${GREEN}✅ Node.js running${NC}" PID=$(pgrep -f "node server.js")
echo -e "${GREEN}✅ Node.js running (PID: $PID)${NC}"
else else
echo -e "${YELLOW}⚠️ Starting Node.js...${NC}" echo -e "${YELLOW}⚠️ Starting Node.js...${NC}"
cd /home/github2/apps/app-connect5 cd /home/github2/apps/app-connect5
nohup node server.js > server.log 2>&1 & nohup node server.js > server.log 2>&1 &
sleep 2 sleep 2
echo -e "${GREEN}✅ Node.js started${NC}" if pgrep -f "node server.js" > /dev/null; then
echo -e "${GREEN}✅ Node.js started${NC}"
fi
fi fi
echo "" echo ""
echo "Step 6: Testing..." echo "Step 6: Testing endpoints..."
sleep 2 sleep 3
LOCAL=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/api/db-status 2>/dev/null || echo "000") LOCAL=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/api/db-status 2>/dev/null || echo "000")
PROD=$(curl -s -o /dev/null -w "%{http_code}" https://connect5.beyondcloud.technology/api/db-status 2>/dev/null || echo "000") echo "Local (localhost:3000): HTTP $LOCAL"
echo "Local endpoint: HTTP $LOCAL" if [ "$LOCAL" = "200" ]; then
echo "Production endpoint: HTTP $PROD" echo -e "${GREEN}✅ Node.js server is responding${NC}"
fi
PROD=$(curl -s -o /dev/null -w "%{http_code}" https://connect5.beyondcloud.technology/api/db-status 2>/dev/null || echo "000")
echo "Production: HTTP $PROD"
if [ "$PROD" = "200" ]; then if [ "$PROD" = "200" ]; then
echo -e "${GREEN}✅ Production is working!${NC}"
echo "" echo ""
echo -e "${GREEN}✅ SUCCESS! Production is working!${NC}" echo "Database Status:"
echo "" 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
curl -s https://connect5.beyondcloud.technology/api/db-status | python3 -m json.tool 2>/dev/null || curl -s https://connect5.beyondcloud.technology/api/db-status elif [ "$PROD" = "502" ]; then
echo -e "${YELLOW}⚠️ 502 Bad Gateway - Nginx is proxying but Node.js might not be ready${NC}"
elif [ "$PROD" = "404" ]; then
echo -e "${YELLOW}⚠️ 404 - Nginx might need manual restart${NC}"
echo "Try: sudo systemctl restart nginx"
fi fi
echo "" echo ""
echo "=================================================" echo "================================="
echo -e "${GREEN}🎉 Setup Complete!${NC}" echo -e "${GREEN}🎉 Setup Complete!${NC}"
echo "" echo ""
echo "Config file: /etc/nginx-cs/conf.d/connect5-proxy.conf"
echo "Visit: https://connect5.beyondcloud.technology/" echo "Visit: https://connect5.beyondcloud.technology/"
echo "=================================================" echo ""
echo "If not working, try:"
echo " sudo systemctl restart nginx"
echo " tail -f /home/github2/apps/app-connect5/server.log"
echo "================================="