How To Configure EC2 For Node.js Apps

blog banner

Amazon EC2 is a cloud service where one can run virtual machines, referred to as instances, on-demand. Lets learn how to configure EC2 for Node.js Apps. It’s an excellent option for deploying Node.js applications since it is highly flexible, scalable, and integrates with other AWS services.

EC2 is simple to scale up or down according to the needs of your application, thus making it useful for small applications as well as for enterprise-level systems.

This blog, will guide you on how to set up an EC2 instance, deploying your Node.js app, and managing your application in a production environment.

This blog takes you through setting up an EC2 instance, deploying a Node.js app, and managing it in the best possible way.

By the end of this blog, you’ll have a completely functional Node.js app running on AWS EC2.

Prerequisites

You should have the following before you set up your EC2 instance

EC2 logo
  • AWS Account: Firstly, sign up at AWS Free Tier. The free tier allows access to a limited amount of EC2 resources, suitable for small projects and experimentation.
  • Basic knowledge of Node.js: This book assumes that you have experience with Node.js and have made at least a simple application. It will also be assumed that you understand how to handle dependencies with npm (Node Package Manager).
  • SSH Key Pair: For security reasons, it is assumed you will generate an SSH key pair that can access your EC2 instance. AWS uses these keys for identifying and authenticating access via SSH.
  • Basic Command-Line Skills: You would require access to a terminal or command-line interface to execute commands on your EC2 instance.

Configuring the EC2 Instance

Choosing the Right EC2 Instance Type

There are several types of EC2 instance, and you will have a choice depending on your app. For minor applications or testing, this would be good to start with a t2 instance since it is eligible for a free tier. Micro instance which comes with 1 vCPU and 1 GB of RAM. 

If more traffic is expected with your app, then you would need to move to bigger instances such as the t2. small or even the t3. Medium.

Factors to be considered

  • CPU and Memory: For simple applications, a small instance such as t2.micro would suffice. For resource-intensive applications, larger instances should be considered.
  • Traffic Expectations: Select an instance type that can handle the expected traffic load.

Launching the EC2 Instance

1. Log in to AWS Console: Go to the AWS Management Console and sign in.

2. Launch EC2 Instance: From the EC2 dashboard, click on “Launch Instance.” You’ll be asked to choose an Amazon Machine Image (AMI). Choose either Ubuntu Server or Amazon Linux 2 as they are widely used for Node.js applications.

3. Configure Instance Settings

  • Instance Name: Give your instance a meaningful name (e.g., NodeApp-EC2).
  • Region: Select the AWS region closest to your target users.
  • Network Settings: You can stick to the default VPC settings or create a new one if there is a need.

4. Security Group Settings: This acts like a virtual firewall for your EC2 instance. When working with Node.js, you’ll open the following ports:

  • Port 22 (SSH):For secure access to your EC2 instance.
  • Port 80 (HTTP):For your application’s web traffic.
  • Port 443 (HTTPS):If you intend to encode your site using SSL.

5. Elastic IP: Optional

You can assign an Elastic IP, if you need a static IP address to your EC2 instance. This will be helpful, especially if your application requires having a fixed IP address.

Configuring SSH Access

Generating and Uploading SSH Key Pair

To connect securely to your EC2 instance, you will need an SSH key pair. AWS will prompt you to create a new key pair when launching the EC2 instance. Download the.pem file and store it securely.

Connecting to Your EC2 Instance via SSH

Once the instance is launched, you can connect via SSH using the following command

ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip

For Ubuntu AMI, the default user is ubuntu instead of ec2-user.

Ensure that the.pem file has the right permissions

chmod 400 /path/to/your-key.pem

Installing Node.js on the EC2 Instance

Updating Your EC2 Instance

First, update your package repository on your EC2 instance. Use the following commands according to the operating system you’re working with:

For Ubuntu

sudo apt-get update

For Amazon Linux

sudo yum update

Install Node.js

You may also install Node.js via a package manager, and you can always use NVM to install Node.js of the desired version. Here is the step to install Node.js on Ubuntu

sudo apt install nodejs npm

Then, you might want to download NVM from here and execute it:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh 
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.bashrc
nvm install node

Verify the install by running 

node -v
npm -v

Uploading Your Node.js Application Uploading Your App to EC2

Upload it from your Node.js app using either SCP (Secure Copy Protocol) or by pulling a Git repository.

Upload SCP

scp -i /path/to/your-key.pem /path/to/your-app ubuntu@your-ec2-public-ip:/home/ubuntu/

Another option is the cloning of repository, if hosted your app with GitHub

ngit clone https://github.com/yourusername/your-node-app.git

Installing Dependencies

After your app files are all on the EC2 instance change to your directory of your application and install it dependencies

cd /path/to/your-app

npm install

How to Run the Application

You can run your Node.js app using the following command

node app.js

Alternatively, you can use npm start if you have set the script in your package.json.

Managing Multiple Instances (Optional)

For production applications, it’s a good idea to use a process manager like PM2 to manage your app and keep it running even after a server restart.

 Install PM2 globally

npm install pm2 -g

Start your app with PM2

pm2 start app.js

Configuring a Reverse Proxy with Nginx (Optional)

Why Use a Reverse Proxy?

A reverse proxy helps route requests to the right server, handles SSL/TLS encryption, and can load-balance traffic. Nginx is a popular reverse proxy that works well with Node.js apps.

Installing Nginx

Install Nginx on your EC2 instance

sudo apt install nginx

Configuring Nginx as a Reverse Proxy

Edit the Nginx configuration file to forward HTTP traffic to your Node.js app

sudo nano /etc/nginx/sites-available/default

Add the following configuration

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;

}

}

Enabling HTTPS with Let’s Encrypt

To secure your app with HTTPS, use Certbot to obtain an SSL certificate from Let’s Encrypt.

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-ec2-public-ip

8. Security Group and Firewall Configuration

Opening Ports for Your Node.js App

Confirm that the EC2 instance security group allows these ports to be open for your instance

  • HTTP: 80
  • HTTPS: 443
  • SSH: 22 

EC2 Security Best Practices

  • Use IAM roles for securely accessing AWS Resources.
  • Limit external access to critical points.
  • Rotate SSH keys and credentials.

9. Scaling and Managing Your Node.js App on EC2

Scaling Your Application

This is where auto-scaling comes in because your app will notice high traffic, and it will scale the number of instances you request for the EC2 based on the load.

Monitoring and Logging

Use AWS CloudWatch in real-time to monitor your EC2 instance. Besides this, you will get logs of the pm2 relative to your Node.js app.

Backup Strategies

Use EC2 snapshots to back up your instance and create an S3 bucket for the regular backups.

10. Conclusion

Setting up a Node.js app on AWS EC2 will allow you to scale when necessary and easily integrate with other AWS services. 

The above steps are a guide on how to easily set up a reliable, secure, and efficient deployment environment for your Node.js application.

If you need further assistance or questions, don’t hesitate to reach out to Codeneur, a developers  boot camp  where you find experts for guidance throughout the entire process of development and deployment. 

Happy coding!

FAQs

Text us on WhatsApp