🌐 Self-Hosting Django with Nginx + Cloudflare Tunnel: Configure a Production-Ready Server


If you've built a Django app and are itching to take full control—why not host it yourself? Whether it’s a server or an old laptop collecting dust or a Raspberry Pi humming silently in your drawer, you can turn it into a full-fledged server. No monthly bills. No vendor lock-in. Just you, your machine, and the internet.
In this guide, we’ll:
✅ Set up a Django project running with Uvicorn
✅ Install and configure Nginx as a reverse proxy
✅ Secure it using a Cloudflare Tunnel (no need to mess with routers or public IPs)
✅ Learn to Config server, local system or repurpose any machine (even a Raspberry Pi) as your personal web server
🚧 Prerequisites
Basic Linux knowledge
A Django project (or start one with
django-admin startproject
)A Cloudflare account
A domain (optional but recommended)
Server ,Old PC, laptop, or Raspberry Pi or Any Linux (Ubuntu recommended) System
🔧 Step 1: Get Django Running Locally
First, set up your Python virtual environment and run Django via Uvicorn:
python3 -m venv venv
source venv/bin/activate
pip install django uvicorn
Now run your app:
uvicorn yourproject.asgi:application --host 127.0.0.1 --port 8000
🚀 Step 2: Install & Configure Nginx
Install Nginx:
sudo apt update
sudo apt install nginx
Create a config file:
sudo nano /etc/nginx/sites-available/myproject
Paste this:
server {
listen 80;
server_name yourdomain.com;
location / {
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;
}
location /static/ {
alias /home/youruser/yourproject/static/;
}
location /media/ {
alias /home/youruser/yourproject/media/;
}
}
Activate it:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
🔒 Step 3: Setup Cloudflare Tunnel (No Port Forwarding Needed!)
Install Cloudflared:
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared-linux-amd64.deb
Authenticate:
cloudflared tunnel login
Create the tunnel:
cloudflared tunnel create mytunnel
Find the credentials file here:
~/.cloudflared/<tunnel-ID>.json
Now, configure it:
nano ~/.cloudflared/config.yml
Paste and edit:
tunnel: <TUNNEL-ID>
credentials-file: /home/youruser/.cloudflared/<TUNNEL-ID>.json
ingress:
- hostname: yourdomain.com
service: http://localhost:80
- service: http_status:404
Route your domain:
cloudflared tunnel route dns mytunnel yourdomain.com
Run the tunnel:
cloudflared tunnel run mytunnel
📁 Step 4: Django Static & Media Files
In settings.py
:
ALLOWED_HOSTS = ['yourdomain.com', '127.0.0.1', 'localhost']
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Run:
python manage.py collectstatic
⚙️ (Optional) Step 5: Run Uvicorn on Boot with Systemd
Create the service:
sudo nano /etc/systemd/system/myproject.service
Paste:
[Unit]
Description=Uvicorn instance to serve Django
After=network.target
[Service]
User=youruser
Group=www-data
WorkingDirectory=/home/youruser/yourproject
Environment="PATH=/home/youruser/yourproject/venv/bin"
ExecStart=/home/youruser/yourproject/venv/bin/uvicorn yourproject.asgi:application --host 127.0.0.1 --port 8000
[Install]
WantedBy=multi-user.target
Start it:
sudo systemctl daemon-reexec
sudo systemctl start myproject
sudo systemctl enable myproject
✅ Final Checklist
Django project running on Uvicorn
Nginx reverse proxy to Uvicorn
Static and media paths handled
Cloudflare Tunnel pointing to Nginx (port 80)
Optional daemon service with Systemd
Own server or Old machine recycled into something awesome
🌍 You now own your corner of the Internet.
You're not relying on a PaaS. You’re running your server, your way. Whether it's for learning, showcasing projects, or building secure internal tools — self-hosting with Cloudflare and Nginx gives you full control and privacy.
Subscribe to my newsletter
Read articles from Rijo S Lal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Rijo S Lal
Rijo S Lal
Driven by a passion for machine learning, I've crafted a robust skill set through self-directed learning and hands-on experience. My expertise encompasses algorithm development, deep learning models, and data-driven solutions aimed at solving complex problems. I thrive in dynamic tech environments where creativity meets technical proficiency. My journey as a self-learner has equipped me with the ability to quickly adapt to new challenges and technologies, ensuring continuous growth and innovation. I am committed to leveraging my skills to make meaningful contributions to cutting-edge projects and teams focused on advancing AI