Before deploying, install necessary packages:
Ensure your project has the following structure
Ensure your project has the following structure
/flask_app
│── app.py
│── requirements.txt
│── Procfile (For Heroku)
│── wsgi.py (For Gunicorn)
│── templates/
│── static/
requirements.txt
Generate dependencies file:
pip freeze > requirements.txt
wsgi.py
A WSGI file ensures Flask runs correctly with Gunicorn:
from app import app
if __name__ == "__main__":
app.run()
Step 4: Run Gunicorn Locally
gunicorn --bind 0.0.0.0:5000 wsgi:app
heroku login
Procfile
for HerokuCreate a file named Procfile (without any extension):
web: gunicorn wsgi:app
Step 7: Deploy to Heroku
git init
git add .
git commit -m "Initial Commit"
heroku create flask-app-name
git push heroku master
ssh -i your-key.pem ubuntu@your-ec2-ip
sudo apt update && sudo apt install python3-pip
pip install flask gunicorn
Step 10: Start Gunicorn
gunicorn --bind 0.0.0.0:8000 wsgi:app
ssh root@your-droplet-ip
sudo apt update && sudo apt install python3-pip
pip install flask gunicorn
gunicorn --bind 0.0.0.0:8000 wsgi:app
In this guide, we covered:
✅ Using Gunicorn to run Flask in production.
✅ Deploying Flask on Heroku with a Procfile
.
✅ Deploying on AWS (EC2 Instance) with Gunicorn.
✅ Deploying on DigitalOcean with a Linux droplet.
Now, your Flask app is ready for real-world usage! 🚀