In this tutorial, we'll explain how to host NestJS Application with Nginx on Ubuntu 24.04 server.
NestJS is a popular framework for building efficient and scalable Node.js server-side applications. NestJS is a progressive Node.js framework designed for building efficient and scalable server-side applications. It leverages TypeScript, which is a statically typed superset of JavaScript, to provide a strongly typed environment that enhances developer productivity and code maintainability.
NestJS is built around the concepts of modular architecture and Dependency Injection, which are inspired by Angular, making it familiar to developers who have worked with Angular. One of the core strengths of NestJS is its flexibility. It is platform-agnostic, meaning it can be used to build various types of applications, from microservices to GraphQL APIs and RESTful applications. The framework is designed to be versatile, allowing developers to use any external libraries and tools, such as Express or Fastify, as the underlying HTTP server.
In this tutorial, we'll walk through the steps to deploy a NestJS application on an Ubuntu VPS using Nginx as a reverse proxy.
Prerequisites
Before we begin, ensure you have the following:
- A server running Ubuntu 24.04 dedicated server or KVM VPS.
- NestJS Application: A ready-to-deploy NestJS application.
- Domain Name: recommended for production.
Host NestJS Application with Nginx on Ubuntu
Step 1: Update Your System
First, update your system packages to ensure everything is up to date:
sudo apt update && sudo apt upgrade -y
Step 2: Install Node.js and npm
For latest version, visit the Node.js official documentation page.
# installs nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
# download and install Node.js (you may need to restart the terminal)
nvm install 20
Verify the installation:
# verifies the right Node.js version is in the environment
node -v # should print `v20.17.0`
# verifies the right npm version is in the environment
npm -v # should print `10.8.2`
Step 3: Deploy NestJS Application
There are two ways to deploy it. First clone your Git repository or create a NestJS application in the server. We have covered both ways.
Clone Git Repository
Next, you'll need to transfer your NestJS application to your VPS. If your project is hosted on GitHub, you can clone it directly:
git clone https://github.com/yourusername/your-nestjs-app.git
Navigate into your project directory:
cd your-nestjs-app
Install Dependencies and Build the Application. Inside your project directory, install the necessary dependencies:
npm install
Build your NestJS application:
npm run build
Create a NestJS Application
Let's proceed with the installation.
The NestJS CLI is a command-line interface that helps in quickly setting up a new project and performing various development tasks.
Install the CLI globally using npm:
npm install -g @nestjs/cli
Create a New NestJS Project. Once the CLI is installed, you can create a new NestJS project by running:
nest new my-nestjs-app
Replace my-nestjs-app
with your desired project name.
The CLI will prompt you to choose the package manager to use (npm or yarn
). Select your preferred option, and the CLI will automatically install the necessary dependencies.
Navigate to the Project Directory. After the project is created, navigate into the project directory:
cd my-nestjs-app
Explore the Project Structure
Your new NestJS application will have the following basic structure:
- src/: Contains the main application code.
- app.controller.ts: Contains a simple controller to handle HTTP requests.
- app.module.ts: The root module of the application.
- app.service.ts: A basic service that can be injected into controllers.
- main.ts: The entry point of the application.
- test/: Contains test files for the application.
- node_modules/: Contains all the installed dependencies.
- nest-cli.json: Configuration file for the NestJS CLI.
- package.json: Contains project metadata and scripts.
To build the application for production, use:
npm run build
This will compile the TypeScript files into JavaScript files inside the dist/
directory.
Step 4: Set Up PM2 to Manage Your Application
PM2 is a process manager for Node.js applications that helps keep your app running and restart it automatically if it crashes.
Install PM2 globally:
sudo npm install -g pm2
Start your NestJS application with PM2:
Note: dist/main.js
file must be exist in the project directory. You will get it after you build the project by executing npm run build
command.
pm2 start dist/main.js --name your-nestjs-app
To ensure your application restarts on server reboots, use:
pm2 startup
pm2 save
Check the status
pm2 status
Output:
┌────┬────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├────┼────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ your-nestjs-app │ default │ 0.0.1 │ fork │ 2521 │ 0s │ 0 │ online │ 0% │ 17.8mb │ root │ disabled │
└────┴────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Step 5: Configure Firewall
If you have enabled UFW firewall, follow this steps. We need to add HTTP and HTTPS ports in the firewall. Execute following set of commands:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
Step 6: Install and Configure Nginx
Install Nginx:
sudo apt install nginx -y
Next, configure Nginx as a reverse proxy to forward requests to your NestJS application.
Create a new configuration file for your application:
sudo nano /etc/nginx/sites-available/your-nestjs-app
Add the following configuration:
server {
listen 80;
server_name yourdomain.com; # Replace with your domain name or IP
location / {
proxy_pass http://localhost:3000; # Your NestJS app port
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;
}
}
Enable the configuration by creating a symbolic link to sites-enabled:
sudo ln -s /etc/nginx/sites-available/your-nestjs-app /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 7: Secure Your Application with SSL
For a production environment, it is highly recommended to secure your application using SSL. You can obtain a free SSL certificate using Let's Encrypt.
Install Certbot, the Let's Encrypt client:
sudo apt install certbot python3-certbot-nginx -y
Obtain and install the SSL certificate:
sudo certbot --nginx -d yourdomain.com
Follow the prompts to complete the installation. Certbot will automatically configure Nginx to use the SSL certificate.
Step 8: Access Your Application
Your NestJS application should now be accessible through your domain name or IP address. If you secured your application with SSL, access it via https://yourdomain.com
.
Conclusion
You have successfully seen how to host NestJS Application with Nginx on Ubuntu server. With PM2 managing your application, it should stay running smoothly, even after server reboots. For production environments, ensure to keep your server and dependencies updated regularly.