So, you’ve built an awesome NestJS app. Now comes the fun part: making it live for the world to see! But let’s be real—deploying to AWS EC2 can feel like assembling IKEA furniture without the manual.
Don’t worry. I’ve burned my fingers so you don’t have to. By the end of this guide, your app will be humming on AWS EC2 like it’s hosting a Grammy after-party. Let’s go!’
What You’ll Need
- An AWS account (If you’re new, grab the free tier)
- An EC2 instance
- Your NestJS code on GitHub (if not available you can use mine one for now)
Update the System and Install Dependencies
First, log in to your EC2 instance using SSH and run the following command to update and upgrade your system
sudo apt update && sudo apt upgrade -yInstall Node.js and NPM using NVM
Since AWS EC2 doesn’t come with Node.js pre-installed, we will install Node Version Manager (NVM), which allows us to install and manage Node.js versions easily.
Switch to the root user and install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bashThis will loads nvm.
. ~/.nvm/nvm.sh Now, install Node.js (version 20):
nvm i 20Verify the installation:
node -vnpm -v
Install Git and Clone Your Repository
Git is needed to fetch your project from GitHub. Install it using:
sudo apt-get update -ysudo apt-get install git -yCheck if Git is installed correctly:
git --versionNow, clone your NestJS project from GitHub (replace the URL with your repository link):
git clone https://github.com/AhmedFakharABbas/graphql-nest-api.gitMove into the project directory:
cd nodejs-on-ec2Install project dependencies:
npm installStart Your NestJS Project
Now, it’s time to start your application! Run the following command:
npm startIf everything is set up correctly, your application should now be running!
sudo apt update && sudo apt upgrade -ySet Up Nginx as a Reverse Proxy
sudo apt updatesudo apt install nginxsystemctl status nginxcurl -4 icanhazip.comsudo chown -R $USER:$USER /home/ubuntu/graphql-nest-apisudo chmod -R 755 /home/ubuntu/graphql-nest-apisudo nano /etc/nginx/sites-available/gqlnestapi
server {
listen 80;
listen [::]:80;
server_name 18.175.168.146;
location / {
proxy_pass http://localhost:3000;
}
}sudo ln -s /etc/nginx/sites-available/gqlnestapi /etc/nginx/s_tes-enabled/sudo nano /etc/nginx/nginx.confserver_names_hash_bucket_size 64;sudo nginx -tsudo systemctl restart nginxUpdate inbound role
Allow trafic from any part of the world! from your inbut roles
Leave a Reply