DEV Community

Sumit Negi
Sumit Negi

Posted on

AWS EC2 Node.js deployment

Step 1: Launching an EC2 Instance

  1. Go to EC2 and click on Launch Instance.

  2. Add a name for your instance.

  3. Select Ubuntu from Quick Start.

  4. Choose the desired instance type.

  5. For connecting to a Key pair, use a pre-existing key or create a new one.

  6. In the Network setting, add a security group. A security group acts as a firewall, controlling both incoming and outgoing traffic from your instance.

  7. Storage configuration is fine as gp2.

Image 1

Image 2

After this connect to your EC2 instance install node and create a mini project:

Installation guide:

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04

Create a new project:

  1. npm init -y
  2. add a new file index.js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
Enter fullscreen mode Exit fullscreen mode
  1. npm i pm2 express
  2. pm2 start index.js
  3. pm2 save

Caddy
Step 1: Install Caddy
https://caddyserver.com/docs/install

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo vim /etc/caddy/Caddyfile
:80 {
    reverse_proxy localhost:3000
}
sudo systemctl restart caddy
Enter fullscreen mode Exit fullscreen mode

Update the Caddyfile to use your domain name and enable HTTPS.

sudo vim /etc/caddy/Caddyfile

mydomain.com {
    reverse_proxy localhost:3000
}
sudo systemctl restart caddy
Enter fullscreen mode Exit fullscreen mode

Top comments (0)