Skip to main content
Back to blog// general

Deploying a NestJS Application

This is a continuation of the NestJS series, take a look at part 1 here Here we'll cover: Setting up NestJS app Dockerizing your NestJS app Deploying to various platforms Using Nginx Other Deploy

NK

Nicanor Korir

Author

July 22, 2024
9 min read
nestjsbackenddeployment

This is a continuation of the NestJS series, take a look at part 1 here

Here we'll cover:

  • Setting up NestJS app
  • Dockerizing your NestJS app
  • Deploying to various platforms
  • Using Nginx
  • Other Deployment methods

Why Deploying

Deploying an app can both be an easy and a difficult task depending on the platform you deploying to and if you've CI/CD pipelines set up. I'll cover the CI/CD pipeline setup with NestJS in another article. I'm going to summarise how to deploy a Nestjs app to the common platforms with the common ways

Step 1: Setting Up Your NestJS Application

Before we can deploy our application, let’s ensure we have a basic NestJS app set up. Take a look at NestJS 101 to get started on a simple REST API. If you don’t already have one, you can create it using the NestJS CLI:

Bash
bash
1
2
3

Step 2: Dockerizing Your NestJS Application

Dockerizing your application helps in creating a consistent environment across development, testing, and production.

Create a Dockerfile

At the root of your project, create a Dockerfile with the following content:

YAML
yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Create a .dockerignore file

Create a .dockerignore file to exclude unnecessary files from the Docker build context:

CSS
css
1
2
3
4
5

Build and Run the Docker Image

Build the Docker image:

Bash
bash

Run the Docker container:

Bash
bash

Your application should now be running inside a Docker container and accessible at `http://localhost:3000`. Using a platform that supports orchestration, you can manage docker images there to expose to external users

Step 3: Deploy Apps to Various Platforms

1\. AWS (Amazon Web Services)

Install AWS CLI to make it efficient in managing was resources

Bash
bash
1
2
3
4
5
6
7
8
  1. #### Using Elastic Beanstalk

Elastic Beanstalk is a PaaS that simplifies the process of deploying applications to AWS. It makes it easier to deploy and manage applications in the cloud without worrying about the infrastructure that runs those applications. Elastic Beanstalk automatically handles the deployment, from capacity provisioning, load balancing, and auto-scaling to application health monitoring.

  • Install the Elastic Beanstalk CLI: Use brew or pip
  • ```bash

brew install awsebcli

```

  • Initialize your Elastic Beanstalk application:

```bash

eb init -p node.js my-nestjs-app

```

  • Create an environment and deploy your application:

```bash

eb create my-nestjs-env

eb deploy

```

  • Access deployed app
Bash
bash
1
2
  1. #### Using ECS (Elastic Container Service) and ECR (Elastic Container Registry)

ECS is a fully managed container orchestration service that makes it easy to run, stop, and manage Docker containers on a cluster of Amazon EC2 instances. ECS can be used to deploy and manage containerized applications.

Amazon Elastic Container Registry (ECR) is a fully managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images.

  • Push Your Docker Image to ECR:

* Authenticate Docker to your Amazon ECR registry.

* Create a repository in Amazon ECR.

* Tag your Docker image and push it to the repository.

```bash

aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account-id.dkr.ecr.your-region.amazonaws.com

docker tag my-nestjs-app:latest your-account-id.dkr.ecr.your-region.amazonaws.com/my-nestjs-app:latest

docker push your-account-id.dkr.ecr.your-region.amazonaws.com/my-nestjs-app:latest

```

  • Set Up ECS:

* Create a new ECS cluster.

* Define a task definition that uses your Docker image from ECR.

* Create a service that runs your task definition on your ECS cluster.

Bash
bash
1
2

Access the deployed app URL using the public URL from the ECS task definition

2\. Heroku

Heroku is a cloud platform that simplifies deploying applications.

  • Install the Heroku CLI:

```bash

curl https://cli-assets.heroku.com/install.sh | sh

```

  • Log in to Heroku and Create a New App:

```bash

heroku login

heroku create my-nestjs-app

```

  • Deploy Your Application:

* Add a Procfile to the root of your project specifying how to run your app.

```bash

web: npm run start:prod

```

* Commit your changes and push to Heroku.

```bash

git add .

git commit -m "Deploy to Heroku"

git push heroku main

```

3\. Render

Render is one of my favorite to deploy my personal apps. Render is a simple yet powerful platform for deploying applications. It's a modern cloud platform that simplifies the process of deploying applications, databases, and static sites. It offers a variety of features and services that make it easy to deploy and manage applications with minimal configuration.

  1. Create a render.yaml file in your project:

```yaml

services:

- type: web

name: my-nestjs-app

env: node

plan: free

buildCommand: npm install && npm run build

startCommand: npm run start:prod

```

  1. Commit the file and push it to your Git repository.
  1. Connect your Git repository to Render and deploy.

4\. Digital Ocean

Digital Ocean is a cloud infrastructure provider that offers a range of cloud services to help developers deploy, manage, and scale applications quickly and efficiently. DigitalOcean provides virtual private servers (called Droplets), managed databases, object storage, Kubernetes clusters, and various other services.

Using App Platform

  1. Create a new app on the Digital Ocean App Platform.
  1. Connect your Git repository.
  1. Set the build and run commands
  1. Deploy your application.

Using Droplets with Docker

  1. Create a Droplet and install Docker.
  1. Push your Docker image to Docker Hub.
  1. Pull the Docker image on your Droplet and run it.

Step 4: Using Nginx for Deployment

Nginx is a high-performance web server, reverse proxy server, and load balancer. It is known for its stability, rich feature set, simple configuration, and low resource consumption. Nginx is commonly used to serve static content, as well as to proxy dynamic content requests to other servers.

Nginx can be used as a reverse proxy to serve the NestJS application.

Configuring Nginx

Create an Nginx configuration file (e.g., nestjs.conf):

YAML
yaml
1
2
3
4
5
6
7
8
9
10
11
12
13

Deploy Nginx and Your Application

  1. Install Nginx on your server.
  1. Copy your configuration file to /etc/nginx/conf.d/.
  1. Restart Nginx:

```bash

sudo systemctl restart nginx

```

  1. Ensure your NestJS application is running and listening on port 3000.

Step 5: Other Deployment Methods

Using PM2

PM2 is a process manager for Node.js applications that helps keep your application alive forever and provides an easy way to manage processes.

  1. Install PM2 globally:

```bash

npm install -g pm2

```

  1. Start your application with PM2:

```bash

pm2 start dist/main.js --name my-nestjs-app

```

  1. Save the PM2 process list and configure it to start on boot:

```bash

pm2 save

pm2 startup

```

There are some other ways to deploy NestJS apps that I haven't covered e.g. GCP, Azure, On-Prem, windows server, etc. These are just the most common ways I've interacted and worked with. If there's any you would love to chat about, please leave a comment here, am looking forward to learning more

Stay in the Loop

Get occasional updates on AI engineering, robotics projects, and lessons from building startups. No spam, unsubscribe anytime.