#!/bin/bash # Nginx Setup Helper for FetchML # This script helps integrate FetchML into an existing nginx setup set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SITE_CONFIG="$SCRIPT_DIR/fetchml-site.conf" # Colors GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' echo -e "${BLUE}FetchML Nginx Setup Helper${NC}" echo "" # Check if nginx is installed if ! command -v nginx &>/dev/null; then echo -e "${YELLOW}Nginx is not installed.${NC}" echo "Install with:" echo " Ubuntu/Debian: sudo apt-get install nginx" echo " RHEL/Rocky: sudo dnf install nginx" exit 1 fi # Detect nginx config structure if [ -d "/etc/nginx/sites-available" ]; then # Debian/Ubuntu style SITES_AVAILABLE="/etc/nginx/sites-available" SITES_ENABLED="/etc/nginx/sites-enabled" STYLE="debian" elif [ -d "/etc/nginx/conf.d" ]; then # RHEL/CentOS style SITES_AVAILABLE="/etc/nginx/conf.d" SITES_ENABLED="" STYLE="rhel" else echo -e "${YELLOW}Could not detect nginx configuration directory.${NC}" echo "Please manually copy $SITE_CONFIG to your nginx config directory." exit 1 fi echo "Detected nginx style: $STYLE" echo "" # Read values read -p "Enter your domain name (e.g., ml.example.com): " domain read -p "Enter API server port [9102]: " port port=${port:-9102} read -p "Enter SSL certificate path: " cert_path read -p "Enter SSL key path: " key_path # Create temp config with substitutions temp_config="/tmp/fetchml-site.conf" sed -e "s|ml\.example\.com|$domain|g" \ -e "s|localhost:9102|localhost:$port|g" \ -e "s|/etc/ssl/certs/ml\.example\.com\.crt|$cert_path|g" \ -e "s|/etc/ssl/private/ml\.example\.com\.key|$key_path|g" \ "$SITE_CONFIG" > "$temp_config" # Install config echo "" echo -e "${BLUE}Installing nginx configuration...${NC}" if [ "$STYLE" = "debian" ]; then sudo cp "$temp_config" "$SITES_AVAILABLE/fetchml" sudo ln -sf "$SITES_AVAILABLE/fetchml" "$SITES_ENABLED/fetchml" echo -e "${GREEN}✓${NC} Config installed to $SITES_AVAILABLE/fetchml" echo -e "${GREEN}✓${NC} Symlink created in $SITES_ENABLED/" else sudo cp "$temp_config" "$SITES_AVAILABLE/fetchml.conf" echo -e "${GREEN}✓${NC} Config installed to $SITES_AVAILABLE/fetchml.conf" fi # Test nginx config echo "" echo -e "${BLUE}Testing nginx configuration...${NC}" if sudo nginx -t; then echo -e "${GREEN}✓${NC} Nginx configuration is valid" # Offer to reload read -p "Reload nginx now? [y/N]: " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then sudo systemctl reload nginx echo -e "${GREEN}✓${NC} Nginx reloaded" else echo "Reload later with: sudo systemctl reload nginx" fi else echo -e "${YELLOW}!${NC} Nginx configuration test failed" echo "Please fix the errors and run: sudo nginx -t" fi # Cleanup rm -f "$temp_config" echo "" echo -e "${GREEN}Setup complete!${NC}" echo "" echo "Your site is configured for: https://$domain" echo "" echo "Next steps:" echo " 1. Ensure your DNS points to this server" echo " 2. Start FetchML API server on port $port" echo " 3. Visit https://$domain/health to test"