How to Deploy a NestJS Application to Digital Ocean

Updated: January 1, 2024 By: Guest Contributor Post a comment

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!