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
3 min readMar 12, 2025
Steps to Sign Up on AWS:
- Visit AWS Signup.
- Click on Create an AWS Account.
- Enter your email address and set a password.
- Provide your personal or business details.
- Enter your credit card details (AWS offers a free tier but requires a valid payment method).
- Verify your identity via phone number and OTP.
- Choose a support plan (the free tier is usually sufficient).
- Complete the signup and log in to your AWS console.
2. Create and Launch an EC2 Instance
Steps to Create an AWS EC2 Instance:
- Log in to the AWS Management Console.
- Navigate to EC2 Dashboard.
- Click Launch Instance.
- Select an Amazon Machine Image (AMI): Choose Ubuntu 22.04 or the latest LTS version.
- Select an Instance Type: Choose
t2.medium
(ort2.micro
for free-tier testing). - Configure Instance:
- Number of instances: 1
- Leave other settings as default
- Add Storage: Default 8GB is enough, but increase if required.
- Configure Security Group:
- Allow SSH (Port 22) for remote access.
- Allow HTTP (Port 80) and HTTPS (Port 443) for web access.
- 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).
- Click Launch Instance.
Connecting to Your EC2 Instance via SSH:
- Open a terminal and navigate to the directory where the key pair (
your-key.pem
) is saved. - Change permissions for security:
chmod 400 your-key.pem
- 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
- Clone your repository:
git clone
https://github.com/wakeupcoders/aws-deployment-sample
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!