Resolving Port Binding Failures in PhpStorm on macOS
When attempting to run a local development server via PhpStorm, you may encounter a "Failed to listen on localhost:80 (reason: Permission denied)" error. This is distinct from an application crash; it indicates that the PHP built-in web server process spawned by the IDE lacks the necessary privileges to bind to port 80. On macOS, this restriction stems from the system's security model, specifically the requirement for sudo privileges to listen on ports below 1024.
Attempting to bypass this by switching to alternative ports often leads to a secondary error: "Address already in use." This occurs because while your PHP server cannot bind to port 80, another process is likely occupying that space or the range of low-numbered ports. In the scenario described, XAMPP is installed and running successfully when accessed directly via the browser. However, PhpStorm attempts to manage its own internal development server, which conflicts with these existing processes.
The core issue here is a collision between two different PHP development servers trying to claim the same network resources. XAMPP typically spawns an Apache process listening on port 80 (or 8080 by default). When you launch the Laravel or PHP application within PhpStorm, it triggers a new php -S instance. If the first instance isn't gracefully shut down, the second one fails to bind.
To resolve this, you must ensure that no other process is holding the port you intend to use. Run the following command in your terminal to identify what is occupying port 80:
sudo lsof -i :80
If the output lists apache2 or a process associated with XAMPP, you have two options. You can either stop the XAMPP services entirely before running the project in PhpStorm, or configure PhpStorm to listen on a different port that is free and accessible. For local development, using ports like 8080, 9000, or 3000 is standard practice because they do not require elevated privileges.
If you choose to keep XAMPP running for other services (like a MySQL database), ensure it is bound to a specific port different from the one PhpStorm wants. You can configure XAMPP's httpd.conf to use a high-numbered port, or simply stop the Apache service when focusing solely on PHP development tasks within the IDE.
Another critical step involves checking the configuration within PhpStorm itself. Navigate to Settings/Preferences > PHP > Servers. Ensure that the "Use local server" option is unchecked if you are relying on an external server like XAMPP. Alternatively, if you want to use the built-in PHP server, make sure the port specified in the project's configuration file (often found in php.ini or .env) matches a free port.
For Laravel applications specifically, check your .env file. Ensure that the APP_URL reflects the actual address you are running on, and that no static route or middleware is forcing traffic to a restricted port. If the .env file explicitly sets APP_URL=http://localhost:80, changing it to http://localhost:8080 will align with the permission-safe approach.
Finally, consider whether you actually need XAMPP installed if you are using PhpStorm. Modern development workflows often favor running a standalone PHP server or using Docker containers for database isolation. Removing XAMPP eliminates the process conflict entirely, allowing PhpStorm to manage its own server lifecycle without interference from legacy Apache configurations. This simplifies the environment and reduces the likelihood of port binding errors.