Sling Academy
Home/Node.js/How to Deploy a NestJS Application to Digital Ocean

How to Deploy a NestJS Application to Digital Ocean

Last updated: January 01, 2024

Introduction

Deploying a NestJS application to Digital Ocean is a great choice for production-level hosting. This tutorial will guide you through the process, from setting up your project to ensuring a successful deployment using the latest NestJS and TypeScript features.

Prerequisites

  • A NestJS application ready for deployment
  • Digital Ocean account
  • Basic understanding of Git and SSH
  • Node.js and npm installed on your local machine

Step By Step Guide

Step 1: Prepare Your NestJS Application

Ensure your application is ready for production by setting the appropriate environment variables and dependencies.

npm install --production

Step 2: Create a DigitalOcean Droplet

Go to your Digital Ocean dashboard and create a new Droplet. Choose an image with Node.js pre-installed or set up Node.js on your server manually.

Step 3: Set Up Your Droplet

Once your droplet is ready, SSH into your new server and install any needed software.

ssh root@your_droplet_ip
apt update
apt install -y nodejs npm

Step 4: Transfer Your NestJS Application to the Droplet

Use Git to clone your repository or an SCP command to upload your project.

git clone your_repository_url

Step 5: Configure Environment Variables

Set up the required environment variables for your NestJS application on the server.

export DATABASE_URL="your_database_url"
export NODE_ENV=production

Step 6: Install Dependencies and Build the Project

Inside your project folder, install dependencies and run the production build.

npm install
npm run build

Step 7: Set Up a Process Manager

Install PM2 to keep your application running in the background.

npm install -g pm2
pm2 start dist/main.js

Step 8: Configure NGINX as a Reverse Proxy

Install and set up NGINX to direct traffic to your NestJS application.

apt install nginx
\n# Edit the default NGINX configuration file to direct traffic to your app
nano /etc/nginx/sites-available/default

Step 9: Secure Your Application with an SSL Certificate

Use Certbot to install a free SSL certificate for your domain.

apt install certbot python3-certbot-nginx
certbot --nginx -d yourdomain.com -d www.yourdomain.com

Step 10: Going Further with Digital Ocean’s Features

Explore additional DO features such as monitoring, alerting, and load balancers to enhance your deployment.

Conclusion

Deploying your NestJS application on Digital Ocean ensures a stable and scalable hosting solution. By following the steps covered in this tutorial, you can efficiently launch your NestJS app to production. Happy coding!

Next Article: How to set up NestJS with Docker Compose

Previous Article: How to get client IP address in NestJS

Series: Nest.js Tutorials: From Basics to Advanced

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates