Pm2: Possible to run multiple instances of same app on different ports

Created on 24 Feb 2014  Â·  4Comments  Â·  Source: Unitech/pm2

Hi,

I wanted to inquire whether it's possible to run my app.js multiple clusters like so:

app.js Cluster1 port 80
….
app.js Cluster2 port 81
….
app.js Cluster 3 port 82
….
app.js Cluster 4 port 83

My hosting provider, Rackspace, has advised me to try to set up this way.

Is this possible to do this under pm2 and have my app.js be running multiple clusters under different ports?

Thanks in advance for your help.

Most helpful comment

NODE_PORT=80 pm2 start app.js -f
NODE_PORT=81 pm2 start app.js -f
NODE_PORT=82 pm2 start app.js -f
NODE_PORT=83 pm2 start app.js -f

... where NODE_PORT is an arbitrary name that you add as .listen(process.env.NODE_PORT) into your program.

(-f flag is necessary to force re-execution, since pm2 will complain otherwise)

All 4 comments

You could clone your app 4 times... I think there are better solutions as load-balancing for those kind of things but pm2 can't handle this without some heavy mods.

NODE_PORT=80 pm2 start app.js -f
NODE_PORT=81 pm2 start app.js -f
NODE_PORT=82 pm2 start app.js -f
NODE_PORT=83 pm2 start app.js -f

... where NODE_PORT is an arbitrary name that you add as .listen(process.env.NODE_PORT) into your program.

(-f flag is necessary to force re-execution, since pm2 will complain otherwise)

What is the benefit of doing so ? @waygee
Deploying the same application on multiple ports and then attaching a proxy say nginx will help you serve requests better ?

https://pm2.keymetrics.io/docs/usage/environment/

module.exports = { apps : [ { name: "myapp", script: "./app.js", instances: 2, exec_mode: "cluster", watch: true, increment_var : 'PORT', env: { "PORT": 3000, "NODE_ENV": "development" } } ] }
Use increment_var as in example

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexpts picture alexpts  Â·  3Comments

ghost picture ghost  Â·  3Comments

rajendar38 picture rajendar38  Â·  3Comments

FujiHaruka picture FujiHaruka  Â·  3Comments

phra picture phra  Â·  3Comments