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.
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.
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
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
Obtain SSL Certificate
Follow the prompts to complete the SSL certificate installation. Certbot will automatically configure Nginx to use the new SSL certificate.
Update Nginx Configuration
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
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.