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.


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
sudo apt install nodejs
sudo apt install npm

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

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.