In this tutorial, we will briefly cover the steps involved in deploying a Node.js/Express application on an AWS EC2 instance. Server configuration and application deployment can often be challenging for developers who may not have a deep understanding of network security or how reverse proxy and load balancing work. After struggling through various blog posts and Stack Overflow answers, I realized the need for a comprehensive guide for deploying applications.
For the first time, I spent nearly three days attempting to handle the hosting aspect and made numerous mistakes. Deploying is much easier if we have a step-by-step guide. In this blog, we will address all the common problems one might encounter.
..Prerequisites
a. Push your project in github. Please add .env and node modules in gitignore
b. Purchase a domain from any domain providers like godaddy. For the sake of simplicity here I'm using 'yourdomain.com'
1. EC2 Configuration
First of all you need to set up the EC2 instance in AWS. I hope you guys already set up the AWS account. From services in AWS select EC2.This will take you to the EC2 dashboard. From here you can select to create a new EC2 instance by clicking the ‘launch instance’ button. Simply add a name and You can choose which operating system to run on your instance. I will be using ubuntu 22.04. Now move to the Key pair and create a key pair by giving a name . Well you noticed a pem file will be downloaded. At the end you will find another button to launch instance
Now please go back to the EC2 dashboard where you will find a new instance. It's status may be pending please wait some minutes it will change to 'Running'. One last step to do. Click on the instance id . You will be redirected to another page. Where you will find security. This will take you to another portion where click on the link below the security groups and change the inbound rules as below
Now we have done all the things with EC2..
2.Let's think about Route 53
In Route 53 Dashboard you will find DNS management where you can create hosted zone. In hosted zone configuration simply giving your domain name you can create it. There you will find 4 records having type as 'NS'.
Don't be panic mkstudio.live is my domain name..😊
Copy each of them excluding the last dote and change it with your domains DNS name servers. I hope you will find that in your DNS providers page. Now create 2 A records by clicking 'create record'.
Create one record with empty subdomain and one as 'www.' You must put the public IP of your instance at the place of value. Finished😁
3.Install Node.js and Clone your git Repository
You can move to the ssh client terminal with the help of connect option in your instance. On your connect window simply copy the example and open your CMD at the location where your pem file is existing. Paste it there enter some 'yes'....Hope you will find it. Or you can use EC2 instance connect option in the connect window
Enter the command bellow to install node.js
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install nodejs
Once we are done setting up node we can clone our repository
git clone <project_repository_clone_URL>
cd yourproject
npm install
Well now we need to create a dotenv file. For that enter the following command
sudo nano .env
Enter the env variables. Here I will provide one example.
API_KEY='fdjnvjk3486787njnkdnv2@@3435'
You can come back to the terminal by clicking ' ctrl + x ' then ' y ' then press enter.
Run the following command
npm start
I have my application running on port 3000. To test it, we can access the public IP address of our instance and specify the port. For me http://18.183.108.5:3000
I hope you will find that your application is running in the public IP.
4.PM2 installation
Now we will install a library named pm2, which will allow our node application to run in the background. To start your application with pm2, execute the following commands.
sudo npm i pm2 -g
pm2 start app
If you have index.js or server.js use pm2 start index.js/server.js
This will launch our app. By visiting the IP address with the specified port again, we should observe that the application is now running.
5.Ngnix and Reverse Proxy
Next, we will configure NGNIX and establish a reverse proxy, which will redirect traffic to our application using the default port 80.
Enter the following command
cd ..
sudo apt install nginx
Finished. Now we can configure it to set up a reverse proxy and redirect our application to port 80. Here we go..
sudo nano /etc/nginx/sites-available/default
Now enter,
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Now restart Ngnix
sudo service nginx restart
6.SSL Certification
Allow some time for the custom domain to propagate. After a while, you can access your custom domain and observe that the application is running.
Excited.. Hope you found your application is running with your domain name😊 But there is 'Not secure' Let's give a lock to there( Now we are going to change http to https).For that we need SSL certification.
Initially, we install the certbot package by executing the following commands
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python3-certbot-nginx
After completing the installation, we will utilize the certbot CLI to generate an SSL certificate. Run the following command and supply your email information when prompted to generate the SSL certificate.
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
On certificate generation you will need to provide your email. After getting the certificate go back and find your application running on your domain.
Top comments (1)
Tnx bruh