INTRODUCTION

Amazon Lightsail offers a simplified way to deploy and manage applications in the cloud. In this guide, we’ll walk through the process of hosting a Node.js application on an AWS Lightsail instance, taking advantage of its user-friendly interface and seamless integration with AWS services.

PREREQUISITES

Preparing for Migration

a. Choose a Lightsail plan:

Login to the AWS console and search or Navigate to the Light Sail console below shown image and click on the create instance for creating a server.

Next you have to choose the Image you want for your server since most of the nodejs applications runs based on the Ubuntu server. I choose choose the OS only option and manually configure all needs, then scroll down for next.

Select a pricing model that suits your applications need.

Add your instance name and click create instance as shown on the picture below.

Once created, you can see the instance spinning up and ready in a while.

b. Configuring Lightsail:

Click on the instance and you will then move into the detailed view of the instance. Click on the networking tab.

On networking, add the port as custom which is used in your nodejs application like 3000 or 5000 as seen in the image below.
Used for Secure Shell (SSH) connections

C. Deploying your Node.js Application

sudo apt-get update
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
nvm --version
nvm install node
nvm install 18
nvm use 18
git clone <paste project git url>
npm install
sudo npm install pm2 -g
pm2 start index.js --name booking-application-backend
pm2 startup systemd
pm2 save

D. Reverse proxy setup:

If your Node.js application operates on a non-standard port (such as 3000 or 5000), consider setting up a reverse proxy using Nginx or another server. This will forward requests to your Node.js application, bolstering security and enabling the use of a custom domain.

sudo apt install nginx

sudo systemctl start nginx

sudo systemctl enable nginx

server_name yourdomain.com www.yourdomain.com;

location / {
proxy_pass http://localhost:8000; # your app's port
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;
}
sudo systemctl reload nginx

If you are using a custom domain name, go to your dns provider and add A record

E. Setup SSL/TLS

Finally, the only task left to do here is to secure our web server with SSL encryption. We can do this with the Certbot library very easily.

Install Certbot and prepare certbot command.

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Run this command to get a certificate and have Certbot edit your nginx configuration automatically to serve it, turning on HTTPS access in a single step.

sudo certbot - nginx

Test automatic renewal

sudo certbot renew --dry-run

To confirm that your site is set up properly, visit https://yourwebsite.com/ in your browser and look for the lock icon in the URL bar.

That’s it. Now you have a secure web application running in your AWS Lightsail Instance. 👏

Conclusion:
AWS Lightsail streamlines Node.js application hosting, combining the power of AWS infrastructure with a user-friendly interface. This approach lets developers focus on crafting exceptional applications rather than grappling with complex server management. By following a straightforward deployment process, you can effortlessly launch, maintain, and expand your Node.js projects, freeing up valuable time and resources for innovation and feature development.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

<hr><p>Migrating Your Existing Node.js App to AWS Lightsail: A Practical Guide was originally published in JavaScript in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>