Introduction
Modern web applications rely on solid, secure support. Therefore, it is essential to create applications that are scalable, secure, and architecturally complex to be managed by small and/or large teams of developers.
Modern developers prefer to use JavaScript on both the front-end and back-end. Express.js is a great JavaScript framework used by most developers. However, its minimal architecture makes it unsuitable for scalability and maintenance by large teams. This is where Nest.js comes in. Nest.js has a built-in architecture that makes it very suitable for scalability and deployment. Also, its native support for TypeScript makes it more suitable for developers than vanilla JavaScript.
In this tutorial, you will learn how to deploy a NestJS application using the Nginx web server on a VPS. You will learn how to securely deploy your application to the web.
Prerequisites
- An Ubuntu 20.04+ VPS or a physical Ubuntu machine
- Node.js and the npm (or yarn) package manager.
- Nginx web server
Step 1 – Preparing and deploying a NestJS application
In this section, you will install the NestJS CLI and create a basic NestJS application, which you will learn how to deploy using Nginx in the following sections.
Install NestJS CLI globally
To install NestJS CLI on your Ubuntu machine, open a terminal and type the following command.
npm i -g @nestjs/cliThis will install the NestJS command line interface on your machine. Next, you will learn how to create a new NestJS project.
Create a new NestJS project
NestJS now offers two ways to start a new project. You can choose the best method that suits you.
To create a NestJS project with the CLI, type the following command.
nest new <project-name>Output? Which package manager would you ❤️ to use? (Use arrow keys)
❯ npm
yarn
pnpmOnce completed, you will receive an output like the following.
OutputCREATE node_app/.eslintrc.js (663 bytes)
CREATE node_app/.prettierrc (51 bytes)
CREATE node_app/README.md (3340 bytes)
CREATE node_app/nest-cli.json (171 bytes)
CREATE node_app/package.json (1947 bytes)
CREATE node_app/tsconfig.build.json (97 bytes)
CREATE node_app/tsconfig.json (546 bytes)
CREATE node_app/src/app.controller.ts (274 bytes)
CREATE node_app/src/app.module.ts (249 bytes)
CREATE node_app/src/app.service.ts (142 bytes)
CREATE node_app/src/main.ts (208 bytes)
CREATE node_app/src/app.controller.spec.ts (617 bytes)
CREATE node_app/test/jest-e2e.json (183 bytes)
CREATE node_app/test/app.e2e-spec.ts (630 bytes)This will create a new project in the current working directory. You can use instead ; Provide the absolute path to another directory as well.
Starter template simulation
NestJS provides an alternative way to start a new project. It is a git repository that acts as a boilerplate template. You can clone that repository and start the project with the following commands.
git clone https://github.com/nestjs/typescript-starter.git <project-directory >After the cloning is complete, you need to cd into the project directory and then run npm install to install the dependencies from package.json.
cd <project-directory>
npm installOnce the project is ready, you can start the application server using the following command:
npm run startThis will run the application at http://localhost:3000. Now, you have a basic NestJS application ready to run on localhost.
Application testing
Once you have developed your application, you can run tests on it to check if it is performing as expected. NestJS provides default Jest tests that run tests on your application. You can start testing using the following command:
npm run testThis program will test you and show results similar to the following:
Output> [email protected] test
> jest
PASS src/app.controller.spec.ts
AppController
root
✓ should return "Hello World!" (24 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.895 s
Ran all test suites.In the following section, you will learn how to deploy this NestJS application using Nginx as a reverse proxy on your web server.
Step 2 – Configure Nginx to serve the NestJS application
Now, we can move on to setting up a web server to host this NestJS application. We will use the reverse proxy approach. In this approach, we run our application on localhost on a specific port and then use the Nginx server to proxy any requests to the VPS public IP address or domain to the application on localhost. Using reverse proxy servers is an industry practice because it increases the security of the web server by creating a barrier between incoming requests and the backend application itself. Also, reverse proxies allow for better load management on the server, especially when the server is used to host multiple web applications.
We install the package manager pm2 We start with the one that manages the program at runtime.
Install pm2 Process Manager
You can use the following command to open the process manager. pm2 Install.
npm install -g pm2With this work pm2 It is installed globally on your device.
Creating Nginx configuration for NestJS application
Now, configure Nginx to run the application. As mentioned in the prerequisite tutorial, make sure you have allowed the Nginx application for HTTP and HTTPS in your firewall. If you are using ufw firewall, you can do this by following the instructions.
ufw enable
ufw allow ‘Nginx Full‘Now, you will create a configuration block for our NestJS application. It is recommended that you create new configuration blocks for new applications rather than editing the default settings. To create the block, type the following code in your terminal.
sudo nano /etc/nginx/sites-available/your_domainHere, we have used your_domain for the application, but you will change it to the name of your application. Then in the editor, enter the following code:
server {
server_name your_domain www.your_domain;
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;
}
}Now, you create a symlink that tells Nginx to look for available web applications in the sites-available folder:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/Also, you need to disable the default link, otherwise, nginx will redirect all requests to the default site. Use the following command to unlink.
sudo unlink /etc/nginx/sites-enabled/defaultNow restart the Nginx service using the following command.
sudo systemctl restart nginxDeploying NestJS application using Nginx
Now, you will launch the pm2 package manager to manage the execution of your NestJS application. Change the working directory to your Nestjs application directory and type the following command.
pm2 start npm --name "your_domain" – startYou can run the following command to configure pm2 to run on server restart.
pm2 startupOnce you are done setting up the program in pm2, go ahead and save the pm2 process list with the following:
pm2 save
Now, we have set the web application to run at startup and configured Nginx to reverse proxy to our application running on localhost.
Web application testing
You can test the web application from your console by typing the following command.
curl http://localhostSince we have set the reverse proxy to the server's IP address itself, any request to the server's public IP address, domain, or localhost request from the server will be forwarded to the application. NestsJS your_domain It is guided.
OutputHello World!In the next section, you will learn to add SSL to your application, which will allow you to use the HTTPS protocol for your requests.
Step 3 – Add SSL using Let's Encrypt (Optional)
So far, you have learned how to deploy a fully functional NestJS application with the Nginx server. However, this deployment uses the HTTP protocol, which is not recommended in production due to exploit vulnerabilities. Therefore, you want to switch to the HTTPS protocol, which is an encrypted version of HTTP. HTTPS uses SSL/TLS certificates issued by a Certificate Authority. These certificates are specific to a website and encrypt the communication between the client and the server.
Install Let's Encrypt Certbot CLI
Let's Encrypt provides a CLI for managing and automating SSL certificates for consumers. You can install the tool with the following command:
sudo apt install certbot python3-certbot-nginxThis will install the certbot client on your Ubuntu VPS.
Fetch SSL/TLS certificates for your domain
You can now obtain SSL certificates for your domain using the following command
sudo certbot --nginx -d <your_domain> -d <www.your_domain>You must your_domain Replace certbot with your actual domain name. If this is the first time you are running certbot on your VPS, you will be asked to enter your email and agree to the user terms. Provide the required information and continue.
Once the certificates are installed, you can redirect all requests to HTTPS. It is recommended to redirect all requests to ensure the integrity of your website.
Result
In this tutorial, you learned how to deploy a NestJS application using the Nginx web server on an Ubuntu VPS in production. You also learned how to set up a NestJS project and a reverse proxy for your application using Nginx. Finally, you learned how to add SSL/TLS certificates for your server domain and ensure the integrity of communications between your clients and your server.









