Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ashifhassandev/aws-ec2-node-hosting

A step-by-step guide to hosting a Node.js application on AWS EC2 with Nginx and PM2.
https://github.com/ashifhassandev/aws-ec2-node-hosting

aws ec2 nginx nodejs pm2

Last synced: 5 days ago
JSON representation

A step-by-step guide to hosting a Node.js application on AWS EC2 with Nginx and PM2.

Awesome Lists containing this project

README

        

# Guide to Hosting Your Project on AWS EC2

## Prerequisites:
1. AWS account
2. Node.js and NPM installed locally
3. Basic knowledge of Linux and SSH
4. Project ready to deploy

## Step 1: Launch an EC2 Instance
1. Log in to your AWS Management Console.
2. Navigate to the EC2 service.
3. Click on "Launch Instance."
4. Select an Amazon Machine Image (AMI): Choose Ubuntu 20.04.
5. Select Instance Type: Choose "t2.micro" (Free tier eligible).
6. Configure Instance:
- Add a key pair for SSH access.
- Add a security group with rules to allow HTTP, HTTPS, and SSH traffic.
7. Launch the instance.

## Step 2: Configure DNS Records
1. Log in to your domain registrar (e.g., GoDaddy, Namecheap, etc.).
2. Go to the DNS management section for your domain.
3. Add the following records:
- **A Record**:
- Host: `@`
- Points to: `your-ec2-public-ip`
- TTL: Default or 1 hour
- **CNAME Record** (optional, for `www`):
- Host: `www`
- Points to: `your-domain.com`
- TTL: Default or 1 hour
4. Save the changes.
5. It may take a few minutes to a few hours for DNS propagation.

## Step 3: Connect to Your EC2 Instance
1. Download the private key file (.pem) for the key pair you created.
2. Open your terminal and navigate to the directory containing the .pem file.
3. Run the command:
```bash
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
```
4. Replace "your-key.pem" with your key file and "your-ec2-public-ip" with the public IP of your instance.

## Step 4: Set Up Your Environment
1. Update the system:
```bash
sudo apt update && sudo apt upgrade -y
```
2. Install Node.js and NPM:
```bash
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
```
3. Verify installation:
```bash
node -v
npm -v
```
4. Install PM2 globally for process management:
```bash
sudo npm install pm2 -g
```

## Step 5: Deploy Your Project
1. Clone your project repository:
```bash
git clone https://github.com/your-username/your-repo.git
```
2. Navigate to the project directory:
```bash
cd your-repo
```
3. Install dependencies:
```bash
npm install
```
4. Start the application with PM2:
```bash
pm2 start app.js
```
Replace `app.js` with your main entry file.
5. Save the PM2 process list to auto-start on reboot:
```bash
pm2 save
pm2 startup
```

## Step 6: Configure Nginx as a Reverse Proxy
1. Install Nginx:
```bash
sudo apt install nginx
```
2. Configure Nginx:
```bash
sudo nano /etc/nginx/sites-available/default
```
Replace the file content with:
```
server {
listen 80;
server_name your-ec2-public-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;
}
}
```
Replace `your-ec2-public-ip` with your instance's public IP and `3000` with your app's port.
3. Restart Nginx:
```bash
sudo systemctl restart nginx
```

## Step 7: Test Your Application
1. Open your browser and visit your domain (e.g., `http://your-domain.com`).
2. Your application should be live.

## Additional Resources
For a full detailed guide to hosting a Node.js app on AWS EC2, see this link:
[https://waterrmalann.notion.site/A-simple-guide-to-hosting-a-Node-js-app-on-AWS-EC2-6e05ee8c5689452f94a2c550f66aeecd](https://waterrmalann.notion.site/A-simple-guide-to-hosting-a-Node-js-app-on-AWS-EC2-6e05ee8c5689452f94a2c550f66aeecd)

## Notes:
- Make sure to replace placeholders like `your-key.pem`, `your-ec2-public-ip`, and `3000` with actual values.
- Always keep your `.pem` file secure.
- Use environment variables for sensitive data in your project.