# NodeJS Deployment Using PM2 on Zhap Cloud

# 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](https://zhap.cloud/app/machine) and launch the app terminal


![terminal-launch.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1640382134223/XYXkMA7bB.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._

```bash
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:

```bash
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:

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

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

```bash
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/](https://pm2.keymetrics.io/docs/usage/quick-start/)

### Some Useful PM2 Commands

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

Get a list of currently running applications.

```bash
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.

```bash
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.

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

Get logs for your application with the logs command.

```bash
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](https://zhap.cloud)

