DEV Community

Cover image for Using Screen and PM2 for Deploying, Debugging, and Running NestJS in Production
Akshay Joshi
Akshay Joshi

Posted on

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!

Top comments (0)