Ec2
#devops
Step 1: Set Up an AWS Account
- Sign up for AWS: If you don’t already have an AWS account, sign up at [AWS Free Tier].
Step 2: Launch an EC2 Instance
- Navigate to EC2 Dashboard: Log in to the AWS Management Console and navigate to the EC2 Dashboard. And select nearest cloud server (Mumbai).
- Launch Instance:
- Click on "Launch Instance".
- Select the Amazon Machine Image (AMI): Choose "Amazon Linux 2" or "Ubuntu 22.04 LTS". (free tier)
- Choose an Instance Type: Select the
t2.microinstance type (which is free tier eligible). - Create new key pair (SSH key connection). V.V.Imp: Keep it safe
- Review and Launch: Launch the instance and create a new key pair (or use an existing one).
Step 3: Connect to Your EC2 Instance
- Connect to your instance: Use an SSH client to connect to your instance.
ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dnsStep 4: Install Node.js and Git
- Update and install Node.js:
# installs nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# download and install Node.js (you may need to restart the terminal)
nvm install 20
# verifies the right Node.js version is in the environment
node -v # should print `v20.16.0`
# verifies the right npm version is in the environment
npm -v # should print `10.8.1`- Install Git: Most probably preinstalled
Step 5: Clone Your Repository and Install Dependencies
- Clone your repository:
git clone https://github.com/yourusername/your-repo-name.git cd your-repo-name- Install dependencies:
npm installStep 6: Set Up Environment Variables
Create .env file:
vim .envAdd your environment variables
Step 7: Start Your Application
- Start your Node.js application:
npm start
# node app.jsAlternatively, use a process manager like PM2 to keep your app running:
sudo npm install -g pm2
pm2 start app.js
pm2 startup
pm2 saveStep 8: Configure Security Group to Allow Traffic
Update Security Group:
- Go to the EC2 Dashboard, select your instance, and click on the associated Security Group.
- Edit the inbound rules to allow traffic on port 3000 (or whichever port your app is running on). Custom TSP.
Step 9: Access Your Application
-
Get your instance’s public IP or DNS from the EC2 dashboard.
-
Access your application:
- Open a web browser and navigate to
http://your-ec2-public-dns:3000.
- Open a web browser and navigate to