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
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.
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
- Log into AWS Console and navigate to EC2 Dashboard.
- Launch a new instance with Ubuntu Server 22.04 LTS (e.g.,
t2.microort3.small). - 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.
- Navigate to Elastic IPs in EC2 and allocate a new IPv4 address.
- Associate the Elastic IP with your newly created instance.
- In your Cloudflare Dashboard, add an
Arecord pointing your domain (e.g.,example.com) to the Elastic IP address with the proxy status set to Proxied (Orange Cloud). - Set SSL/TLS encryption mode to Full or Full (Strict).
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 nodejsClone 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 buildCreate an Nginx server block to serve React static files and route /api/ traffic to the Django backend:
sudo nano /etc/nginx/sites-available/fullstack-appPaste 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 nginxFor complete instructions on setting up Python, Django, Gunicorn, and Systemd background services on the same host, refer to the paired repository: 👉 codewithmuh/backend-django
- 📺 YouTube Channel: CodeWithMuh
- 🌐 Connect on LinkedIn: Muhammad Rashid
This project is licensed under the MIT License.
