Web Servers

  • Configure production web servers like Gunicorn and Nginx for Django apps.
  • What are Web Servers?

    A web server is software that serves web content to users via HTTP/HTTPS.

    • In Django deployment, two main components handle requests:

      1. Gunicorn → Python WSGI server (runs Django app)

      2. Nginx → Reverse proxy & static file server

    Gunicorn

    What is Gunicorn?

    • WSGI HTTP server for Python

    • Serves Django applications to the web

    • Handles multiple requests efficiently

Install Gunicorn

pip install gunicorn

Run Django with Gunicorn

gunicorn projectname.wsgi:application --bind 0.0.0.0:8000
  • Code Description

    • projectname.wsgi:application → Entry point for Django app

    • --bind 0.0.0.0:8000 → Bind to server IP and port

    • Gunicorn handles Python/Django requests

    • Does not serve static files efficiently

Gunicorn with Workers

gunicorn projectname.wsgi:application --workers 3 --bind 0.0.0.0:8000
  • Description

    • --workers 3 → 3 concurrent workers for handling requests

    • Improves performance under load

    Nginx

    What is Nginx?

    • High-performance reverse proxy server

    • Serves static files and forwards dynamic requests to Gunicorn

    • Handles SSL, caching, load balancing

    Nginx Workflow with Django

    [Client Browser] --> [Nginx] --> [Gunicorn] --> [Django App] --> [Database]

    • Nginx serves static / media files directly

    • Nginx forwards API / dynamic requests to Gunicorn

Install Nginx

sudo apt update
sudo apt install nginx

Nginx Configuration for Django

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

Add:

server {
    listen 80;
    server_name yourdomain.com;

    location /static/ {
        root /home/ubuntu/project;
    }

    location /media/ {
        root /home/ubuntu/project;
    }

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

Enable Nginx Site

sudo ln -s /etc/nginx/sites-available/project /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
  • Description

    • Nginx now serves static files and forwards dynamic requests to Gunicorn

    • proxy_set_header → Sends correct client info to Django

Static & Media Handling with Nginx

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/project/staticfiles/'

MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/ubuntu/project/media/'
  • Collect static files:

    python manage.py collectstatic

    Nginx serves files from STATIC_ROOT & MEDIA_ROOT

    Run Gunicorn as System Service

Create Gunicorn systemd Service

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

Add:

[Unit]
Description=Gunicorn Django Project
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/project
ExecStart=/home/ubuntu/project/venv/bin/gunicorn projectname.wsgi:application \
          --bind 127.0.0.1:8000 \
          --workers 3

[Install]
WantedBy=multi-user.target

Commands to Enable Service

sudo systemctl start project
sudo systemctl enable project
sudo systemctl status project
  • Description

    • Gunicorn runs as background service

    • Starts automatically on server reboot

    • Handles production traffic reliably