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
- NestJS Application: Ensure you have a NestJS application ready for deployment.
- Node.js and npm: Make sure Node.js and npm are installed on your server.
- 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
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
For RHEL/Oracle Linux:
sudo yum install screen
sudo npm install -g pm2
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
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
5. Install Dependencies
Ensure all dependencies are installed.
npm install
6. Start the Application in Debug Mode
Start your NestJS application in debug mode to assist with development and debugging.
npm run start:debug
7. Detach from the Screen Session
Detach from the screen session so the process continues running in the background.
Ctrl + A, then D
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
Using PM2 for Production
9. Build the Application
Build your NestJS application for production.
npm run build
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
11. Monitor and Manage with PM2
PM2 provides several commands to manage and monitor your application:
- Check Application Status:
pm2 status
- View Logs:
pm2 logs nestjs-app
- Restart Application:
pm2 restart nestjs-app
- Stop Application:
pm2 stop nestjs-app
- Delete Application:
pm2 delete nestjs-app
12. Ensure Application Runs on Reboot
To ensure your application starts on server reboot, configure PM2 to run at startup.
pm2 startup
pm2 save
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)