🚀 How to Deploy Your NestJS Backend on AWS EC2 with SSL in Minutes (Using Automation Script)
Introduction
In recent time, I have had to deploy different nest.js backend apps on ec2 server, this can be challenging for a beginner or someone new to deploying on AWS EC2. This article will break things down in a simple terms, I wish I had seen this kind of guide when I was trying my first deployment years ago.
What is AWS?
AWS stands for Amazon Web Services. It is a platform that offers a wide range of services, tools, and technologies for building, managing, and deploying applications (which is our focus for today).
Some examples of AWS services include:
AWS Simple Email Service (SES): This provides Simple Mail Transfer Protocol (SMTP) support. Think of the emails in your apps such as welcome emails, password reset emails, forgot password emails, or 2FA verification emails.
AWS Elastic Compute Cloud (EC2): This is the main focus of this article. EC2 allows you to launch and manage virtual servers for hosting and running your applications.
What is EC2 in simple term?
Using a simple analogy, EC2 is like your regular operating system — it could be Windows, macOS, or Linux (like Ubuntu). The main difference is that EC2 runs on a server managed by AWS. It allows you to run your applications the same way you would on your local PC, but with the added advantage that your application can be accessed by anyone through a URL (e.g., www.example.com or api.example.com/docs) after some setup, which will be explained later in this article.
Another difference is that EC2 stays switched on for as long as you want, provided you are paying for the server (or as long as you are on the free tier). However, you can stop the EC2 instance to save costs and restart it later, which is especially useful during the development phase.
Setting up your first EC2 on AWS.
Navigate to https://signin.aws.amazon.com/signup?request_type=register to create a new account on aws.
After successfully registering, search for EC2 by typing
ecinto the global search bar at the top left corner of your AWS account dashboard. Also, make sure to note the AWS region you are using — you can find it at the top right corner of the dashboard (see image below).
- Click on the EC2 icon, and it will take you to a new page similar to the one shown below.
Note: The screenshot reflects how the page looks as of 25th August 2025. Amazon regularly updates their dashboard layout, but the text and options will generally remain the same.

Click on the Launch Instance button. This will take you to a new page where you can configure your server. Here, you will:
Select the Operating System (OS) image — this is similar to choosing an operating system on your local computer (e.g., Windows, macOS, or Linux). For this tutorial, select Ubuntu.
Name your server.
Choose RAM and storage (memory) options.
Create or select a key pair — this is a security file (with extensions
.pemor.ppk) that allows you to connect to your server securely.
Amazon also provides a Free Tier option, which we will use for this tutorial.
Next, configure security settings:
Allow SSH traffic (port 22).
Allow HTTP traffic (port 80).
Allow HTTPS traffic (port 443).
These rules make your server accessible from anywhere. This means that when you deploy your app, it can be reached globally via the internet. However, note that only someone with your .pem key file will be able to log in to the server through SSH.

Note: A key pair file contains the private and public keys needed to access your newly created EC2 instance. You can think of it as a password required to log in to your operating system on your PC. It must be kept safe and protected from unauthorized access.
For this tutorial, download your key pair as a .pem file and save it in a secure folder. Be sure to remember the folder location, as you will need the .pem file later in the setup process.

Once you click on Launch Instance, your new instance should start running.
The next step is to log in to your EC2 instance using Secure Shell (SSH).
Follow the instructions shown in the images below:


Open your terminal, then navigate to the directory where you saved your .pem file.
Run the following command to set the correct permissions on the file:chmod 400 “yourpem.pem“, then run the command ssh -i "yourpem.pem" ubuntu@yourec2ip.compute-1.amazonaws.com.
If everything is correct, you should now be logged into your server. The terminal should look similar to the image below.

Afer accessing the server successfully, do the following
1. A domain name
Buy a domain (from Namecheap, Cloudflare, GoDaddy, etc).
Create an A record in your DNS that points your domain (e.g.,
api.yourdomain.com) to your EC2’s public IP.
2. Install required tools on the server
SSH into your EC2 instance and install these:
sudo apt update && sudo apt install -y git curl nginx certbot python3-certbot-nginx
3. A non-root user
For security, don’t run the script as root. Create a user, add it to the sudo group, and use that one. Example:
sudo adduser deployer
sudo usermod -aG sudo deployer
su - deployer
4. Your NestJS project in a Git repository
Make sure your project is in GitHub, GitLab, or Bitbucket so the script can clone it.
5. Create a bash file in your server(EC2 server)
Run nano deploy-backend.sh copy and paste the script from this url https://github.com/Tdaycode/nestjs-deloyment-script/blob/main/deploy-backend.sh
Run chmod +x deploy-backend.sh then ./deploy-backend.sh.
Follow the script prompt as it is in the image below



And booooooom your app is live
After Deployment
Test your API
curl https://api.example.com/health
Update your app
cd /home/username/apps/app-name
./update-backend.sh

Monitor your app
./monitor-backend.sh

What the Script Does for You
Here’s what happens under the hood:
Installs Node.js (using NVM).
Installs your preferred package manager (npm, yarn, or pnpm).
Deploys your NestJS app into
/home/username/apps/app-name.Sets up PM2 to keep your app alive and restart on crash.
Configures Nginx as a reverse proxy.
Secures your domain with Let’s Encrypt SSL (free 🔐).
Generates update & monitoring scripts for easy maintenance.
Thanks for reading!!!
If there is any other backend concept or topic you would like me to write about, kindly comment or reach out on linkedin.
https://linkedin.com/in/ganiyuomotayo

