Add automatic port selection to avoid conflicts

Instead of stopping existing processes, setup scripts now automatically
find and use alternative ports if defaults are in use.

Features:
- Automatically detects port conflicts
- Finds next available port (e.g., 8080 -> 8081 -> 8082...)
- Writes custom ports to .env file
- Shows which ports were reassigned
- Displays correct URLs at end of setup
- Zero user interaction needed

Implementation:
- setup.bat: Uses netstat and batch functions to find free ports
- setup.sh: Uses lsof/netstat and bash functions to find free ports
- Ports checked: APP_PORT, MAIL_DASHBOARD_PORT, MAIL_PORT, REDIS_PORT, DB_PORT
- All ports written to src/.env for docker-compose to use

Example output:
  [OK] Port 8080 (Web Server) - Available
  [!] Port 8025 in use - using 8026 for Mailpit Dashboard

  Your app will be at: http://localhost:8080
  Mailpit at: http://localhost:8026

This ensures setup never fails due to port conflicts and doesn't
disrupt existing services.
This commit is contained in:
2026-03-10 20:06:54 +02:00
parent 0d2506f3ef
commit ac11580eb3
2 changed files with 120 additions and 86 deletions

112
setup.sh
View File

@@ -32,63 +32,87 @@ fi
echo -e "${YELLOW}Setting up with ${DB}...${NC}"
echo ""
# Step 1: Check port availability
# Step 1: Check port availability and auto-assign alternatives
echo -e "${YELLOW}→ Checking port availability...${NC}"
PORTS_OK=1
# Function to check if port is in use
check_port() {
# Function to find next available port
find_port() {
local port=$1
local service=$2
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":$port "; then
echo -e "${RED}[✗] Port $port ($service) - IN USE${NC}"
PORTS_OK=0
else
echo -e "${GREEN}[✓] Port $port ($service) - Available${NC}"
fi
while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":$port "; do
port=$((port + 1))
done
echo $port
}
# Check required ports
check_port 8080 "Web Server"
check_port 8025 "Mailpit Dashboard"
check_port 1025 "Mailpit SMTP"
check_port 6379 "Redis"
# Check and assign APP_PORT (Web Server)
if lsof -Pi :8080 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":8080 "; then
APP_PORT=$(find_port 8081)
echo -e "${YELLOW}[!] Port 8080 in use - using $APP_PORT for Web Server${NC}"
else
APP_PORT=8080
echo -e "${GREEN}[✓] Port 8080 (Web Server) - Available${NC}"
fi
# Check database port based on selection
# Check and assign MAIL_DASHBOARD_PORT (Mailpit Dashboard)
if lsof -Pi :8025 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":8025 "; then
MAIL_DASHBOARD_PORT=$(find_port 8026)
echo -e "${YELLOW}[!] Port 8025 in use - using $MAIL_DASHBOARD_PORT for Mailpit Dashboard${NC}"
else
MAIL_DASHBOARD_PORT=8025
echo -e "${GREEN}[✓] Port 8025 (Mailpit Dashboard) - Available${NC}"
fi
# Check and assign MAIL_PORT (Mailpit SMTP)
if lsof -Pi :1025 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":1025 "; then
MAIL_PORT=$(find_port 1026)
echo -e "${YELLOW}[!] Port 1025 in use - using $MAIL_PORT for Mailpit SMTP${NC}"
else
MAIL_PORT=1025
echo -e "${GREEN}[✓] Port 1025 (Mailpit SMTP) - Available${NC}"
fi
# Check and assign REDIS_PORT
if lsof -Pi :6379 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":6379 "; then
REDIS_PORT=$(find_port 6380)
echo -e "${YELLOW}[!] Port 6379 in use - using $REDIS_PORT for Redis${NC}"
else
REDIS_PORT=6379
echo -e "${GREEN}[✓] Port 6379 (Redis) - Available${NC}"
fi
# Check and assign DB_PORT based on database selection
if [ "$DB" = "mysql" ]; then
check_port 3306 "MySQL"
if lsof -Pi :3306 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":3306 "; then
DB_PORT=$(find_port 3307)
echo -e "${YELLOW}[!] Port 3306 in use - using $DB_PORT for MySQL${NC}"
else
DB_PORT=3306
echo -e "${GREEN}[✓] Port 3306 (MySQL) - Available${NC}"
fi
elif [ "$DB" = "pgsql" ]; then
check_port 5432 "PostgreSQL"
if lsof -Pi :5432 -sTCP:LISTEN -t >/dev/null 2>&1 || netstat -tuln 2>/dev/null | grep -q ":5432 "; then
DB_PORT=$(find_port 5433)
echo -e "${YELLOW}[!] Port 5432 in use - using $DB_PORT for PostgreSQL${NC}"
else
DB_PORT=5432
echo -e "${GREEN}[✓] Port 5432 (PostgreSQL) - Available${NC}"
fi
fi
# Handle port conflicts
if [ $PORTS_OK -eq 0 ]; then
echo ""
echo -e "${RED}WARNING: Some required ports are already in use!${NC}"
echo ""
echo "To find what's using a port:"
echo " lsof -i :PORT"
echo " netstat -tuln | grep :PORT"
echo ""
echo "To stop a process using a port:"
echo " 1. Find the PID: lsof -ti :PORT"
echo " 2. Kill the process: kill -9 PID"
echo ""
read -p "Do you want to continue anyway? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${RED}Setup cancelled.${NC}"
exit 1
fi
echo ""
echo -e "${YELLOW}Continuing with setup...${NC}"
fi
echo ""
# Step 2: Configure environment
echo -e "${YELLOW}→ Configuring environment...${NC}"
if [ -f "src/.env.${DB}" ]; then
cp "src/.env.${DB}" "src/.env"
# Append port configurations to .env
echo "APP_PORT=$APP_PORT" >> src/.env
echo "MAIL_DASHBOARD_PORT=$MAIL_DASHBOARD_PORT" >> src/.env
echo "MAIL_PORT=$MAIL_PORT" >> src/.env
echo "REDIS_PORT=$REDIS_PORT" >> src/.env
[ -n "$DB_PORT" ] && echo "DB_PORT=$DB_PORT" >> src/.env
echo -e "${GREEN}✓ Environment configured for ${DB}${NC}"
else
echo -e "${RED}✗ Template file src/.env.${DB} not found${NC}"
@@ -153,9 +177,9 @@ echo -e "${GREEN}╚════════════════════
echo ""
echo -e "${GREEN}Your Laravel application is ready!${NC}"
echo ""
echo -e " 📱 Laravel App: ${GREEN}http://localhost:8080${NC}"
echo -e " 🔐 Admin Panel: ${GREEN}http://localhost:8080/admin${NC}"
echo -e " 📧 Mailpit: ${GREEN}http://localhost:8025${NC}"
echo -e " 📱 Laravel App: ${GREEN}http://localhost:$APP_PORT${NC}"
echo -e " 🔐 Admin Panel: ${GREEN}http://localhost:$APP_PORT/admin${NC}"
echo -e " 📧 Mailpit: ${GREEN}http://localhost:$MAIL_DASHBOARD_PORT${NC}"
echo ""
echo -e "${YELLOW}Admin Login:${NC}"
echo -e " Email: admin@example.com"