Deploy Frontend and Backend on the Same Server

This article is for deploying django and react applications on same server.
Table of Contents
- AWS EC2 Instance Setup
- Attaching Elastic IP
- Adding Domain on Cloudflare
- Necessary Packages Installation on AWS EC2
- Frontend Deployment
- Backend Django Deployment
- Nginx Setup
- Additional Notes
Note📢⚠️❗🔔: I recommend watching the accompanying video for detailed instructions and using the commands and file texts from the video. This article serves as a supplementary guide.
- Frontend Repo: GitHub Repository
- Backend Repo: GitHub Repository
YouTube Complete Tutorial Video 📺:
Part 1. AWS EC2 Instance Setup
Before starting, ensure you have an AWS EC2 instance up and running. Make sure it’s configured with sufficient resources based on your project needs.
Part2. Attaching Elastic IP
To make your EC2 instance accessible from the internet, attach an Elastic IP:
- Go to the AWS EC2 Dashboard.
- Navigate to “Elastic IPs” and allocate a new IP.
- Associate this IP with your EC2 instance.
Part 3. Adding Domain on Cloudflare
- Go to Cloudflare and log in.
- Add your domain to Cloudflare and follow the setup instructions.
- Update your DNS settings to point to the Elastic IP of your EC2 instance.
Part 4. Necessary Packages Installation on AWS EC2
Update the Package Manager
sudo apt-get update -yInstall Node.js and Nginx
- Install Node.js (Version 20)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs
- Install npm
sudo apt install npm -y
- Install Nginx
sudo apt install nginx -y
Part 5. Frontend Deployment
Step 1: Clone the ReactJS App to EC2
- For Public Repository:
git clone <YOUR-GIT-Repo>
- For Private Repository:
git clone <YOUR-GIT-Repo>
You will be prompted for your GitHub username and password (use a GitHub token if needed).
Step 2: Install Required Dependencies
cd <project-folder> npm install
Step 3: Create Production Build
npm run build
Step 4: Set Up Directory and Copy Build
sudo mkdir -p /var/www/vhosts/frontend/
sudo cp -R build/ /var/www/vhosts/frontend/
Part 6. Backend Django Deployment
Step 1: Install Python
sudo apt update
sudo apt install python3-pip python3-devStep 2: Create a Python Virtual Environment
sudo pip3 install virtualenv sudo apt install python3-virtualenv
- Clone the Django Project
git clone https://github.com/codewithmuh/backend-django.git
cd ~/backend-django- Create and Activate Virtual Environment
virtualenv venv source venv/bin/activate
- Install Dependencies
pip install -r requirements.txt
Step 3: Install Django and Gunicorn
pip install django gunicorn
Step 4: Configure Django Project
- Update ALLOWED_HOSTS in settings.py
- Run Migrations and Collect Static Files
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic- Update settings.py and urls.py
- Add whitenoise to INSTALLED_APPS and MIDDLEWARE.
- Ensure static files are properly served.
pip install whitenoise
Step 5: Configure Gunicorn
- Create Gunicorn Socket
sudo vim /etc/systemd/system/gunicorn.socket
Paste:
[Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target
- Create Gunicorn Service
sudo vim /etc/systemd/system/gunicorn.service
Paste:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/backend-django
ExecStart=/home/ubuntu/backend-django/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
blog.wsgi:application
[Install]
WantedBy=multi-user.target- Start and Enable Gunicorn
sudo systemctl start gunicorn.socket sudo systemctl enable gunicorn.socket
Part 7. Nginx Setup
Step 1: Remove Default Configuration
cd /etc/nginx/sites-enabled
sudo rm -f defaultStep 2: Configure Nginx as a Reverse Proxy
- Create Nginx Configuration File
sudo vim /etc/nginx/sites-available/nginx
Paste:
# Frontend Nginx Config
server {
listen 80;
server_name YOUR_DOMAIN.com;
location / {
autoindex on;
root /var/www/vhosts/frontend/build;
try_files $uri $uri/ /index.html;
}
}
# Backend Nginx Config
server {
listen 80;
server_name api.YOUR_DOMAIN.com;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location /staticfiles/ {
alias /home/ubuntu/backend-django/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
proxy_set_header X-Forwarded-Protocol $scheme;
}
client_max_body_size 134217728;
}- Activate Configuration
sudo ln -s /etc/nginx/sites-available/nginx /etc/nginx/sites-enabled/
Run this command to load a static file
$ sudo gpasswd -a www-data username
username in my case is ubuntu, which is mention in my nginx.
Restart nginx and allow the changes to take place.
- Restart Services
sudo systemctl restart nginx sudo systemctl restart gunicorn
Part 8. Additional Notes
If you encounter errors, review the logs and configurations. This guide provides a foundational setup, and further adjustments may be necessary based on specific project requirements.
Ensure all services are running smoothly:
- Check Nginx and gunicorn Status
sudo systemctl status nginx
sudo systemctl status gunicorn2. Check Error Logs
sudo tail -f /var/log/nginx/error.logWe Are Done. If you like my article, You can hit the clap button.