Image for post
Image for post
Photo by Joshua Aragon on Unsplash

PM2: Production-Ready Nodejs Applications in Minutes

Amir Ghorbanian
Jan 30 · 6 min read

Contents

What is PM2?

Installation

Example Application Setup

const express = require('express');const app = express();const PORT = process.env.PORTapp.get('/',(req,res) => {
res.send("Hello World!\n");
})
app.listen(PORT,() => {
console.log(`server is running on port ${PORT}`);
})

Application Management

module.exports = {
apps : [{
script: 'index.js',
watch: '.'
}, {
script: './service-worker/',
watch: ['./service-worker']
}],
deploy : {
production : {
user : 'SSH_USERNAME',
host : 'SSH_HOSTMACHINE',
ref : 'origin/master',
repo : 'GIT_REPOSITORY',
path : 'DESTINATION_PATH',
'pre-deploy-local': '',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
'pre-setup': ''
}
}
};
module.exports = {
apps : [{
script: 'index.js',
watch: '.',
// add the port as an environment variable
env: {
PORT: 8080
}
...
$ pm2 start ecosystem.config.js
$ curl localhost:8080
Hello World!

Why Use PM2

Image for post
Image for post
PM2 Github Repository

Use Cases

PM2 in Development

PM2 Environment Variables

module.exports = {
apps : [{
script: 'index.js',
watch: '.',
// add development environment variables
env_development: {
PORT: 8080
},
// add production environment variables
env_production: {
PORT: 8081
}
...

PM2 Error Logs

PM2 in Production

What is Blue-Green Deployment?

Image for post
Image for post
Blue-Green Deployment Model Diagram

How can Blue-Green Deployment be Implemented Using PM2?

...app.get('/',(req,res) => {
res.send("Hello World, I'm the green application!!\n");
})
...
module.exports = {
apps : [{
// new modifier
name: 'green',
...

Conclusion:

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Amir Ghorbanian

Written by

Software engineer and FOSS enthusiast | LinkedIn: https://www.linkedin.com/in/amir-ghorbanian

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Amir Ghorbanian

Written by

Software engineer and FOSS enthusiast | LinkedIn: https://www.linkedin.com/in/amir-ghorbanian

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store