NodeJS Deployment Using PM2 on Zhap Cloud

A walkthrough detailing deploying a NodeJS application on Zhap Cloud using PM2

ยท

2 min read

NodeJS Deployment Using PM2

Prerequisites

This walkthrough assumes you have the following:

  • Zhap Cloud account (obviously)
  • Knowledge of building apps in NodeJS
  • A git repository containing your codebase
  • A domain name pointed at Zhap Cloud

Step 1 - Clone Project

Zhap Cloud App service comes preinstalled with git.

Head on to https://zhap.cloud/app/machine and launch the app terminal

terminal-launch.png

In the /usr/src/app directory run a git clone _REPOURL. Ensure all files and operations are done in the /usr/src/app directory and its children.

root@a15f257eec:/usr/src/app$ git clone https://github.com/chinaza/dummy-app

Step 2 - Setup Project

With your project cloned, cd into the project directory and install npm dependencies/ packages and run any build steps if exists.

As an example:

root@a15f257eec:/usr/src/app$ cd dummy-app

root@a15f257eec:/usr/src/app$ npm install

Once the dependencies are installed, you can proceed to the next step.

Step 3 - Start Application

To enable your app being accessible ensure it runs dynamically on the PORT environment variable. To manually get the assigned port for your app service environment, run this command on your terminal:

root@a15f257eec:/usr/src/app$ echo $PORT

Now all is set! Let's start our application using PM2.

root@a15f257eec:/usr/src/app$ pm2 start --name="DUMMY-APP" npm -- start

For a better understanding on PM2 and its commands, visit https://pm2.keymetrics.io/docs/usage/quick-start/

Some Useful PM2 Commands

root@a15f257eec:/usr/src/app$ pm2 list

Get a list of currently running applications.

root@a15f257eec:/usr/src/app$ pm2 stop APP_NAME_OR_ID

Use this command to stop your application. You must specify the app name or id gotten from pm2 list above.

root@a15f257eec:/usr/src/app$ pm2 restart APP_NAME_OR_ID

Use this command to restart your application. You must specify the app name or id gotten from pm2 list above.

root@a15f257eec:/usr/src/app$ pm2 logs

Get logs for your application with the logs command.

root@a15f257eec:/usr/src/app$ pm2 flush

With flush, you can clear the logs collected from your application.

Congratulations! Your app is now fully setup and deployed on Zhap Cloud!

Get managed hosting and database for your NodeJS, Python, Go applications and more. SQL and NoSQL databases all included under one plan. Start for free today! https://zhap.cloud

Did you find this article valuable?

Support Chinaza Egbo by becoming a sponsor. Any amount is appreciated!