Steps to Deploy a Node.js App to AWS/Azure Using PM2, NGINX, and SSL from Let’s Encrypt

1. Sign Up for an AWS Account

--

Steps to Sign Up on AWS:

  1. Visit AWS Signup.
  2. Click on Create an AWS Account.
  3. Enter your email address and set a password.
  4. Provide your personal or business details.
  5. Enter your credit card details (AWS offers a free tier but requires a valid payment method).
  6. Verify your identity via phone number and OTP.
  7. Choose a support plan (the free tier is usually sufficient).
  8. Complete the signup and log in to your AWS console.

2. Create and Launch an EC2 Instance

Steps to Create an AWS EC2 Instance:

  1. Log in to the AWS Management Console.
  2. Navigate to EC2 Dashboard.
  3. Click Launch Instance.
  4. Select an Amazon Machine Image (AMI): Choose Ubuntu 22.04 or the latest LTS version.
  5. Select an Instance Type: Choose t2.medium (or t2.micro for free-tier testing).
  6. Configure Instance:
  • Number of instances: 1
  • Leave other settings as default
  1. Add Storage: Default 8GB is enough, but increase if required.
  2. Configure Security Group:
  • Allow SSH (Port 22) for remote access.
  • Allow HTTP (Port 80) and HTTPS (Port 443) for web access.
  1. Create or Select an Existing Key Pair:
  • Choose “Create a new key pair” (or use an existing one).
  • Download the .pem file and keep it secure (needed for SSH access).
  1. Click Launch Instance.

Connecting to Your EC2 Instance via SSH:

  1. Open a terminal and navigate to the directory where the key pair (your-key.pem) is saved.
  2. Change permissions for security:
chmod 400 your-key.pem
  1. Connect to your instance:
ssh -i your-key.pem ubuntu@your-server-ip
  • Replace your-server-ip with the Public IPv4 Address of your EC2 instance.

3. Install Node.js and NPM

  • Run the following commands to install Node.js:
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install nodejs
  • Verify installation:
node --version

4. Clone Your Project from GitHub

5. Install Dependencies and Start the App Using PM2

  • Install PM2 globally:
  • sudo npm i pm2 -g
  • Start your application:
  • pm2 start index.js
  • Other PM2 commands:
  • pm2 show app
  • pm2 status
  • pm2 restart app
  • pm2 stop app
  • pm2 logs # Show log stream
  • pm2 flush # Clear logs
  • Ensure the app starts after a reboot:
  • pm2 startup ubuntu

6. Set Up a Firewall

  • Enable the firewall and allow necessary ports:
  • sudo ufw enable
  • sudo ufw status
  • sudo ufw allow ssh # Port 22
  • sudo ufw allow http # Port 80
  • sudo ufw allow https # Port 443

7. Install and Configure NGINX

  • Install NGINX:
  • sudo apt install nginx
  • Edit the default site configuration:
  • sudo nano /etc/nginx/sites-available/default
  • Add the following configuration inside the server block:
  • server_name yourdomain.com www.yourdomain.com;
  • location /
  • { proxy_pass http://localhost:8001; # Adjust to 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; }
  • Test the NGINX configuration:
  • sudo nginx -t
  • Restart NGINX:
  • sudo nginx -s reload

8. Secure the Site with SSL Using Let’s Encrypt

  • Install Certbot for NGINX:
  • sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install python3-certbot-nginx
  • Generate and install SSL certificates:
  • sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • Test auto-renewal of the SSL certificate:
  • certbot renew --dry-run

Summary

  • Sign Up on AWS: Create an AWS account and log in.
  • Launch an EC2 Instance: Select Ubuntu, configure security groups, and set up SSH.
  • Connect via SSH: Use the .pem key file and connect using SSH.
  • Install Node.js & NPM: Install the required runtime.
  • Clone Project: Pull the repository from GitHub
  • Install Dependencies & Start App: Use PM2 to manage the Node.js app.
  • Configure Firewall: Open necessary ports for traffic.
  • Set Up NGINX: Use it as a reverse proxy.
  • Enable SSL: Use Certbot to secure your site.

Deployment Complete 🎉

Your Node.js application is now running with PM2, accessible through NGINX, and secured with SSL from Let’s Encrypt!

--

--

Wakeupcoders - Digital Marketing & Web App Company
Wakeupcoders - Digital Marketing & Web App Company

Written by Wakeupcoders - Digital Marketing & Web App Company

We make your business smarter and broader through the power of the internet. Researcher | Web developer | Internet of things | AI | www.wakeupcoders.com

No responses yet