How to Deploy NestJS on Your VPS in Production Mode Using PM2 and Nginx

Deploying a NestJS application in a production environment involves several steps to ensure it runs efficiently, is highly available, and is secure. Using PM2 and Nginx together provides a robust solution for managing and serving your NestJS application. Here’s a step-by-step guide to deploy your NestJS application on a VPS using PM2 and Nginx.

PM2 and Nginx

What is PM2?

PM2 is a widely-used process manager for Node.js applications. It offers a range of features to manage, monitor, and keep Node.js applications running smoothly. PM2 helps with automatic restarts if the application crashes, efficiently utilizes CPU cores, performs zero-downtime deployments, and provides valuable monitoring tools, making it ideal for production environments.

What is Nginx?

Nginx (pronounced “Engine-X”) is a high-performance, open-source web server and reverse proxy server. It is known for its ability to handle high traffic volumes and numerous concurrent connections efficiently. Nginx is used for serving static content, load balancing, caching, and acting as an API gateway, making it a powerful tool to enhance the performance and security of your application.

Why Choose PM2 and Nginx for NestJS Deployment?

Using PM2 and Nginx together enhances the deployment of your NestJS application by ensuring it is reliable, scalable, and efficient. PM2 manages the application processes, restarts them automatically if needed, and allows zero-downtime updates. Nginx serves as a reverse proxy, managing incoming traffic, handling SSL termination, and improving security. It also balances the load across multiple application instances and serves static files efficiently.

Steps for Deploying on VPS

  1. Build the Application for Production
    • Run the following command to create an optimized production build of your NestJS application:
      npm run build
      
    • This command minifies assets and prepares your application for production deployment.
  2. Install PM2
    • If PM2 is not already installed, install it globally using:
      npm install pm2 -g
  3. Start the Production Server
    • Start your NestJS application using PM2. By default, it will run on port 3000:
      pm2 start npm --name "nestjs-app" -- start
    • The –name argument assigns a name to your application, making it easier to manage. For instance, you can stop the application using:
      pm2 stop nestjs-app
  4. Save the PM2 Configuration
    • To ensure your application restarts automatically after a server reboot, save the PM2 configuration:
      pm2 save
  5. Configure Nginx as a Reverse Proxy
    • Create a new Nginx configuration file for your NestJS application:
      sudo nano /etc/nginx/sites-available/nestjs-app.conf
    • Add the following configuration to the file:
      server {
          listen 80;
          server_name yourdomain.com;
          location / {
              proxy_pass http://localhost:3000;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              proxy_set_header Host $host;
              proxy_cache_bypass $http_upgrade;
          }
      }
    • Replace yourdomain.com with your actual domain name or IP address.
  6. Enable the Nginx Configuration
    • Create a symbolic link to enable the new Nginx configuration:
      sudo ln -s /etc/nginx/sites-available/nestjs-app.conf /etc/nginx/sites-enabled/
  7. Restart Nginx
    • Apply the new configuration by restarting Nginx:
      sudo service nginx restart

By following these steps, you will have successfully deployed your NestJS application on a VPS using PM2 and Nginx. PM2 ensures that your application remains running smoothly and efficiently, while Nginx manages incoming traffic, handles SSL, and provides enhanced security and load balancing. This combination allows your application to perform well under high traffic and remain resilient in a production environment.

Reach Out to me!

DISCUSS A PROJECT OR JUST WANT TO SAY HI? MY INBOX IS OPEN FOR ALL