Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How do you deploy Python web applications to production? Python web applications को production में कैसे deploy करते हैं?

Answer
# Using Gunicorn (WSGI server)
# Install: pip install gunicorn
# Run: gunicorn -w 4 -b 0.0.0.0:8000 app:app
# or for Django: gunicorn myproject.wsgi:application

# Nginx configuration (reverse proxy)
server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://localhost: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;
    }
}

# Supervisor (process manager)
[program:myapp]
command=gunicorn -w 4 -b 0.0.0.0:8000 app:app
directory=/home/user/myapp
autostart=true
autorestart=true

# Environment variables (.env)
DATABASE_URL=postgresql://user:password@localhost:5432/db
SECRET_KEY=your-secret-key
DEBUG=False

# Using python-dotenv
from dotenv import load_dotenv
import os

load_dotenv()
db_url = os.getenv('DATABASE_URL')

# Docker deployment
# Dockerfile
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ['gunicorn', '-b', '0.0.0.0:8000', 'app:app']

# docker-compose.yml
version: '3'
services:
  web:
    build: .
    ports:
      - '8000:8000'
    environment:
      - DATABASE_URL=postgresql://...
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=password

# Heroku deployment
# Procfile
web: gunicorn myproject.wsgi:application

# AWS deployment
# Using Elastic Beanstalk
eb init myapp
eb create production
eb deploy

# PythonAnywhere deployment
# Upload files via web interface
# Configure WSGI file
# Set up virtual environment
Deployment options:
1. Gunicorn: WSGI server
   gunicorn app:app

2. Nginx: Reverse proxy
   proxy_pass to Gunicorn

3. Supervisor: Process manager
   Keeps app running

4. Docker: Containerization
   Dockerfile + docker-compose

5. Heroku: Platform as Service
   Simple deployment

6. AWS: Elastic Beanstalk
   Scalable infrastructure

7. PythonAnywhere:
   Python hosting service

Was this answer clear?