An IIHT Company

Amazon Machine Image (AMI) based on Ubuntu 20.04 with Nginx configured to forward port 80 requests to a Node.js web server running on localhost:3000 using PM2:

Launch an Ubuntu 20.04 EC2 Instance:

Log in to your AWS account.
Go to the EC2 Dashboard.
Create a new EC2 instance using Ubuntu 20.04 as the base image.
Access Your EC2 Instance:

Once the instance is up, establish an SSH connection using your key pair.
Copy code
ssh -i your-key.pem ubuntu@your-instance-public-ip
Update and Upgrade Packages:

Copy code
sudo apt update
sudo apt upgrade -y
Install Nginx:

Copy code
sudo apt install nginx -y
Configure Nginx:
Edit the Nginx configuration file at /etc/nginx/sites-available/default to define a server block that forwards requests to your Node.js app on localhost:3000 via proxy.

Replace the default server block with the following:

Copy code
server {
listen 80;
server_name; # Replace with your domain or IP

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;
Save the file and exit.

Test Nginx Configuration:
Ensure the Nginx configuration is error-free:

Copy code
sudo nginx -t
Restart Nginx:

Copy code
sudo systemctl restart nginx
Install Node.js and PM2:
Install Node.js and PM2 for running your Node.js web application:

Copy code
sudo apt install nodejs -y
sudo apt install npm -y
sudo npm install -g pm2
Deploy Your Node.js Application:
Deploy your Node.js application to the server and start it using PM2. Navigate to your app’s directory and run:

Copy code
pm2 start your_app.js
Create an AMI:
Once you have configured your server, create an AMI from your running EC2 instance. This will capture the software installations and configurations.

Terminate the EC2 Instance:
If you no longer need the original EC2 instance, you can choose to terminate it to save costs.

Now, you have an AMI that includes Ubuntu 20.04, Nginx configured to route port 80 requests to your Node.js app on localhost:3000 via PM2. This AMI can be used to launch new instances with the same setup whenever required.