First Log In to AWS and search ‘EC2’. Click on it.
Now click on Instances (running) then click on Launch Instances.
You can Name your instance. Select Ubuntu Server 22.04 LTS as your OS Image. It should be not too old but recent and instance type would be t2.micro which is good to start with.
We can create a key pair like this (see the configuration). It will ask us to download the file, we’ll use this file to log in to the server.
vpc is like a container in a region in which we have everything, whatever we create will go into vpc.
Subnet is kind of a region like where you want to host it.
Auto-assign public IP: If you want the server to be available publicly through an IP address then we can Enable it.
Security group: It is like a firewall, we can mention all the rules here like incoming or outgoing traffic.
We can create our own Security group and attach it to EC2 instance or select the existing one.
Search security groups and click on it to create your own security group.
We should allow ssh → port 22 to get access to the machine. If we don’t allow it, then we won’t be able to enter into the machine cause generally we connect to the machine though ssh.
We have two security groups configuation:
Inbound rules (What traffic can come in from outside)
OutBound rules (What traffic we make from our machine)
Two rules, All traffic from anywhere 1. IPv4 and 2. IPv6 (This is not recommended)
We will allow specific services (SSH, HTTP, HTTPS) from Anywhere (IPv4) to access our EC2 instance. We can also allow some custom services on a specific port, for ex, 5000 from anywhere.
For OutBound rules, keep it same, if you delete anything then machine won’t be able to make request outside to the internet.
Now your security group will be visible to the “Select existing security group” selection.
Keep every other settings as default.
In Advanced Details > User data → we can write commands which will run when the instance gets started for the first time. This command will run as a root. Don’t need to start with sudo.
Now click on “Launch Instance”.
You can directly connect to the EC2 instance by selecting the instance then clicking on “Connect” or use SSH to connect it through the terminal.
sudo apt update // to update the environment
Clone the repo in the machine (EC2 instance). The node might not installed, so install it. Then do:
npm run dev
<EC2-public-IP>:port , eg. 13.126.10.230:5000
Copy the public IP from instance machine details and put the port you’ve allowed to open and this is the same port our application is running on. If you hit, you’ll see the response in your browser. Remember, you’ve added port 5000 in the inbound rules (security rules), if you delete it, you won’t be able to access the application.
If we do npm run dev and close the session from the terminal to EC2 then we won’t be able to access the site. So we need a way to run the process in the background even if we close the session.
Solution: Pm2
sudo npm i -g pm2 // To install pm2
pm2 status // To check which processes are running
pm2 start index.js // To start a process, index.js is your backend entry file
pm2 stop 0 // To stop the first process.
But user don’t type port along with IP or domain right? Even for API, they access it with http or https. So for that we’ll install nginx.
sudo apt install nginx // To install nginx
cd /etc/nginx -> Here we configure the nginx
If we now directly go the EC2 public IP, we’ll see the Nginx Page. We want this to be routed to the localhost:5000. So we need to setup the “default” points to localhost:5000.
sudo nano default // to edit the nginx default file.
location / {} // So if anyone comes to this path -> '/' is the default IP address
sudo systemctl restart nginx // To restart nginx after configuring it
sudo systemctl status nginx // To check its status whether its running or not
Our server should also be running so that you can see it in the browser when you hit the IP. So now you can see it, just copy paste the public IP in the browser.
Its directly available in port 80 now.
SSL configuration → We need a domain for that.
Its not a good practice to write site configuration in “default” nginx file, there is a folder called “sites-available” where you can create separate file for each sub-domain and link it to “default”.