Deploy Node.js on EC2: Docker and Nginx Configuration Explained

Deploy Nodejs on EC2 with Docker and Nginx configuration. This guide covers Docker setup, configuring Nginx, and secure your Node.js app with SSL.

Prerequisites

Before you get started, make sure you have the following ready:

  1. An AWS EC2 instance running Ubuntu.
  2. Your Node.js application hosted on GitHub.

    Step 1: Node.js Application Structure

    First, ensure your Node.js project has this structure:

    project-root/
    ├── controller
    ├── middleware
    ├── models
    ├── routes
    ├── docker-compose.yml
    ├── Dockerfile
    ├── .env
    ├── package.json
    ├── README.md
    └── server.js

    Step 2: Setting Up Docker in Your Node.js Application

    Create a Dockerfile in your project root:

    FROM node:16-alpine

    WORKDIR /app

    COPY package.json .

    RUN npm install

    COPY . .

    EXPOSE 3000

    CMD ["node", "server.js"]

    Create a docker-compose.yml file in your project root:

    version: "3.9"
    services:
    backend:
    build: .
    ports:
    - "3000:3000"
    env_file:
    - .env

    Set up a .env file with your MongoDB URL and other variables:

    PORT=3000
    MONGO_URI=<YOUR_MONGODB_URL>

    Step 3: Connecting to Your EC2 Instance

    Connect to your EC2 instance via SSH:

    ssh -i /path/to/your-key.pem ubuntu@your-ec2-instance-ip

    Step 4: Updating and Installing Dependencies

    Update and install necessary dependencies:

    sudo apt-get update
    sudo apt-get install -y git

    Step 5: Installing Docker and Docker Compose

    Install Docker:

    sudo apt-get install -y docker.io
    sudo systemctl start docker
    sudo systemctl enable docker

    Install Docker Compose:

    sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    docker-compose --version

    Step 6: Cloning Your GitHub Repository

    Clone your GitHub repository and navigate to your project:

    git clone https://github.com/your-gitub-username/your-private-repo.git
    cd your-private-repo

    Step 7: Building and Running Your Docker Containers

    Ensure your Dockerfile and docker-compose.yml are set up correctly. Build and run your Docker containers:

    sudo docker-compose up --build -d

    Step 8: Verifying the Deployment

    Check the status of your Docker containers:

    sudo docker ps

    Your Node.js application should be running. Access it via your EC2 instance’s IP address or domain.

    Step 9: Setting Up Nginx as a Reverse Proxy

    Install Nginx:

    sudo apt-get install -y nginx

    Remove the default Nginx configuration:

    sudo rm /etc/nginx/sites-enabled/default

    Create or update your Nginx configuration file:

    sudo nano /etc/nginx/sites-available/<your-app>

    Add this configuration:

    server {
    listen 80;
    server_name <YOUR_DOMAIN_NAME>;

    location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    }
    }

    Enable the new configuration:

    sudo ln -s /etc/nginx/sites-available/<your-app> /etc/nginx/sites-enabled/

    Test the Nginx configuration:

    sudo nginx -t

    If successful, restart Nginx:

    sudo systemctl restart nginx

    Step 10: Additional Configuration

    1. Check Docker Container: Ensure your Docker container is running and listening on port 3000:
    sudo docker ps

    2. Firewall/Security Group: Ensure your EC2 instance’s security group allows HTTP traffic on port 80:

    sudo ufw allow 'Nginx Full'

    3. DNS Settings: Confirm your domain points correctly to your EC2 instance’s IP address.

    Configure security group rules for ports 80 and 3000.

    Check your setup by visiting http://<your-domain> in your browser; it should forward to your Node.js backend.

    Step 11: Securing Your Backend with SSL

    Install Certbot

    sudo snap install --classic certbot

    Allow Permission

    sudo ufw allow 443

    Obtain Your SSL Certificate

    sudo certbot certonly --standalone -d your_domain

    Automatically Renew Your SSL Certificate

    sudo certbot renew --dry-run

    Don’t forgot to Configure security group rules for ports 443.

    By following these steps, your Node.js application will be up and running on AWS EC2 using Docker. If you have questions or need further assistance, feel free to reach out!

    One Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *