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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay