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 REPO_URL. 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!