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.

 

 

Author: Labanish

I am a Senior Cloud Solutions Architect at Oracle. Am interested in Cloud Automation, Cloud Native Applications dev and Emerging Tech. I have over 6 years work experience handling different tech roles in East Africa, Romania & now in United Kingdom. All views are mine.

One thought on “Run Node.js Applications on Oracle Cloud Infrastructure using PM2”

  1. Funny, I have enabled in my security list ingress and egress for port 3000, firewall-cmd’ed the port 3000 as you and still I can’t access my hello world 😀

    netstat -tulpen | grep 3000
    tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 0 2096284 28208/node

    access from internet:
    curl https://dbgenie.tech:3000
    curl: (7) Failed to connect to dbgenie.tech port 3000: Connection refused

    In my VM:
    curl https://dbgenie.tech:3000
    curl: (7) Failed connect to dbgenie.tech:3000; Connection refused

    curl localhost:3000
    Hello World!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: