How to deploy with pm2 Javascript applications
Tags: devops, architecture, javascript, programming
Published: 2020-02-14 09:40:36

---
title: How to deploy with pm2 Javascript applications
published: true
date: 2020-02-14 01:09:00 UTC
tags: DevOps, architecture, javascript, programming
canonical_url: https://boobo94.xyz/devops/deploy-with-pm2/?utm_source=rss&utm_medium=rss&utm_campaign=deploy-with-pm2
cover_image: https://i1.wp.com/boobo94.xyz/wp-content/uploads/2020/02/deploy-with-pm2.jpg?resize=1920%2C884&ssl=1
---

Today I talk with you about how to deploy with pm2. I want to present a simple example for a node.js app, but you can use it to deploy react, Vue or other Javascript apps.

I know how frustrating is for some of you this part about devops. That’s why pm2 comes throw us like an angel which wants to save the humanity in front of Continous Deployment process.

## Pm2 configuration file

```
{ "apps": [{ "name": "App", "script": "./app.js", "autorestart": true, "watch": false, "max\_memory\_restart": "1G", "env": { "NODE\_ENV": "development", }, "env\_production": { "NODE\_ENV": "production", } }], "deploy": { "development": { "user": "user", "host": "111.111.111.111", "ref": "origin/develop", "repo": "git clone git@github.com:repo.git", "path": "/var/www/development", "post-deploy": "cd /var/www/development &amp;&amp; npm install &amp;&amp; pm2 reload ecosystem.json" }, "production": { "user": "user", "host": "222.222.222.222", "ref": "origin/develop", "repo": "git clone git@github.com:repo.git", "path": "/var/www/production", "post-deploy": "cd /var/www/production &amp;&amp; npm install &amp;&amp; pm2 reload ecosystem.json --env production" } }}
```

If you want to know everything about how to deploy with pm2 process, read the [official documentation](https://pm2.keymetrics.io/docs/usage/deployment/).

Firstly, please add the previous configuration into your **ecosystem.json** file. Please make sure to fill in the details under deploy property for environment that you are interested in. You must change **host** with your own IP, **repo** is the ssh git remote url of your repository, **path** is the folder full path for the folder where your app is stored, and please make sure that your update the path in **post-deploy** as well.

## How to setup ssh key

Before to move forward with the deploy process, you need one more step. You have to create a ssh key which marks your computer as trusted on the hosting server. If you don’t know how to do it, I invite to my post about how to [Generate a new SSH key and add it to the ssh-agent](https://boobo94.xyz/tutorials/generate-ssh-key/).

_!!!The next commands are executed from your local machine, not on your remote hosting server._

After this configuration run **pm2 deploy development setup** for the first time<mark id="annotation-text-9e524070-7604-4483-b091-c636b49e5252"></mark>. After that, each time when you want to deploy with pm2, execute **pm2 deploy development**.

You can take a look at my previous post about [How to deploy Node.js app with PM2 in production](https://boobo94.xyz/devops/deploy-nodejs-app-with-pm2/), where I present how to run a Typescript project in production using pm2 with ts-node plugin.

The post [How to deploy with pm2 Javascript applications](https://boobo94.xyz/devops/deploy-with-pm2/) appeared first on [boobo94](https://boobo94.xyz).