Deploying a web application involves making it accessible to users over the internet. Flask and Django apps can be deployed on various platforms, including Heroku, AWS, DigitalOcean, and PythonAnywhere. This guide covers step-by-step deployment for both Flask and Django.
1. Deployment Preparation
Before deploying, ensure:
Your application runs locally without errors
You have a requirements.txt file listing dependencies
You use a virtual environment to manage dependencies
You have configured environment variables correctly
2. Deployment of Flask Apps
Flask applications can be deployed using Gunicorn (for running production servers) and Nginx or Apache (for reverse proxy).
2.1 Creating a Flask App
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask Deployment!"
if __name__ == "__main__":
app.run(debug=True)
2.2 Install Dependencies
Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate # On Windows, use venv\Scripts\activate
pip install flask gunicorn
Freeze dependencies into a requirements.txt
file:
pip freeze > requirements.txt
2.3 Deploy Flask App on Heroku
Step 1: Install Heroku CLI
Download and install Heroku CLI from Heroku Official Site.
Step 2: Login to Heroku
heroku login
Step 3: Create a Procfile
Heroku needs a Procfile
to define the entry point:
web: gunicorn app:app
Step 4: Initialize Git and Deploy
git init
heroku create flask-app-name
git add .
git commit -m "Initial commit"
git push heroku main
Step 5: Open the App
heroku open
2.4 Deploy Flask App on AWS EC2
- Launch an EC2 instance (Ubuntu)
- Install Python, Flask, and Gunicorn:
sudo apt update && sudo apt install python3-pip pip install flask gunicorn
- Run the Flask App:
gunicorn --bind 0.0.0.0:8000 app:app
- Use Nginx as a Reverse Proxy (Optional for production).
3. Deployment of Django Apps
Django apps require Gunicorn and Nginx for production deployment.
3.1 Creating a Django App
Install Django and create a project:
pip install django gunicorn
django-admin startproject myproject
cd myproject
Run migrations:
python manage.py migrate
Run the server:
python manage.py runserver
3.2 Deploy Django on Heroku
Step 1: Install Heroku CLI
pip install django-heroku gunicorn
Step 2: Modify settings.py
Add:
import django_heroku
django_heroku.settings(locals())
Step 3: Create Procfile
web: gunicorn myproject.wsgi
Step 4: Initialize Git and Deploy
git init
heroku create django-app-name
git add .
git commit -m "Initial commit"
git push heroku main
Step 5: Open the App
bashCopyEditheroku open
3.3 Deploy Django on AWS EC2
- Launch EC2 instance
- Install dependencies:
sudo apt update && sudo apt install python3-pip pip install django gunicorn
- Run Gunicorn:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi
- Configure Nginx as a Reverse Proxy.
4. Using Docker for Deployment
Both Flask and Django apps can be deployed using Docker.
4.1 Create a Dockerfile
For Flask:
FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
For Django:
FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["gunicorn", "-b", "0.0.0.0:8000", "myproject.wsgi"]
4.2 Build and Run the Docker Container
docker build -t myapp .
docker run -p 8000:8000 myapp