#!/bin/bash # Immediate fix for Waitress port issue - run on VPS # This script fixes the service file to use hardcoded port from .env set -e echo "=== Immediate Fix for Waitress Port Issue ===" echo APP_DIR="/opt/mockapi" SERVICE_FILE="/etc/systemd/system/mockapi.service" ENV_FILE="${APP_DIR}/.env" # Check if running as root or with sudo if [[ $EUID -eq 0 ]]; then echo "Running as root." elif sudo -n true 2>/dev/null; then echo "Has sudo privileges." else echo "ERROR: This script needs sudo privileges." echo "Run with: sudo bash $0" exit 1 fi # Step 1: Check files exist if [[ ! -f "$ENV_FILE" ]]; then echo "ERROR: .env file not found at $ENV_FILE" exit 1 fi if [[ ! -f "$SERVICE_FILE" ]]; then echo "ERROR: Service file not found at $SERVICE_FILE" exit 1 fi # Step 2: Read port from .env PORT=$(grep "^PORT=" "$ENV_FILE" | cut -d'=' -f2 || echo "8000") HOST=$(grep "^HOST=" "$ENV_FILE" | cut -d'=' -f2 || echo "0.0.0.0") # Validate port is numeric if [[ ! "$PORT" =~ ^[0-9]+$ ]]; then echo "WARNING: PORT '$PORT' is not numeric. Using 8000." PORT="8000" # Fix .env sed -i "s/^PORT=.*/PORT=8000/" "$ENV_FILE" 2>/dev/null || \ echo "PORT=8000" >> "$ENV_FILE" fi echo "Found in .env: HOST=$HOST, PORT=$PORT" echo # Step 3: Backup service file BACKUP="${SERVICE_FILE}.backup.$(date +%s)" cp "$SERVICE_FILE" "$BACKUP" echo "Backup created: $BACKUP" # Step 4: Update service file echo "Updating service file..." # Get virtual environment path from current service file or use default VENV_DIR=$(grep "Environment=\"PATH=" "$SERVICE_FILE" | cut -d'=' -f2 | cut -d':' -f1 | sed 's/"//g' || echo "/opt/mockapi/venv") # Extract other settings from current file SERVICE_USER=$(grep "^User=" "$SERVICE_FILE" | cut -d'=' -f2) SERVICE_GROUP=$(grep "^Group=" "$SERVICE_FILE" | cut -d'=' -f2) # Create fixed service file cat > /tmp/mockapi_fixed.service << EOF [Unit] Description=Mock API Service After=network.target Wants=network.target [Service] Type=simple User=$SERVICE_USER Group=$SERVICE_GROUP WorkingDirectory=$APP_DIR Environment="PATH=$VENV_DIR/bin" Environment="PYTHONPATH=$APP_DIR" EnvironmentFile=$APP_DIR/.env # Fixed: Hardcoded port from .env ExecStart=$VENV_DIR/bin/waitress-serve --host=$HOST --port=$PORT wsgi:wsgi_app Restart=always RestartSec=10 StandardOutput=syslog StandardError=syslog SyslogIdentifier=mockapi # Security hardening NoNewPrivileges=true PrivateTmp=true ProtectSystem=strict ReadWritePaths=$APP_DIR [Install] WantedBy=multi-user.target EOF # Replace service file cp /tmp/mockapi_fixed.service "$SERVICE_FILE" rm /tmp/mockapi_fixed.service echo "Service file updated with hardcoded port $PORT." echo # Step 5: Reload and restart echo "Reloading systemd..." systemctl daemon-reload echo "Restarting service..." if systemctl restart mockapi; then echo "Service restart initiated." # Wait and check sleep 3 if systemctl is-active --quiet mockapi; then echo "SUCCESS: Service is running!" echo echo "MockAPI should now be accessible at:" echo " http://$(hostname -I | awk '{print $1}'):$PORT" echo " http://localhost:$PORT" echo echo "Test with: curl http://localhost:$PORT/health" else echo "WARNING: Service not active after restart." echo "Checking logs..." journalctl -u mockapi -n 20 --no-pager fi else echo "ERROR: Failed to restart service." systemctl status mockapi --no-pager -l fi echo echo "=== Fix completed ===" echo "If issues persist, restore backup:" echo " cp $BACKUP $SERVICE_FILE" echo " systemctl daemon-reload" echo " systemctl restart mockapi"