DEV Community

Olashina
Olashina

Posted on

How to Deploy a NestJS Application to a VPS Server: A Complete Guide.

This guide documents the process of deploying a NestJS application to a VPS server for the first time, after having worked extensively with platforms like Docker, DigitalOcean, Heroku, AWS (EC2), and Google Cloud Service. It serves as a detailed reference for those transitioning from managed platforms to the more hands-on approach of VPS deployment.

Deploying a NestJS application to a VPS involves setting up the server, transferring your application, configuring Nginx as a reverse proxy, managing the application with PM2, and securing it with HTTPS via Let's Encrypt. This article explains each step and the reasoning behind it, making it a useful resource for both newcomers and those needing a refresher.

Prerequisites

  • A NestJS application on your local machine.
  • A VPS server (in this case, Contabo VPS).
  • A registered domain (e.g., from Namecheap).
  • SSH access to the VPS server.
  • A Linux distribution (or your preferred OS) installed on the VPS.

1. Setting Up the VPS and SSH Access

Before deploying your app, you need secure access to the VPS server. SSH is the standard protocol to remotely connect to servers.

Steps Taken:

  • Access the VPS via SSH: First, ensure that SSH is enabled and configured correctly on your VPS. If you face issues like "Permission denied", ensure you are using the correct username and password, and that SSH keys (if configured) are set up correctly.

Command:

   ssh root@<your-vps-ip>
Enter fullscreen mode Exit fullscreen mode
  • Using VNC as an Alternative: If SSH fails, some VPS providers (like Contabo) offer a web-based or VNC console to access the server directly. This helps resolve initial connection issues.

Tools:

  • RealVNC was used in this case to access the server when SSH wasn’t working.

2. Transferring the NestJS Application to the VPS

You need to transfer your NestJS project files from your local machine to the VPS server so they can be run and served to users.

Steps Taken:

  • Using Git or SCP to Transfer Files:

    • If Git is installed on your server, you can clone your project directly from your repository:
     git clone https://github.com/your-repo/your-nestjs-app.git
    
    • Alternatively, you can use scp to securely copy files from your local machine to the VPS:
     scp -r /path-to-your-project root@your-server-ip:/var/www/your-app
    

3. Installing Dependencies and Building the Project

Your NestJS app likely has dependencies that need to be installed before it can run on the server. Building the project ensures that all necessary files (such as compiled JavaScript) are ready to be served.

Steps Taken:

  • Navigate to the Project Directory:
   cd /var/www/your-app
Enter fullscreen mode Exit fullscreen mode
  • Install Dependencies: Install all required dependencies from the package.json file:
   npm install
Enter fullscreen mode Exit fullscreen mode
  • Build the Application: Build the app for production, generating the dist/ folder containing the compiled code:
   npm run build
Enter fullscreen mode Exit fullscreen mode

4. Running the Application with PM2

PM2 is a process manager that allows you to keep your NestJS app running in the background and automatically restart it if it crashes or after deployments.

Steps Taken:

  • Install PM2: If PM2 is not already installed, install it globally:
   npm install -g pm2
Enter fullscreen mode Exit fullscreen mode
  • Start the Application: Start your NestJS app using PM2:
   pm2 start dist/main.js --name your-app-name
Enter fullscreen mode Exit fullscreen mode
  • Set Up Auto-Restart: Configure PM2 to automatically start your application on server reboots:
   pm2 startup
   pm2 save
Enter fullscreen mode Exit fullscreen mode

5. Configuring Nginx as a Reverse Proxy

Nginx acts as a reverse proxy to forward incoming traffic from port 80 (HTTP) or 443 (HTTPS) to your NestJS application, which usually runs on an internal port like 3000. This setup is necessary to route web traffic properly.

Steps Taken:

  • Install Nginx: If Nginx isn’t installed, install it using:
   sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  • Configure Nginx: Create a new Nginx configuration file for your app:
   sudo nano /etc/nginx/sites-available/your-app
Enter fullscreen mode Exit fullscreen mode

Add the following configuration to proxy traffic to your NestJS app running on port 3000:

   server {
       listen 80;
       server_name yourdomain.com www.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;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  • Enable the Configuration: Create a symbolic link to enable the site:
   sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/
   sudo nginx -t
   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

6. Configuring DNS on Namecheap

To access your app via a custom domain, you need to configure DNS records on your domain provider (e.g., Namecheap) to point your domain to the VPS’s IP address.

Steps Taken:

  • Add an A Record:

    • Log in to Namecheap and go to Domain List > Manage for your domain.
    • Under the Advanced DNS tab, add an A Record:
      • Host: @
      • Value: <your-vps-ip>
  • Add a Subdomain:
    If you’re also using a subdomain (like www.yourdomain.com), add another A Record:

    • Host: www
    • Value: <your-vps-ip>

7. Securing Your App with HTTPS (Let's Encrypt)

Securing your app with HTTPS ensures that traffic between the client and the server is encrypted, adding an extra layer of security.

Steps Taken:

  • Install Certbot: Install Certbot, a tool used to generate SSL certificates from Let’s Encrypt:
   sudo apt install certbot python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode
  • Generate an SSL Certificate: Use Certbot to obtain an SSL certificate for your domain:
   sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Enter fullscreen mode Exit fullscreen mode

Certbot automatically configures Nginx to use the SSL certificate.

  • Renew Certificates Automatically: Set up a cron job to auto-renew the certificates:
   sudo crontab -e
Enter fullscreen mode Exit fullscreen mode

Add the following line to renew the certificates automatically:

   0 0 * * * /usr/bin/certbot renew --quiet
Enter fullscreen mode Exit fullscreen mode

By following these steps, you’ve successfully deployed a NestJS application to a VPS, configured Nginx as a reverse proxy, secured your app with HTTPS, and ensured it remains running with PM2. This guide not only helps you understand the deployment process but also serves as a reference for future VPS projects.

Top comments (0)