Deploy NodeJS App on AWS
Written By: Avinash Malhotra
Updated on
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.
Verify the installation by checking the versions:
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:
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.
If your code is in a Git repository, clone it:
Install the dependencies:
Test the application locally to ensure it runs:
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.
Start your Node.js application with PM2:
Check the status of your application:
To ensure PM2 restarts your application on server reboot, run:
This will generate a command. Run that command to set up the startup script. Finally, save the PM2 process list:
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:
Check and configure the firewall to allow HTTP traffic
Allow firewall to pass ssh
Enable firewall
Restart Nginx
or
Update Domain in Nginx
Edit the Nginx configuration file to route traffic from your domain to your Node.js app:
Update localhost on nginx
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
Restart Nginx
Allow Nginx for http
Check 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.