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

View File

@@ -24,53 +24,74 @@ echo.
echo Setting up with %DB%...
echo.
REM Step 1: Check port availability
REM Step 1: Check port availability and auto-assign alternatives
echo Checking port availability...
set PORTS_OK=1
REM Check port 8080 (Nginx)
REM Function to find next available port
REM Usage: call :find_port <start_port> <result_var>
goto :skip_functions
:find_port
set /a port=%~1
:find_port_loop
netstat -ano | findstr ":%port% " >nul 2>&1
if %errorlevel% equ 0 (
set /a port+=1
goto :find_port_loop
)
set %~2=%port%
exit /b 0
:skip_functions
REM Check and assign APP_PORT (Web Server)
netstat -ano | findstr ":8080" >nul 2>&1
if %errorlevel% equ 0 (
echo [X] Port 8080 ^(Web Server^) - IN USE
set PORTS_OK=0
call :find_port 8081 APP_PORT
echo [!] Port 8080 in use - using !APP_PORT! for Web Server
) else (
set APP_PORT=8080
echo [OK] Port 8080 ^(Web Server^) - Available
)
REM Check port 8025 (Mailpit Dashboard)
REM Check and assign MAIL_DASHBOARD_PORT (Mailpit Dashboard)
netstat -ano | findstr ":8025" >nul 2>&1
if %errorlevel% equ 0 (
echo [X] Port 8025 ^(Mailpit Dashboard^) - IN USE
set PORTS_OK=0
call :find_port 8026 MAIL_DASHBOARD_PORT
echo [!] Port 8025 in use - using !MAIL_DASHBOARD_PORT! for Mailpit Dashboard
) else (
set MAIL_DASHBOARD_PORT=8025
echo [OK] Port 8025 ^(Mailpit Dashboard^) - Available
)
REM Check port 1025 (Mailpit SMTP)
REM Check and assign MAIL_PORT (Mailpit SMTP)
netstat -ano | findstr ":1025" >nul 2>&1
if %errorlevel% equ 0 (
echo [X] Port 1025 ^(Mailpit SMTP^) - IN USE
set PORTS_OK=0
call :find_port 1026 MAIL_PORT
echo [!] Port 1025 in use - using !MAIL_PORT! for Mailpit SMTP
) else (
set MAIL_PORT=1025
echo [OK] Port 1025 ^(Mailpit SMTP^) - Available
)
REM Check port 6379 (Redis)
REM Check and assign REDIS_PORT
netstat -ano | findstr ":6379" >nul 2>&1
if %errorlevel% equ 0 (
echo [X] Port 6379 ^(Redis^) - IN USE
set PORTS_OK=0
call :find_port 6380 REDIS_PORT
echo [!] Port 6379 in use - using !REDIS_PORT! for Redis
) else (
set REDIS_PORT=6379
echo [OK] Port 6379 ^(Redis^) - Available
)
REM Check database port based on selection
REM Check and assign DB_PORT based on database selection
if "%DB%"=="mysql" (
netstat -ano | findstr ":3306" >nul 2>&1
if !errorlevel! equ 0 (
echo [X] Port 3306 ^(MySQL^) - IN USE
set PORTS_OK=0
call :find_port 3307 DB_PORT
echo [!] Port 3306 in use - using !DB_PORT! for MySQL
) else (
set DB_PORT=3306
echo [OK] Port 3306 ^(MySQL^) - Available
)
)
@@ -78,39 +99,28 @@ if "%DB%"=="mysql" (
if "%DB%"=="pgsql" (
netstat -ano | findstr ":5432" >nul 2>&1
if !errorlevel! equ 0 (
echo [X] Port 5432 ^(PostgreSQL^) - IN USE
set PORTS_OK=0
call :find_port 5433 DB_PORT
echo [!] Port 5432 in use - using !DB_PORT! for PostgreSQL
) else (
set DB_PORT=5432
echo [OK] Port 5432 ^(PostgreSQL^) - Available
)
)
REM Handle port conflicts
if %PORTS_OK% equ 0 (
echo.
echo WARNING: Some required ports are already in use!
echo.
echo To find what's using a port:
echo netstat -ano ^| findstr ":PORT"
echo.
echo To stop a process using a port:
echo 1. Find the PID from netstat output
echo 2. taskkill /PID [PID] /F
echo.
choice /C YN /M "Do you want to continue anyway"
if errorlevel 2 (
echo Setup cancelled.
exit /b 1
)
echo.
echo Continuing with setup...
)
echo.
REM Step 2: Configure environment
echo Configuring environment...
if exist "src\.env.%DB%" (
copy /y "src\.env.%DB%" "src\.env" >nul
REM 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
if defined DB_PORT echo DB_PORT=%DB_PORT% >> src\.env
echo Environment configured for %DB%
) else (
echo ERROR: Template file src\.env.%DB% not found
@@ -181,9 +191,9 @@ echo ========================================================
echo.
echo Your Laravel application is ready!
echo.
echo Laravel App: http://localhost:8080
echo Admin Panel: http://localhost:8080/admin
echo Mailpit: http://localhost:8025
echo Laravel App: http://localhost:%APP_PORT%
echo Admin Panel: http://localhost:%APP_PORT%/admin
echo Mailpit: http://localhost:%MAIL_DASHBOARD_PORT%
echo.
echo Admin Login:
echo Email: admin@example.com

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"