0

I am about to switch my node application server from phusion passenger to pm2.

Most of the ports of my apps are set to 3001. With passenger that's never been a problem, but with pm2 ports collide (EADDRINUSE).

Do I have to set a different port for every app to prevent port collisions?

LongHike
  • 4,016
  • 4
  • 37
  • 76
  • Yes. Unlike Passenger, `pm2` is not innately aware of such configurations for the apps it’s monitoring, and it’d be on you to manage the port assignments independently. – esqew Jul 27 '22 at 09:30

1 Answers1

0

Yes, of course you need to have each application listen on a different and free port. It is your app that listen to port, not PM2.

You can leave the same port in source code, but in this case, start your app like this to change port when starting your app:

// Work for express and some others
PORT=3012 pm2 start -n "My Application" app.js

That's because express add this in your starter script:

var port  = normalizePort( process.env.PORT || '3509' );

Notice that other package may use another name env var, like NODE_PORT for example.

Alaindeseine
  • 3,260
  • 1
  • 11
  • 21
  • I am aware that the ports are application bound. But as I mentioned, when I asked my initial question, passenger e.g. applies some mechanisms to set ports for each application so they never collide. I get your approach, it's just quite a handful to manage, if one's got more than let's say 20 applications running on a server. – LongHike Aug 07 '22 at 13:59