Skip to content

rmnjaat/Cloud_Scaled_User_Authentication-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


πŸš€ Cloud_Scaled_User_Authentication-System using Nodejs and nginx on AWS EC2

This guide explains how to set up a Node.js backend on an Ubuntu EC2 instance and configure it to run in a production environment using Nginx as a reverse proxy.


πŸ“œ Steps for Deployment

1. πŸ”§ Launch an EC2 Instance

  • Go to the AWS Console and launch a new EC2 instance with Ubuntu as the operating system.
  • In the Security Group, allow port 80 (HTTP) and port 22 (SSH) access.
  • After creating the instance, download the .pem key for SSH access.

2. πŸ”’ SSH into EC2 Instance

Use the following command to connect:

ssh -i your-key.pem ubuntu@<your-ec2-public-ip>

3. πŸ§‘β€πŸ’» Clone the Project and Install Dependencies

  1. Navigate to the desired directory and clone your project:
git clone https://github.com/your-username/your-repository.git
cd your-repository
  1. Install dependencies:
npm install

4. βš™οΈ Install Node.js and NPM

  1. Update package list and install Node.js & NPM:
sudo apt update
sudo apt install nodejs
sudo apt install npm
  1. Verify installation:
node -v
npm -v

5. πŸ“ Update package.json

Ensure the scripts section in package.json has the following:

"scripts": {
  "start": "node index.js"
}

Replace index.js with your server file if necessary.


6. 🌐 Install Nginx

Install Nginx to serve as a reverse proxy:

sudo apt update
sudo apt install nginx

Check if Nginx is working by visiting http://<your-ec2-public-ip>.


7. βš™οΈ Configure Nginx

  1. Create a new configuration file:
sudo nano /etc/nginx/sites-available/login.conf
  1. Add the following configuration:
server {
    listen 80;
    server_name <your-ec2-public-ip>;

    location / {
        proxy_pass http://localhost:8080;  # Your Node.js app runs on port 8080
        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;
    }
}
  1. Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/login.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

8. πŸ”’ Set Up .env Variables

  1. Create the .env file:
nano .env
  1. Add environment variables:
PORT=8080
MONGO_CONN=
JWT_SECRET="sec-123"

9. πŸ”„ Run Node.js App with PM2

PM2 ensures your app runs continuously:

  1. Install PM2 globally:
sudo npm install pm2 -g
  1. Start the app with PM2:
pm2 start index.js  # Replace with your main file name
  1. Set PM2 to restart the app on reboot:
pm2 startup
pm2 save

10. βœ… Verify the Setup

  • Open a browser and visit http://<your-ec2-public-ip>. Your app should be up and running.

Conclusion πŸŽ‰

Your Node.js backend is now live on AWS EC2 with Nginx as a reverse proxy. The app runs continuously with PM2, and .env keeps your environment variables secure. πŸš€


Frontend Vite App Deployment on EC2 🌐

This section explains how to deploy a Vite frontend app on your EC2 instance.


πŸ”§ Prerequisites

  1. EC2 instance (Ubuntu or other Linux OS)
  2. Node.js and npm installed
  3. Git installed
  4. Vite project (React, Vue, or other)
  5. Security Groups configured for port 5173

Step-by-Step Guide πŸ“š

1. πŸ›  Install Node.js and npm

  1. SSH into the EC2 instance:
ssh -i your-key.pem ubuntu@<your-ec2-ip>
  1. Install Node.js and npm:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
  1. Verify the installation:
node -v
npm -v

2. πŸ”„ Clone the Vite Project

  1. Install Git (if not already installed):
sudo apt install git
  1. Clone your Vite project:
git clone https://github.com/your-username/your-vite-project.git
cd your-vite-project
  1. Install dependencies:
npm install

3. πŸ”‘ Update Environment Variables

Create or update the .env file for necessary configurations, such as:

VITE_URL="http://<backend-ec2-ip>/auth"

4. πŸš€ Run Vite Development Server

Run the app and make it accessible:

npm run dev -- --host

Check the output:

Local: http://localhost:5173/
Network: http://<public-ec2-ip>:5173/

5. πŸ” Configure Security Group

  1. In AWS Console, navigate to Security Groups.
  2. Add inbound rule for port 5173 (or your Vite port).

6. πŸ” Test External Access

From your browser:

http://<public-ec2-ip>:5173/

Or using curl:

curl http://<public-ec2-ip>:5173/

7. ⏳ Keep the Server Running

Use tmux, screen, or pm2 to keep the server running:

Using tmux
  1. Install tmux:
sudo apt install tmux
  1. Start a tmux session:
tmux new -s frontend
  1. Run your Vite app inside tmux:
npm run dev -- --host
  1. Detach from tmux:

Press Ctrl + b, then d.


Conclusion 🏁

You have successfully set up a Vite frontend app on your EC2 instance with a Node.js backend. Your app is now accessible externally, and PM2 ensures it's always running.


Enjoy the smooth deployment of your applications on AWS! 😎


πŸ“£πŸ“£πŸ“£ Feel Free to Open an Issue or Pull Request πŸ“£πŸ“£πŸ“£

If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request! Don't hesitate to reach out if you face any problems during the setup processβ€”I'm happy to help!


About

Industry level Hosted on Cloud with load balancer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published