Troubleshooting localhost Port Conflicts

Resolving issues with ports like localhost 80, 443, 3000, 5173 being occupied, preventing Apache, Vite, and Next.js from starting.


When starting XAMPP, Vite, Next.js, or Docker, you may frequently encounter “port already in use” or Address already in use errors. This article explains how to troubleshoot and free up common localhost ports.

Common Conflict Ports

PortCommon OccupantsTypical Error Scenarios
80Apache, IIS, Skype, CaddyXAMPP Apache cannot Start
443Apache HTTPS, IISSSL site startup failure
3000Next.js, Express, NestJS, CRASecond Node project startup failure
3306MySQL, MariaDBDocker MySQL conflicts with XAMPP
5173ViteMultiple Vite projects or previous process not exited
8080Tomcat, Backup ApacheJava / Second web service
5432PostgreSQLLocal PG conflicts with Docker PG
6379RedisDuplicate Redis installation

Checking Occupied Processes

macOS / Linux

# Check port 5173
lsof -i :5173

# Or
sudo ss -tlnp | grep 5173

The PID column in the output indicates the process ID.

Windows

netstat -ano | findstr :5173

The last column is the PID, which you can use in Task Manager or:

taskkill /PID <pid> /F

Terminating Processes (Unix)

kill <PID>
# Force
kill -9 <PID>

Common Scenarios and Solutions

XAMPP Apache Cannot Bind to 80

  1. Close IIS, Skype, and other web servers.
  2. Alternatively, modify httpd.conf from Listen 80 to Listen 8080, and use http://localhost:8080.
  3. On Windows, check if the World Wide Web Publishing Service is running.

Vite 5173 Occupied

Vite will automatically try 5174, 5175, etc., but you can also explicitly specify:

npm run dev -- --port 3000

Or set server.port in vite.config.ts.

Two MySQL Instances Running Simultaneously

XAMPP MySQL conflicts with Docker -p 3306:3306. Solutions:

  • Stop XAMPP MySQL and use only Docker.
  • Or map Docker to 3307:3306, and connect using localhost:3307.

Next.js and NestJS Competing for 3000

Change the port for one of them:

npm run dev -- -p 3001          # Next.js
PORT=3001 npm run start:dev     # NestJS (or modify in main.ts)

Prevention Recommendations

  • Close services in the XAMPP control panel when not in use.
  • Use docker compose down to release mapped ports for Docker projects.
  • Use the localhost-ports quick reference table to confirm default ports for various tools.
  • When running multiple projects in parallel, assign different ports to each project and document them in the README.

Conclusion

To resolve port conflicts, first use lsof / netstat to find the PID and terminate the process, or modify the service listening port. The most commonly conflicting ports in local development are 80 (Apache), 3000 (Node), 5173 (Vite), and 3306 (MySQL).

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run