How to Deploy Django Application: Step-by-Step Guide
To deploy a Django application, set up a production-ready web server like
Gunicorn behind a reverse proxy such as Nginx, configure your settings.py for production, and use a database like PostgreSQL. Then, collect static files with python manage.py collectstatic and run migrations before starting the server.Syntax
Deploying a Django app involves these main steps:
- Configure settings.py: Set
DEBUG = False, add allowed hosts inALLOWED_HOSTS, and configure static files. - Collect static files: Run
python manage.py collectstaticto gather CSS, JS, and images. - Apply migrations: Run
python manage.py migrateto update the database schema. - Run a WSGI server: Use
gunicornto serve the app. - Set up a reverse proxy: Use
nginxto forward requests to Gunicorn and serve static files.
python
DEBUG = False ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com'] # Static files settings STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / 'staticfiles' # Run commands in terminal python manage.py collectstatic python manage.py migrate # Run Gunicorn server gunicorn yourproject.wsgi:application # Example nginx config snippet server { listen 80; server_name yourdomain.com www.yourdomain.com; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /path/to/your/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; } }
Example
This example shows a minimal deployment setup using Gunicorn and Nginx on a Linux server.
It demonstrates configuring settings.py, collecting static files, running migrations, and starting Gunicorn.
bash
# settings.py snippet DEBUG = False ALLOWED_HOSTS = ['example.com'] STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / 'staticfiles' # Terminal commands python manage.py collectstatic python manage.py migrate # Start Gunicorn server gunicorn myproject.wsgi:application --bind 0.0.0.0:8000 # Nginx server block example server { listen 80; server_name example.com; location /static/ { alias /home/user/myproject/staticfiles/; } 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; } }
Output
Collecting static files...
0 static files copied.
Applying migrations...
Operations to perform:
Apply all migrations.
Starting Gunicorn server...
[2024-06-01 12:00:00 +0000] [12345] [INFO] Starting gunicorn 20.1.0
[2024-06-01 12:00:00 +0000] [12345] [INFO] Listening at: http://0.0.0.0:8000 (12345)
[2024-06-01 12:00:00 +0000] [12345] [INFO] Using worker: sync
[2024-06-01 12:00:00 +0000] [12348] [INFO] Booting worker with pid: 12348
Common Pitfalls
Common mistakes when deploying Django apps include:
- Leaving
DEBUG = Truein production, which exposes sensitive info. - Not setting
ALLOWED_HOSTS, causing server errors. - Forgetting to run
collectstatic, so static files don’t load. - Not configuring the database properly for production.
- Running the development server (
runserver) instead of a production WSGI server like Gunicorn.
python
# Wrong: DEBUG True in production DEBUG = True # Right: DEBUG False and ALLOWED_HOSTS set DEBUG = False ALLOWED_HOSTS = ['yourdomain.com']
Quick Reference
Summary tips for deploying Django:
- Always set
DEBUG = Falseand configureALLOWED_HOSTS. - Use
gunicornas the WSGI server behindnginx. - Run
python manage.py collectstaticbefore starting the server. - Apply all migrations with
python manage.py migrate. - Secure your app with HTTPS (use Certbot for free SSL certificates).
Key Takeaways
Set DEBUG to False and configure ALLOWED_HOSTS for security.
Use Gunicorn as the production server and Nginx as a reverse proxy.
Always run collectstatic and migrate commands before starting the server.
Never use Django's development server in production.
Secure your deployment with HTTPS and proper server configuration.