Running Nodejs applications in production forever using pm2

I’ve been running a few production Nodejs websites for a while now. I was using pm2 .

Some pm2 basics :

Firstly you need to install it globally using the “-g” flag via NPM:

npm install pm2 -g

You can then start apps using the start command. I found passing the “-n” flag followed by a nice name of the app helps to identify the app later:

pm2 start server.js -n "servername"

You can get a nice list output of apps using the list command:

pm2 list

You can also run your app in cluster mode which spools off multiple processes. If you are wanting to run 4 process you app can be started like:

pm2 start server.js -n servername -i 4

After a clustered app is started you can scale up or back using:

pm2 scale servername 2

You can then manage your apps by specifying the command followed by the “id” from the “pm2 list” command:

Restart

pm2 restart 0

Stop

pm2 stop 0

Delete

pm2 delete 0

I found the logging fantastic too. You can easily tail the logs to get any errors:

pm2 logs

Remember to run the following command to save the processes for when the server is rebooted:

pm2 save

Check this video for more information :

Leave a Reply

Your email address will not be published. Required fields are marked *

GiottoPress by Enrique Chavez