DEV Community

Cover image for Easy node apps deployment with PM2
Hiram
Hiram

Posted on

Easy node apps deployment with PM2

This is the first post of a serie of helpful snippets to deploy node and react apps.

For this practice we will use pm2 which is a process manager for node.js

The following are all the considerations you need to take in order to be able to deploy with pm2:

The following is the actual code you will have to define in order to install the node app on your remote server, PM2 calls it ecosystem.config.js file:

module.exports = {
  apps : [],
  // Deployment Configuration
  deploy : {
    production : {
       "user" : "root",
       "host" : ["my-remote-server.xyz", "...",],
       "ref"  : "origin/master",
       "repo" : "git@github.com:username/repository.git",
       "path" : "/var/www/my-repository",
       "post-setup" : "npm install"
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

Once you defined this ecosystem file you should be able to run it using the following command:

pm2 deploy ecosystem.config.js production setup

The previous command will download the ref of your repo into the specified path, it will run as user on the specified host(s), and finally it will run the post-setup commands.

And that's it, now you can install your projects wherever you need. There exists a pre-setup command as well in case you need to make some actions before deployment. Here is the official docs

pm2 has many more features that we will discuss in next posts. Also, this script is not limited to node apps only, coming on examples will show you how to deploy a react app from deployment to serve, setting env parameters and building new releases after PR's using Github Actions, basically achieving zero downtime deployments pipelines.

Top comments (1)

Collapse
 
orville profile image
Orville Redenbacher

This is great! I've been using forever but I think it's time for me to switch to something more mature.