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:
94
setup.bat
94
setup.bat
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user