How to Deploy Node App to AWS EC2 Instance
Introduction:
Welcome to the world of DevOps! Whether you’re a complete beginner or have some experience with servers, you can deploy your application on AWS EC2 with just a few easy steps. We will walk you through every step, including the setting up AWS EC2 Instance, installation of Node.js and all other dependencies, Nginx installation and configuration, and SSL configuration.
Step 1: Launching and Accessing Your EC2 Instance
- Set Up Your EC2 Instance: Log in to your AWS Management Console and navigate to the EC2 dashboard. Click on “Launch Instance” to begin the setup process.
- Choose an Amazon Machine Image (AMI): Select an Ubuntu AMI that aligns with your requirements. Ubuntu Server LTS versions are recommended for stability and long-term support.
- Select Instance Type: Choose an instance type based on your application’s needs. Consider factors like CPU, memory, and network performance.
- Configure Instance: Customize instance settings such as network, storage, and security groups. Ensure SSH (port 22) is open to allow remote access.
- Review and Launch: Review your instance configuration and click “Launch.” Select or create a new SSH key pair for secure access to your instance.
- Access Your Instance: Once your instance is running, SSH into it using your terminal or preferred SSH client:
ssh -i /path/to/your-key.pem ubuntu@your-instance-public-ip
Replace /path/to/your-key.pem with the path to your SSH private key and your-instance-public-ip with the public IP address of your EC2 instance.
Step 2: Installing Node.js and NPM
After accessing your EC2 instance, update the package index and install Node.js and NPM using the following commands:
sudo apt update
sudo apt install nodejs
node -v
sudo apt install npm
Verify the installations by checking the Node.js and NPM versions:
node -v
npm -v
Step 3: Deploying Your Node.js Application
Clone Your Application Repository: Navigate to the desired directory and clone your Node.js project repository using Git:
cd /var
sudo mkdir your_project_name
sudo git clone your_project_repository_url
cd your_project_name
Install Dependencies: Install project dependencies using npm and start project:
npm install
npm install pm2 -g
pm2 start index.js
Nginx will serve as a reverse proxy for your Node.js application, managing incoming HTTP requests. Install Nginx using the following command:
Step 4: Nginx Installation and Configuration Steps
sudo apt install nginx
cd /etc/nginx/sites-available
vi filename
This is the sample file for the configuration with SSL certification:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name exmaple.com;
return 301 https://exmaple.com$request_uri;
location / {
proxy_pass http://localhost:5000;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
}
}
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/exmaple.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/exmaple.com/privkey.pem;
server_name exmaple.com;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
proxy_pass http://localhost:5000;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
}
}
Step 5: Installing SSL Certificate with Certbot
Securing your website with SSL/TLS encryption enhances data privacy and builds trust with your users. Follow the SSL certificate installation steps provided in the previous version of the guide
sudo apt install certbot
systemctl stop nginx
certbot certonly -d example.com
systemctl restart nginx
After install the certbot and restart the nginx server, This line creates a symbolic link (shortcut) from the Nginx configuration file located in the /etc/nginx/sites-available
directory to the /etc/nginx/sites-enabled
directory. After that restart the nginx again.
sudo ln -s /etc/nginx/sites-available/filename /etc/nginx/sites-enabled/
systemctl restart nginx
Congratulations! You’ve successfully deployed your Node.js application on an AWS EC2 instance running Ubuntu. Your application is now accessible over the internet, ready to serve users with speed and reliability. Happy coding!