Run Node.js Applications on Oracle Cloud Infrastructure using PM2

Prerequisites:

What is PM2?

Pm2 is a production process manager for Node.js applications with built-in load balancer. It allows you to keep you applications alive forever, reloads them at zero downtime. It’s simple to use and makes managing a production environment seamless.

Starting a node app in pm2 is as easy as;

$ pm2 start app.js

Installing PM2 on Oracle Compute;

First, you need to take care of your firewall and open the necessary ports.

Login to your compute instance on OCI through cmd and open the ports you want to use using the firewalld command.

First install firewalld (If not yet installed)

 sudo yum install firewalld

Next expose port you want to use ie.5001 to the public to allow in-bound web traffic via HTTP. (Adding it on the public zone)

sudo firewall-cmd --zone=public --add-port=5001/tcp --permanent

Reload the firewalld – you can even check the features enabled on the public zone.

sudo firewall-cmd --reload
sudo firewall-cmd --info-zone public

node1

Now that we have exposed port 5001 to the public, lets now install PM2. It’s a piece of cake! Run;

npm install pm2 -g

On your node application, specify the port you exposed.

node2

Run your app using pm2 start command (My node app is saved as app.js)

pm2 start app.js

node3

You can now test your Node.js app on your browser using the public IP address and the port you exposed;

 <your compute instance public IP address>: <port>

node4

It works! Note that you can run more than one Node.js application using PM2, just expose them in different ports.

Finally a few other PM2 commands and resources to keep you going.

pm2 ls — Show a list of all applications
pm2 stop <app> — Stops a specific application
pm2 start <app> — Starts a specific application
pm2 <app> scale N — Scales the application you specify to N number of instances (can be used to scale up or down)
pm2 kill — Kills all running applications
pm2 restart — Restarts all running applications
pm2 reload — Reloads the app configuration
pm2 monit -will return a rich set of data around your application’s health
pm2 logs — Outputs logs from all running applications
pm2 logs app — Outputs logs from only the app application
pm2 flush — Flushes all log data, freeing up disk space

Final thoughts;

Pm2 is an amazing open source project. Many thanks to everyone putting all the resources out there!

If you need any support feel free to reach out, or if you have any additional tips and tricks, feel free to share with me!

Here are additional resources and references:

 

Thank you and Happy coding! If you liked it, share!

This blog reflects  my own thoughts and doesn’t reflect the thoughts of my employer.