Deploying a Node.js application to production is a crucial step for developers. AWS Lightsail provides an easy-to-use cloud platform for launching virtual private servers (VPS) with pre-configured environments.

In this comprehensive guide, we'll walk you through deploying a Node.js app on AWS Lightsail. You'll learn to set up an Ubuntu VPS, install necessary tools like Node.js, Git, PM2, and Nginx, and configure everything for a production-ready deployment.

Node.js applications can be deployed on various hosting solutions, from shared hosting to dedicated VPS. While shared hosting is cost-effective, dedicated VPS like AWS Lightsail offers better security, performance, and control over your environment.

Prerequisites

To follow this tutorial, you'll need:

  • An AWS account with Lightsail access
  • A basic Node.js application (e.g., an Express.js server)
  • Basic knowledge of Linux commands and SSH
  • A domain name (optional, for production setup)

If you haven't created a Lightsail instance yet, log in to your AWS console, navigate to Lightsail, and create a new Ubuntu instance. Note down the public IP and SSH key.


What is AWS Lightsail?

AWS Lightsail is a simplified cloud service that provides developers with a quick and easy way to launch virtual private servers (VPS) with pre-configured environments. It's ideal for developers who want to get started quickly without the complexity of managing infrastructure.

Benefits of AWS Lightsail

  • Easy to use with a simple interface
  • Pre-configured environments for quick deployment
  • Cost-effective for small to medium-sized applications
  • Scalable resources as your application grows

Minimum server requirements

To run a Node.js application on AWS Lightsail, ensure your instance meets the following minimum requirements:

  • 2 vCPUs
  • 1 GB RAM
  • 20 GB SSD storage

Dual Stack vs IPv6

AWS Lightsail instances support both IPv4 and IPv6 addressing. Dual-stack support allows your application to communicate over both protocols, ensuring compatibility with a wider range of clients and services.


Install Node JS on Ubuntu

Node.js is the runtime for running JavaScript on the server, and npm is the package manager used to install dependencies.

sudo apt update
curl -sL https://deb.nodesource.com/setup_24.x | sudo -E bash -
sudo apt-get install -y nodejs

Verify the installation by checking the versions:

node -v
npm -v

You should see the installed versions of Node.js and npm.


Install GIT

Git is a version control system that allows you to manage and track changes to your codebase. To install Git on Ubuntu, run the following commands:

sudo apt install git
git -v

Create Folder and Setup App

Now, create a directory for your Node.js application and set up your project files. You can either clone your repository from Git or manually upload your files.

mkdir nodeApp
cd nodeApp

If your code is in a Git repository, clone it:

git clone https://github.com/yourusername/your-repo.git .

Install the dependencies:

npm install

Test the application locally to ensure it runs:

npm start

If your app starts on a port (e.g., 3000), you should see it running. Press Ctrl+C to stop it for now.



Install PM2

PM2 is a process manager for Node.js applications. It allows you to manage and keep your application running continuously.

sudo npm install -g pm2

Start your Node.js application with PM2:

pm2 start app.js

Check the status of your application:

pm2 status

To ensure PM2 restarts your application on server reboot, run:

pm2 startup

This will generate a command. Run that command to set up the startup script. Finally, save the PM2 process list:

pm2 save

Install and Configure Nginx

Nginx is a web server that can also be used as a reverse proxy, load balancer, and HTTP cache. To install Nginx on Ubuntu, run the following commands:

sudo apt install nginx

Check and configure the firewall to allow HTTP traffic

sudo ufw app list

Allow firewall to pass ssh

sudo ufw allow ssh

Enable firewall

sudo ufw enable

Restart Nginx

sudo systemctl stop nginx
sudo systemctl start nginx

or

sudo systemctl restart nginx

Update Domain in Nginx

Edit the Nginx configuration file to route traffic from your domain to your Node.js app:

sudo nano /etc/nginx/sites-available/your_domain.com
sudo nano /etc/nginx/nginx.conf

Update localhost on nginx

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

  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;

    try_files $uri $uri/ =404;
  }

Test Nginx

sudo nginx -t

Restart Nginx

sudo systemctl restart nginx

Allow Nginx for http

sudo ufw allow 'Nginx HTTP'

Check ufw status

sudo ufw status

Add SSL Certificate

To add an SSL certificate to your Nginx server, you can use Let's Encrypt to obtain a free SSL certificate.

Install Certbot

sudo apt install certbot python3-certbot-nginx

Obtain SSL Certificate

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Follow the prompts to complete the SSL certificate installation. Certbot will automatically configure Nginx to use the new SSL certificate.

Update Nginx Configuration

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

Ensure your Nginx configuration includes the following lines to redirect HTTP to HTTPS:


server {
    listen 80;
    server_name your_domain.com www.your_domain.com;
    return 301 https://$host$request_uri;
}

Restart Nginx

sudo systemctl restart nginx

Test SSL Configuration

To test ssl working, visit your domain using https://your_domain.com

You should see a secure connection in the browser, indicating that your SSL certificate is properly installed and configured.

If you encounter any issues, check the Nginx error logs in /var/log/nginx/error.log.


Conclusion

Congratulations! You've successfully deployed your Node.js application on AWS Lightsail. Your app is now running with PM2 for process management and Nginx as a reverse proxy, ensuring high availability and performance.

For production environments, consider additional steps like setting up SSL certificates with Let's Encrypt, configuring environment variables securely, and implementing monitoring tools. Regularly update your dependencies and monitor server resources.

If you encounter issues, check the PM2 logs with pm2 logs and Nginx error logs in /var/log/nginx/error.log.