Skip to content

About

React Single Page Application deployed alongside Django Backend on single AWS EC2 instance with Nginx & Cloudflare

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

Repository files navigation

Fullstack Deployment Guide: React Frontend on AWS EC2 with Nginx & Cloudflare

GitHub Release License: MIT GitHub Stars GitHub Forks React AWS EC2 Nginx Cloudflare Backend: Django YouTube: @codewithmuh

Complete deployment architecture and production guide for hosting a React frontend alongside a Django REST Framework backend on a single AWS EC2 server behind Nginx, Gunicorn, and Cloudflare.

Architecture • EC2 Setup • Elastic IP & DNS • Frontend Build • Nginx Configuration • Paired Backend


Deployment Architecture Banner

Overview

This repository hosts the React Single Page Application (SPA) designed to run collaboratively with the backend-django REST API on a single AWS EC2 instance. This unified single-host architecture minimizes infrastructure costs while maximizing performance by serving optimized static frontend bundles directly through Nginx and reverse-proxying API traffic to Gunicorn.


System Architecture

flowchart TD
    subgraph Clients["Users & Browsers"]
        Browser["User Web Browser"]
    end

    subgraph Edge["Edge Network & CDN"]
        Cloudflare["Cloudflare (Universal SSL + DNS Proxy)"]
    end

    subgraph AWSCloud["AWS Infrastructure (EC2 Ubuntu 22.04 LTS)"]
        EIP["AWS Elastic IP (Static Public IPv4)"]
        
        subgraph ServerHost["Single Host Nginx Server"]
            Nginx["Nginx Web Server / Reverse Proxy (Port 80 / 443)"]
            StaticFiles["React Production Build (/var/www/frontend/build)"]
            
            subgraph BackendService["Django Gunicorn Daemon"]
                Socket["Unix Domain Socket / Localhost:8000"]
                Django["Django REST Framework API"]
            end
        end
    end

    Browser -->|HTTPS Request| Cloudflare
    Cloudflare -->|Proxied Traffic| EIP
    EIP --> Nginx
    Nginx -->|Route / (Static HTML/JS)| StaticFiles
    Nginx -->|Route /api/* & /admin/*| Socket
    Socket --> Django
Loading

Step-by-Step Production Deployment Guide

1. AWS EC2 Instance Setup

  1. Log into AWS Console and navigate to EC2 Dashboard.
  2. Launch a new instance with Ubuntu Server 22.04 LTS (e.g., t2.micro or t3.small).
  3. Configure Security Group Rules:
    • SSH (22): Your personal IP only.
    • HTTP (80): Anywhere (0.0.0.0/0) or Cloudflare IP ranges.
    • HTTPS (443): Anywhere (0.0.0.0/0) or Cloudflare IP ranges.

2. Attaching Elastic IP & Cloudflare DNS

  1. Navigate to Elastic IPs in EC2 and allocate a new IPv4 address.
  2. Associate the Elastic IP with your newly created instance.
  3. In your Cloudflare Dashboard, add an A record pointing your domain (e.g., example.com) to the Elastic IP address with the proxy status set to Proxied (Orange Cloud).
  4. Set SSL/TLS encryption mode to Full or Full (Strict).

3. Server Package Installation

SSH into your server and install required dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx git curl build-essential

# Install Node.js (LTS version)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

4. Frontend React Build & Deployment

Clone the frontend repository and compile production static assets:

# Clone to web root directory
sudo mkdir -p /var/www/frontend
sudo chown -R $USER:$USER /var/www/frontend
git clone https://github.com/codewithmuh/frontend-react.git /var/www/frontend

# Install dependencies and build
cd /var/www/frontend
npm install
npm run build

5. Nginx Unified Reverse Proxy Configuration

Create an Nginx server block to serve React static files and route /api/ traffic to the Django backend:

sudo nano /etc/nginx/sites-available/fullstack-app

Paste the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    # React Static Assets
    root /var/www/frontend/build;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    # Django Backend API Proxy
    location /api/ {
        proxy_pass http://127.0.0.1:8000;
        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;
    }

    # Django Admin Panel
    location /admin/ {
        proxy_pass http://127.0.0.1:8000;
        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;
    }

    # Django Static / Media Files
    location /static/ {
        alias /var/www/backend/staticfiles/;
    }

    location /media/ {
        alias /var/www/backend/media/;
    }
}

Enable the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/fullstack-app /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx

Paired Backend Repository

For complete instructions on setting up Python, Django, Gunicorn, and Systemd background services on the same host, refer to the paired repository: 👉 codewithmuh/backend-django


Community & Video Walkthrough


License

This project is licensed under the MIT License.

About

React Single Page Application deployed alongside Django Backend on single AWS EC2 instance with Nginx & Cloudflare

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages