Deploy Node App to AWS EC2 Instance.
Kingsley Odim
Posted on June 12, 2024
Deploying a Node.js application on an EC2 instance and configuring a domain to point to it while ensuring the app keeps running involves several steps. Here's a comprehensive guide to achieve this:
Prerequisites
- AWS Account
- Domain name
- Basic knowledge of Node.js, SSH, and Linux command line
Steps
-
Launch an EC2 Instance
- Log in to your AWS Management Console.
- Navigate to the EC2 Dashboard.
- Click "Launch Instance".
- Choose an Amazon Machine Image (AMI), preferably Ubuntu or Amazon Linux.
- Choose an instance type (e.g., t2.micro for small applications).
- Configure instance details, storage, and tags as needed.
- Configure Security Group to allow HTTP (port 80), HTTPS (port 443), and SSH (port 22) access.
- Review and launch the instance, then download the key pair for SSH access.
-
Set Up the EC2 Instance
-
Connect to your EC2 instance via SSH:
ssh -i /path/to/your-key-pair.pem ubuntu@your-ec2-public-ip
-
- Update the package list and install the necessary dependencies:
```
sudo apt update
sudo apt install nodejs npm
sudo npm install -g pm2
```
-
Deploy Your Node.js Application
-
Transfer your application code to the EC2 instance using
scp
or by cloning from a Git repository:
scp -i /path/to/your-key-pair.pem -r /local/path/to/your-app ubuntu@your-ec2-public-ip:/home/ubuntu
-
- Navigate to your application directory and install dependencies:
```
cd /home/ubuntu/your-app
npm install
```
- Start your Node.js application using PM2 to keep it running:
```
pm2 start app.js
pm2 save
pm2 startup
```
-
Set Up a Domain Name
- Go to your domain registrar and configure the DNS settings:
- Create an
A
record pointing to your EC2 instance's public IP address.
- Create an
- (Optional) For SSL, consider using AWS Certificate Manager and setting up an Application Load Balancer (ALB).
- Go to your domain registrar and configure the DNS settings:
-
Configure a Reverse Proxy with Nginx (Optional but Recommended)
-
Install Nginx:
sudo apt install nginx
-
- Configure Nginx as a reverse proxy:
```
sudo nano /etc/nginx/sites-available/default
```
- Replace the contents with the following (adjust paths and server name as needed):
```
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;
}
}
```
- Test the Nginx configuration and restart Nginx:
```
sudo nginx -t
sudo systemctl restart nginx
```
-
Access Your Application
- Navigate to your domain in a web browser to see your deployed Node.js application.
Additional Tips
- Security: Regularly update your EC2 instance and installed packages.
- Monitoring: Use PM2's monitoring tools to keep track of your application's performance.
- Backups: Consider setting up automatic backups of your EC2 instance and application data.
This guide provides a basic setup. For production environments, you may want to look into more advanced configurations, such as load balancing, auto-scaling, and using a managed database service like Amazon RDS.
Posted on June 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.