Amazon EC2 (Elastic Compute Cloud) is widely used for hosting applications because it is scalable and flexible. In this tutorial, we will install a Node.js server on an EC2 instance, configure a static IP address, install Nginx as a reverse proxy, configure security groups, and install an SSL certificate with Certbot.
Step 1: Launch an EC2 Instance
- Go to the AWS Management Console and navigate to the EC2 dashboard.
- Click Launch Instance and choose an Amazon Machine Image (AMI), such as Ubuntu 22.04 LTS.
- Select an instance type (e.g., t2.micro, which is free-tier eligible).
- Configure instance details and select Create new key pair for SSH access.
- Launch the instance and download the key file (.pem).
Step 2: Assign a Static IP (Elastic IP)
- In the AWS EC2 dashboard, navigate to Elastic IPs under Network & Security.
- Allocate a new Elastic IP and associate it with your running instance.
- Update your domain’s DNS settings if needed to point to the static IP.
Step 3: Connect to Your EC2 Instance
Use SSH to connect to the instance:
ssh -i your-key.pem ubuntu@your-ec2-ip
Step 4: Install Node.js and PM2
Update the system and install Node.js:
sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Verify installation:
node -v
npm -v
Install PM2 for managing Node.js processes:
npm install -g pm2
Step 5: Deploy Your Node.js Application
- Clone or copy your Node.js project into the instance:
git clone https://github.com/your-repo.git
cd your-repo
npm install
- Start the Node.js server using PM2:
pm start
# OR
pm run start:prod
# To keep it running in the background
pm2 start app.js --name "node-server"
pm2 save
Step 6: Configure Security Groups
- In the AWS EC2 dashboard, go to Security Groups.
- Edit inbound rules to allow:
- SSH (port 22) – only for your IP.
- HTTP (port 80) – open to the world.
- HTTPS (port 443) – open to the world.
- Custom (port 3000) – if your Node.js app runs on port 3000 (for internal access only).
Step 7: Install and Configure Nginx as a Reverse Proxy
- Install Nginx:
sudo apt install nginx -y
- Configure Nginx:
sudo nano /etc/nginx/sites-available/default
Replace the contents with:
server {
listen 80;
server_name your-domain.com;
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;
}
}
- Restart Nginx:
sudo systemctl restart nginx
Step 8: Install SSL Certificate using Certbot
- Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
- Generate and install an SSL certificate:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
- Auto-renew SSL:
sudo certbot renew --dry-run
Conclusion
You have successfully installed a Node.js server on AWS EC2 with Nginx as the reverse proxy and SSL encryption through Certbot. This installation provides a secure and scalable deployment for your application. Happy coding!
Top comments (0)