Efficiently Manage your applications with PM2 in Background

·

4 min read

Introduction

If you have worked with any of the frontend or backend projects, you know that you need to keep your applications alive in the background. This can be achieved through several methods like PM2,systemd, Docker, Forever, and Supervisord.

In this article, we will talk about how PM2 efficiently manages your application in the background and helps streamline your workflow, and ensures the smooth running of applications.

What is PM2?

PM2 stands for process manager 2, it is a powerful and popular daemon process management tool that helps you manage your applications smoothly in your production.

It is written in Nodejs and built to manage nodeJs applications but can also be used for all the other applications. In 2016 it was ranked as the 86th most popular GitHub project.

Key Features of PM2:

  • Automatic process management

  • Process clustering for improved performance

  • Zero-downtime reload for seamless updates

  • Log management with rotation, streaming, and aggregation

  • Monitoring and health checks

  • Startup script generation

  • Environment management for different configurations

  • Integration with the Node.js ecosystem

  • Easy deployment and scaling

  • Extensibility with plugins and a vibrant plugin ecosystem.

How To Install PM2 in EC2?

Step 1: Create EC2 instance by logging into your IAM or Root AWS account

Step 2: SSH into your Virtual Machine using the pem file that you have downloaded after creating an EC2 instance. You can get the SSH login command on the AWS console.

  1. Select your EC2 instance and on top you can see the Connect button.

2. Once you check the box of instance, the connect button will be enabled and it will navigate you towards the login command.

Step 3: After successfully logging-in to your VM, install PM2 globally to get started.

sudo apt update
sudo apt install nodejs
npm install pm2 -g

#1 Run your Frontend Application using PM2

Now that you have PM2 installed globally you can easily manage your frontend application using PM2.

Step 1: Clone your project into your VM

git clone “github-url-of-your-project”

Step 2: Navigate to your cloned project directory.

cd /my-app

Step 3: Install dependencies for your project (node modules)

npm install

Step 4: Now build your application

npm run build

This will build all your web pages in a production environment.

Step 5: Now comes the magic part , that is to run your application through PM2 which keeps it live in the background and manages your application.

pm2 start npm --name “your-app-name” -- start

You can name your project anything meaningful Ex: frontend-prod which helps you identify your project within PM2.

Step 6: To ensure that your frontend application and PM2 start automatically when the server reboots you can generate a startup script.

pm2 startup

Boom! Now Check if your application is running with PM2 by executing the command

pm2 list

Note: If it doesn’t show up make sure you have allowed ports in your security groups for your frontend application.

Now You can access the web page with your publicly assigned Ip address along with your port number.

#2 Run your Backend Application using PM2

Step 1: If you have your Backend written in Java then package your application into a Jar file

mvn clean install

Step 2: Upload your Jar file into your AWS S3 Bucket and make the object public so that you can access it.

Step 3: Navigate to the directory where you want to download your jar file from S3.

wget “url-of-your-jar-file”

Step 4: Create a script file to execute the Jar file

java -jar your-jar-file.jar

This creates a startup script, save this script with .sh extension.

Step 5: Make the startup script executable

chmod +x start.sh

Step 6: Run your backend application

pm2 start start.sh --name “your-app-name”

You can name your project anything meaningful Ex: Backend-Prod which helps you identify your project within PM2.

Step 7: To ensure that your frontend application and PM2 starts automatically when the server reboots you can generate a startup script.

pm2 startup

Now Check if your application is running with PM2 by executing the command

pm2 list

Note*: If it doesn’t show up make sure you have allowed ports in your security groups for your Backend application.*

You can access the APIs with your publicly assigned Ip address along with your port number.

Conclusion:

PM2 is a powerful process management tool that simplifies the management of background applications. From application startup and monitoring to automatic restart, scaling, and log management, PM2 provides a comprehensive set of features to increase productivity and ensure smooth application performance. By using PM2, developers and system administrators can save time, optimize resources and focus on developing great applications. Try PM2 and take control of your application management workflow.