DEV Community

Cover image for How to deploy with pm2 Javascript applications
Bogdan Alexandru Militaru
Bogdan Alexandru Militaru

Posted on • Originally published at boobo94.xyz on

How to deploy with pm2 Javascript applications

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 && npm install && 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 && npm install && pm2 reload ecosystem.json --env production" } }}
Enter fullscreen mode Exit fullscreen mode

If you want to know everything about how to deploy with pm2 process, read the official documentation.

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.

!!!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. 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, 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 appeared first on boobo94.

Top comments (0)