MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Deploying to Heroku0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
10.1Deploying to Heroku10.2Static & Media Files10.3Environment Variables & Secrets
HomeTutorialsDjango for Backend DevelopmentDeploying to Heroku
Static & Media Files

Deploying to Heroku

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Deploying Django with Gunicorn and Docker

Deploying a production Django backend requires moving away from the single-threaded runserver to a robust, multi-process WSGI server. Modern enterprise deployments package Django into containerized Docker images orchestrated behind Gunicorn (Green Unicorn) and Nginx, deployed across cloud platforms (Heroku, AWS ECS, Google Cloud Run, or DigitalOcean).


1. The Production Architecture Stack

1User (Browser) ──► HTTPS (Port 443) ──► Nginx Reverse Proxy
2 │
3 ▼ (Reverse Proxy / UNIX Socket)
4 Gunicorn WSGI Master Process
5 ├── Worker 1 (Python Process)
6 ├── Worker 2 (Python Process)
7 └── Worker 3 (Python Process)
8 │
9 ▼
10 PostgreSQL Database
  • Nginx: Handles SSL termination, serves static/media files, and load balances requests.
  • Gunicorn: Manages a pool of pre-forked worker processes executing Python code.

2. Production Dependencies

1# Production WSGI server
2pip install gunicorn
3 
4# PostgreSQL database driver
5pip install psycopg2-binary
6 
7# Database URL parser for cloud platforms (DATABASE_URL)
8pip install dj-database-url
9 
10# Static file serving for single-container cloud environments
11pip install whitenoise

3. Configuring WhiteNoise for Static Assets

On platforms like Heroku or single-container Docker setups without a separate Nginx container, WhiteNoise allows Django to serve its own static files efficiently with gzip/brotli compression and caching:

1# config/settings.py
2MIDDLEWARE = [
3 "django.middleware.security.SecurityMiddleware",
4 "whitenoise.middleware.WhiteNoiseMiddleware", # Place directly after SecurityMiddleware!
5 # ...
6]
7 
8# Enable WhiteNoise compression and caching
9STATIC_ROOT = BASE_DIR / "staticfiles"
10STATIC_URL = "/static/"
11STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

4. Production Dockerfile

1# Use lightweight official Python image
2FROM python:3.11-slim
3 
4# Set environment variables
5ENV PYTHONDONTWRITEBYTECODE=1
6ENV PYTHONUNBUFFERED=1
7 
8WORKDIR /app
9 
10# Install system dependencies (for psycopg2 and build tools)
11RUN apt-get update && apt-get install -y --no-install-recommends build-essential libpq-dev && rm -rf /var/lib/apt/lists/*
12 
13# Install Python packages
14COPY requirements.txt .
15RUN pip install --no-cache-dir -r requirements.txt
16 
17# Copy application source code
18COPY . .
19 
20# Collect static files at build time
21RUN python manage.py collectstatic --noinput
22 
23# Expose production port
24EXPOSE 8000
25 
26# Run Gunicorn with 3 pre-forked worker processes
27CMD ["gunicorn", "--workers=3", "--bind=0.0.0.0:8000", "config.wsgi:application"]

5. Heroku Deployment via Procfile

For deploying to Heroku or Railway, specify process commands in a Procfile:

1release: python manage.py migrate
2web: gunicorn config.wsgi:application --workers 3 --log-file -

The release phase runs database migrations automatically before traffic is switched to the newly deployed container, preventing downtime.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why is Gunicorn required to serve Django applications in production instead of manage.py runserver?
Question 2
What does WhiteNoise enable for containerized Django deployments?
Question 3
How do you calculate the recommended number of Gunicorn worker processes?
Question 4
What does the command python manage.py collectstatic do?
Question 5
In a Heroku or Railway Procfile, what does the release phase do?
Next Lesson

Static & Media Files

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Django Debug ToolbarStatic & Media Files

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext