Resolving Node.js and Apache / Nginx Port Conflicts in Local Environments
One of the most common annoyances for web developers when setting up a local development environment occurs when different services try to reserve the same network port. A typical example is when we want to run a Node.js (Express) application locally alongside an Apache or Nginx web server.
By default, many systems assign port 8080 or 8081 to development servers. If we launch a Node.js application that listens on port 8080, while the Apache Virtual Host is also configured for it, we run into the infamous EADDRINUSE (Address already in use) error.
Steps to the Solution
The safest way to resolve the conflict is to move the local Apache configuration to a less commonly used unique port, such as 8090. To do this, we need to modify the Apache configuration file (httpd.conf or the virtual hosts file):
<VirtualHost *:8090>
ServerAdmin webmaster@localhost
DocumentRoot "/var/www/simplesolution"
ServerName simplesolution.local
ErrorLog "logs/simplesolution-error.log"
CustomLog "logs/simplesolution-access.log" common
</VirtualHost>
Do not forget to add the Listen 8090 directive to the main configuration as well. This frees up ports 8080 and 8081 for our Node.js applications, allowing both environments to run smoothly side by side.