Home / Tutorials / Raspberry Pi Tutorial / Building a Django Webserver on Raspberry Pi: A Step-by-Step Guide
pcbway

Building a Django Webserver on Raspberry Pi: A Step-by-Step Guide

Django, a high-level Python web framework, can be a perfect choice for building dynamic web applications. Combining this with the Raspberry Pi—a versatile, compact computer—enables developers to deploy a robust and affordable web server. This guide walks you through setting up a Django web server on a Raspberry Pi.

Prerequisites

  • Raspberry Pi:
    • A Raspberry Pi 3, 4, or newer.
    • Raspbian OS (preferably Raspberry Pi OS Lite or Desktop) installed.
  • Hardware:
    • A microSD card (8GB or more).
    • Internet connection (Wi-Fi or Ethernet).
  • Software:
    • Python 3.7 or newer (pre-installed on Raspberry Pi OS).
    • Basic understanding of Linux and Python.

Step 1: Update Your Raspberry Pi

Before installing Django, ensure your Raspberry Pi is up to date:

sudo apt update
sudo apt upgrade -y

Step 2: Install Python and Required Tools

Django requires Python 3 and pip (Python’s package manager). These are typically pre-installed, but verify their presence:

python3 --version
pip3 --version

If missing, install them:

sudo apt install python3 python3-pip -y

Install venv to create isolated Python environments:

sudo apt install python3-venv -y

Step 3: Set Up a Virtual Environment

It’s best to run Django in a virtual environment to avoid conflicts with system-wide packages.

  1. Create a directory for your project:
  2. mkdir ~/django_project
  3. cd ~/django_project
  4. Create and activate the virtual environment:
  5. python3 -m venv env
  6. source env/bin/activate

You’ll notice your shell prompt changes to indicate the environment is active.

Step 4: Install Django

With the virtual environment active, install Django using pip:

pip install django

Verify the installation:

django-admin --version

Step 5: Start a Django Project

Create a new Django project:

django-admin startproject mysite
cd mysite

Next, run the development server to ensure everything works:

python manage.py runserver 0.0.0.0:8000

After this, visit http://<Raspberry_Pi_IP>:8000 in a browser to see the Django welcome page.

Step 6: Configure Django for Deployment

Update settings.py:

To allow external connections, open the settings.py using nano or vim. Look for this line:

ALLOWED_HOSTS = ['<Raspberry_Pi_IP>', 'localhost']

Set up static files:

STATIC_ROOT = BASE_DIR / 'staticfiles'

Run the following to collect static files:

python manage.py collectstatic

Step 7: Install and Configure Gunicorn

Gunicorn is a Python WSGI server that helps deploy Django applications. To install, run:

pip install gunicorn

After this, test gunicorn it with your project:

gunicorn mysite.wsgi:application --bind 0.0.0.0:8000

Step 8: Set Up Nginx as a Reverse Proxy

Nginx will serve as a reverse proxy for Gunicorn. Install Nginx using:

sudo apt install nginx -y

Then configure Nginx for your Django project. For this, we create a new configuration file:

sudo nano /etc/nginx/sites-available/mysite

Then we write the following to the configuration file:

server {
    listen 80;
    server_name <Raspberry_Pi_IP>;

    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;
    }

    location /static/ {
        alias /home/pi/django_project/mysite/staticfiles/;
    }
}

Next, enable the configuration:

sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 9: Enable Gunicorn to Start at Boot

Create a systemd service file for Gunicorn:

sudo nano /etc/systemd/system/gunicorn.service

Add the following:

[Unit]

Description=gunicorn daemon for Django
After=network.target

[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/django_project/mysite
ExecStart=/home/pi/django_project/env/bin/gunicorn --workers 3 --bind unix:/home/pi/django_project/mysite.sock mysite.wsgi:application

[Install]
WantedBy=multi-user.target
Start and enable the service:
sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Step 10: Test the Deployment

Visit http://<Raspberry_Pi_IP> in a browser. You should see your Django application served by Nginx and Gunicorn.

Conclusion

You’ve successfully set up a Django web server on a Raspberry Pi! This setup is perfect for small-scale projects, educational purposes, or prototyping IoT applications. For production environments, consider securing your server with HTTPS using tools like Let’s Encrypt.

Feel free to extend your application with features or integrate it with sensors and GPIO for an IoT-powered web solution.

 

Check Also

Automating Video Uploads to TikTok Using Raspberry Pi

The Raspberry Pi, a versatile and affordable single-board computer, has gained immense popularity among hobbyists, …

Index