DEV Community

Cover image for How to Run a Django Server: Development, Testing, and Deployment
Tima
Tima

Posted on

How to Run a Django Server: Development, Testing, and Deployment

Running a Django server isn’t always the same—it depends on whether you’re developing locally, testing deployment readiness, or running on real hosting/VPS. This guide walks through the different approaches.


1. Normal Server Startup for Development

The simplest way to run Django locally is:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

By default, it runs on port 8000. You can specify another port:

python manage.py runserver 7000
Enter fullscreen mode Exit fullscreen mode

👉 Changing ports can be useful when integrating with tools like VSCode Live Server or when running multiple apps side by side.


2. Testing Deployment Readiness

When you switch to production mode (DEBUG=False and ALLOWED_HOSTS set), Django stops serving static files. To simulate production but still serve static files locally, use:

python manage.py runserver --insecure
Enter fullscreen mode Exit fullscreen mode

This lets you test custom error pages (404, 500, etc.) without setting up Apache or Nginx yet.


3. Running on Shared Hosting

Django apps don’t run with runserver in production. Instead, they use WSGI or ASGI interfaces to communicate with web servers.

  • WSGI: synchronous communication (classic Django setup).
  • ASGI: asynchronous communication (supports async features).

A typical wsgi.py might look like:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Website.settings")
application = get_wsgi_application()
Enter fullscreen mode Exit fullscreen mode

4. Running on a VPS

On a VPS, you’ll configure a web server (Apache or Nginx) to handle requests and proxy them to Django via a socket or port.

Example Nginx config (HTTP):

server {
    listen 80;
    server_name example.com;

    location /static {
        root /home/site/example.com;
    }

    location /media {
        root /home/site/example.com;
    }

    location / {
        proxy_set_header Host example.com;
        proxy_pass http://unix:/tmp/example.com.socket;
    }
}
Enter fullscreen mode Exit fullscreen mode

👉 In 2025, you should use HTTPS (port 443) with SSL certificates from Let’s Encrypt:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location /static {
        root /home/user/example.com;
    }

    location /media {
        root /home/user/example.com;
    }

    location / {
        proxy_set_header Host example.com;
        proxy_pass http://unix:/tmp/example.com.socket;
    }
}
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

  • Use runserver for local development.
  • Use --insecure for testing production‑like conditions.
  • Use WSGI/ASGI for shared hosting.
  • Configure Nginx/Apache for VPS deployment with HTTPS.

Treat server setup as part of your Django architecture—it’s not just about running code, but about ensuring reliability, scalability, and security.


Tags

#django #python #webdev #deployment #devops


Discussion

How do you usually run Django in production?

Do you prefer WSGI or ASGI for modern projects?


Further Reading

Top comments (0)