How to Deploy a NestJS Application with Nginx on Ubuntu VPS?

How to Deploy a NestJS Application with Nginx on Ubuntu VPS on Accuweb.Cloud?

NestJS is an innovative Node.js framework for building efficient, scalable, and maintainable server-facet packages. It makes use of TypeScript through default and is built on top of Express (or Fastify) to deal with HTTP requests. NestJS includes modern programming standards along with dependency injection, modular architecture, and interior designers, making it the best for building complex programs.

In this guide, we will walk through the steps of deploying a NestJS application on a VPS hosted on AccuWeb.Cloud, is a reliable cloud service provider. We will utilize Ubuntu as the operating system for the server, Nginx as a reverse proxy, and npm for managing and running the application. Nginx, a powerful and efficient web server, will be used to forward requests to the NestJS application running on a specific port, enhancing performance, load balancing, and security.
Deploy a NestJS Application with Nginx on Ubuntu VPS



Steps to Deploy a NestJS Application with Nginx on Ubuntu VPS

Step 1: Set Up Your VPS on AccuWeb.Cloud

1.1. Visit the AccuWeb.Cloud dashboard. Log in with your credentials, or sign up for a new account if you don’t have one.

1.2. Click on New Environment to begin the process.
New Environment1.3. Choose Ubuntu OS as your operating system.

1.4. Select the VPS type based on your resource requirements.

1.5. Set the desired resource allocation for CPU, RAM, and storage. You can scale your VPS later if needed.

1.6. Provide a name for the environment to easily identify it.

1.7. Once everything is configured, click Create to start provisioning your VPS.
Start provisioning your VPS1.8. AccuWeb.Cloud will begin provisioning your VPS with the selected configuration. After a few minutes, you’ll receive an email with your login credentials, public IP address, and other necessary details. Your VPS will also be visible on the dashboard.
Login credentials1.9. Once the environment is created, click Web SSH on the AccuWeb.Cloud dashboard to access your server.

Web SSH

Step 2: Prepare the Server

2.1. Once you’re logged in to your VPS, update the package list and upgrade the system:

sudo apt update && sudo apt upgrade -y

Update the package list2.2. Install the necessary development tools for building and running your application:

sudo apt install curl git build-essential -y


Install development tools

Step 3: Install Node.js and npm

3.1. To install the latest stable version of Node.js, add the NodeSource repository:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Save $100 in the next
5:00 minutes?

Register Here

Add NodeSource repository3.2. Now, install Node.js and npm (Node Package Manager):

sudo apt install nodejs -y

Install Node.js and npm3.3. To verify that Node.js and npm were successfully installed, check their versions:

node -v
npm -v


Verify Node.js and npm

Step 4: Install NestJS CLI and Create a New Project

4.1. Now, Install NestJS CLI Globally. You need the NestJS CLI to create and manage your project. Install it globally:

npm i -g @nestjs/cli

Install NestJS CLI Globally4.2. Now, create a new NestJS project by running:

nest new <project-name>

Create a new NestJS projectFollow the prompts to select a package manager (npm, yarn, or pnpm) and complete the project setup.

4.3. After the project is created, navigate into the project directory and run tests to ensure everything is working:

cd <project-name>
npm run test

Navigate into project directory

Step 5: Set Up Nginx as a Reverse Proxy

5.1. If Nginx is not already installed on your server, you can install it by running:

sudo apt install nginx -y

Install Nginx5.2. If you are using UFW (Uncomplicated Firewall), allow Nginx to handle HTTP and HTTPS traffic:

sudo ufw enable
sudo ufw allow 'Nginx Full'

Save $100 in the next
5:00 minutes?

Register Here

5.3. Now, configure Nginx to forward requests to your NestJS application. First, create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/your_domain

Add the following configuration in the file, replacing your_domain with your application name or domain:

server {
listen 80;
server_name example.com www.example.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;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}

5.4. Create a symbolic link to enable the configuration for your domain:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

5.5. To avoid conflicts with the default site configuration, unlink it:

sudo unlink /etc/nginx/sites-enabled/default

5.6. Finally, restart Nginx to apply the changes:

sudo systemctl restart nginx


Restart Nginx

Step 6: Run the NestJS Application with npm

6.1. To run your NestJS application, navigate to your project directory and start the application using npm:

cd <project-name>
npm start

Run NestJS applicationThis will start the application on http://localhost:3000 by default.

6.2. If you want to run the application in the background, you can use the nohup command:

nohup npm run start &

This will run the application in the background, and you can safely log out of your SSH session without stopping the application.

Logout of SSH

Step 7: Test the Web Application

7.1. Test the Application Locally
You can test the application from the server by running:

curl http://localhost

This should return the response from your NestJS application, such as “Hello World!”
Test application locally7.2. To test the application from an external browser or device, navigate to the server’s public IP address or domain name:
http://your_domain_or_IP

Navigate to the server's public IP addressYou should see the output of your NestJS application, such as “Hello World!” displayed in your browser.

Conclusion

Deploying a NestJS application on an Ubuntu VPS with Nginx as a reverse proxy on AccuWeb.Cloud provides a reliable and efficient solution for hosting backend applications in a production environment. By following the steps outlined in this guide, you have successfully set up a scalable and secure web application that can handle traffic effectively. This setup will ensure that your NestJS application runs securely and efficiently, with Nginx handling traffic and optimizing performance. If you need to scale your app in the future, you can easily adjust your server resources in AccuWeb.Cloud dashboard.

Save $100 in the next
5:00 minutes?

Register Here