Logo

dev-resources.site

for different kinds of informations.

Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production

Published at
7/22/2024
Categories
webdev
nestjs
screen
pm2
Author
doozieakshay
Categories
4 categories in total
webdev
open
nestjs
open
screen
open
pm2
open
Author
12 person written this
doozieakshay
open
Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production

Deploying a NestJS application in a production environment involves ensuring stability, security, and performance. Two useful tools for managing your NestJS application are screen and PM2. screen allows you to manage long-running processes on a server, making it ideal for debugging, at the same time PM2 is a production-grade process manager that simplifies management, monitoring, and load balancing of Node.js applications. Here's how you can use both tools effectively.

Prerequisites

  1. NestJS Application: Ensure you have a NestJS application ready for deployment.
  2. Node.js and npm: Make sure Node.js and npm are installed on your server.
  3. Server Access: SSH access to your production server.

Step-by-Step Guide

1. SSH into Your Server

First, SSH into your production server where you want to deploy the NestJS application.

ssh your_user@your_server_ip
Enter fullscreen mode Exit fullscreen mode

2. Install Screen and PM2

If screen and PM2 are not already installed on your server, you can install them using the package manager for your server's operating system.

For Ubuntu/Debian:

sudo apt update
sudo apt install screen
sudo npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

For RHEL/Oracle Linux:

sudo yum install screen
sudo npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

Using Screen for Debugging

3. Create a New Screen Session

Create a new screen session where your NestJS application will run.

screen -S nestjs-app-debug
Enter fullscreen mode Exit fullscreen mode

This will create a new screen session named nestjs-app-debug and attach you to it.

4. Navigate to Your Application Directory

Navigate to the directory where your NestJS application is located.

cd /path/to/your/nestjs-app
Enter fullscreen mode Exit fullscreen mode

5. Install Dependencies

Ensure all dependencies are installed.

npm install
Enter fullscreen mode Exit fullscreen mode

6. Start the Application in Debug Mode

Start your NestJS application in debug mode to assist with development and debugging.

npm run start:debug
Enter fullscreen mode Exit fullscreen mode

7. Detach from the Screen Session

Detach from the screen session so the process continues running in the background.

Ctrl + A, then D
Enter fullscreen mode Exit fullscreen mode

8. Reattach to the Screen Session (if needed)

If you need to reattach to the screen session to check on your application, you can do so with:

screen -r nestjs-app-debug
Enter fullscreen mode Exit fullscreen mode

Using PM2 for Production

9. Build the Application

Build your NestJS application for production.

npm run build
Enter fullscreen mode Exit fullscreen mode

10. Start the Application with PM2

Start your NestJS application using PM2 for better process management and monitoring in production.

pm2 start dist/main.js --name nestjs-app
Enter fullscreen mode Exit fullscreen mode

11. Monitor and Manage with PM2

PM2 provides several commands to manage and monitor your application:

  • Check Application Status:
  pm2 status
Enter fullscreen mode Exit fullscreen mode
  • View Logs:
  pm2 logs nestjs-app
Enter fullscreen mode Exit fullscreen mode
  • Restart Application:
  pm2 restart nestjs-app
Enter fullscreen mode Exit fullscreen mode
  • Stop Application:
  pm2 stop nestjs-app
Enter fullscreen mode Exit fullscreen mode
  • Delete Application:
  pm2 delete nestjs-app
Enter fullscreen mode Exit fullscreen mode

12. Ensure Application Runs on Reboot

To ensure your application starts on server reboot, configure PM2 to run at startup.

pm2 startup
pm2 save
Enter fullscreen mode Exit fullscreen mode

This will generate a startup script that runs PM2 and your applications on server boot.

Conclusion

By combining screen and PM2, you can effectively manage the deployment, debugging, and running of your NestJS application in production. Use screen for debugging sessions where you need to interact with your application and leverage PM2 for robust, production-grade process management, ensuring your application is always running smoothly and can recover from failures. Both tools together provide a flexible and powerful setup for managing your NestJS application in different stages of its lifecycle.

Happy Nesting!

pm2 Article's
30 articles in total
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Guia de Comandos PM2
Favicon
🚀 Deploying Node.js Application with PM2, NGINX, and SSL Configuration 🚀
Favicon
Monitoring PM2 in production
Favicon
Mastering PM2: Optimizing Node.js and Next.js Applications for Performance and Scalability
Favicon
Setting Up PM2 for Multi-User Access on Ubuntu Instance
Favicon
Manual deployment of NestJS and Angular applications on a dedicated server via "Docker Compose" and "PM2"
Favicon
Build applications on NestJS and Angular and run them in two versions: via PM2 and via Docker Compose
Favicon
An example of a simple update of NestJS-mod libraries
Favicon
Using pm2 to Manage Node.js Applications
Favicon
Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production
Favicon
Host Multiple Node Apps with nginx, pm2 with SSL certificate
Favicon
Node.js PM2 Orchestration Explained
Favicon
Managing Logs with PM2 and pm2-logrotate
Favicon
Streamlining PM2 Startup for Node.js Applications: A Comprehensive Guide
Favicon
Managing Next.js and NestJS Applications in Production with PM2
Favicon
Deploy a Full Stack Web App to VPS Server with NGINX and PM2!
Favicon
Como utilizar o PM2 para gerenciar aplicações
Favicon
Guide to Running a Node Server in Nx Monorepo using PM2 Process Manager
Favicon
How to run old nodejs Project into new nodejs project
Favicon
CentOS 7 NodeJS Kurulumu
Favicon
How to start node.js app with pm2
Favicon
How to start node.js app with pm2
Favicon
Deploying Multiple NodeJS Servers on a Single DigitalOcean Droplet; Managed by PM2, Without Using an ecosystem.config.js file
Favicon
Automatic deploys with PM2, Caddy, and GitHub Actions
Favicon
Utilizando PM2 (Basico)
Favicon
Experience on PM2 Tricks to manage your NodeJs processes
Favicon
Deploy Nest JS App using PM2 on Linux (Ubuntu) Server
Favicon
Install PM2 (Process Manager 2)
Favicon
Generate server block (virtual hosts) for nginx dynamically

Featured ones: