/

How to Use pm2 to Serve a Node.js App

How to Use pm2 to Serve a Node.js App

In this tutorial, we will explore how to use pm2, a powerful process management tool for Linux, to run and manage a Node.js app on a DigitalOcean VPS. Additionally, we will set up pm2 to automatically update and restart the app whenever changes are pushed to its corresponding GitHub repository.

To begin, make sure you have signed up for a DigitalOcean account and created a VPS using the NodeJS image, which comes pre-installed with pm2 and Node.js. Once your VPS is up and running, log in as the “nodejs” user.

The sample Node.js app is located in the “/var/www/html/“ folder and consists of the “hello.js” file. To view a list of all running applications, use the command “pm2 list”. To restart the application, use “pm2 restart hello”. Similarly, “pm2 stop hello” will stop the application, while “pm2 start hello” will bring it back up.

One of the advantages of pm2 is that it automatically restarts applications when the system is restarted.

Now, let’s deploy a different application. Stop the current sample app using “pm2 stop hello”. Create a “test” folder in the “/var/www/html” directory and navigate into it. Run “npm init -y” to initialize a new Node.js project, and then install Express using “npm install express”.

Next, create a file named “app.js” and populate it with the following code:

1
2
3
4
5
const express = require("express")
const app = express()

app.get("/", (req, res) => res.send("Hello World!"))
app.listen(3000, () => console.log("Server ready"))

To start the application, use the command “pm2 start app.js –name app”. You should see the app running. By default, it will use port 3000. If you need to use a different port, you will need to modify the “/etc/nginx/sites-available/default” file to map the new URL to your app.

Now, let’s explore how to deploy an app from GitHub. First, stop the previously built app using “pm2 stop app”. Create a sample GitHub app with similar functionality but a different message. Create a folder locally, run “npm init -y” and install Express. Create a file named “app.js” and add the following code:

1
2
3
4
5
const express = require("express")
const app = express()

app.get("/", (req, res) => res.send("Hello World, deployed from GitHub!"))
app.listen(3000, () => console.log("Server ready"))

Push this app to GitHub. To clone the private repository on the server, go to the “/var/www/html” folder and run “git clone [repository URL]”.

Once the app files are cloned, navigate to the app’s directory and run “npm install” to install the necessary dependencies.

To start the new app, stop the previous app using “pm2 stop test” and then use “pm2 start app.js –name testdo”.

Next, install the “pm2-githook” module using “pm2 install pm2-githook”. Configure the app using the following command, ensuring correct escaping of double quotes: “pm2 set pm2-githook:apps “{"testdo":{"secret":"test123","prehook":"npm install –production","posthook":"echo done"}}””. Make sure to choose a secure value for the “secret” option.

By default, the module listens on port 8888 for GitHub webhooks. Open this port using “sudo ufw allow 8888”. Go to the repository settings on GitHub and add a new webhook, providing the URL of your domain with port 8888. Remember to use the same “secret” value as before.

With everything set up, any changes pushed to the GitHub repository will now automatically trigger a deployment of your app.

That’s it! You now know how to use pm2 to run and manage a Node.js app on a DigitalOcean VPS, and how to leverage GitHub webhooks for automatic updates and restarts.

tags: [“pm2”, “Node.js”, “DigitalOcean”, “GitHub”, “Linux”, “process management”, “webhooks”, “deployment”]