hosting_with_gunicorn - dwilson2547/wiki_demo GitHub Wiki
Let’s go step by step on how to host a Flask application, starting simple and then moving to production-ready setups.
- 1. Basics: Running Locally
- 2. Hosting Options
- 3. Production Setup (Gunicorn + Nginx on Linux)
- 4. Quick Hosting with Heroku (Example PaaS)
- 5. Using Docker (Portable Option)
When you first create a Flask app (app.py
for example):
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
Run it with:
python app.py
- Flask will start a development server on
http://127.0.0.1:5000/
. - Great for development, but not safe or scalable for production.
You have several ways to host Flask depending on your needs:
- Run Flask on your own server or computer.
- Use Gunicorn or uWSGI as a production-ready WSGI server.
- Optionally put Nginx or Apache in front as a reverse proxy.
-
Platform-as-a-Service (PaaS):
- Heroku, PythonAnywhere, Render, Railway → Easiest deployment.
-
Infrastructure-as-a-Service (IaaS):
- AWS EC2, Google Cloud VM, Azure VM → More control but requires setup.
-
Containerized Deployment:
- Use Docker + Kubernetes (EKS, GKE, AKS) for large-scale apps.
Here’s a common setup:
-
Install Gunicorn
pip install gunicorn
-
Run Flask with Gunicorn
gunicorn -w 4 app:app
-
-w 4
→ 4 worker processes. -
app:app
→app.py
file andapp
Flask object.
This runs on
http://127.0.0.1:8000
. -
-
Install and Configure Nginx
-
Nginx acts as a reverse proxy: receives traffic on port
80
/443
and forwards to Gunicorn. -
Example
/etc/nginx/sites-available/flask_app
:server { listen 80; server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
-
Enable config and restart Nginx:
sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled sudo systemctl restart nginx
-
-
Run Gunicorn as a Service (Optional) Use
systemd
so it restarts automatically on reboot.
-
Install Heroku CLI.
-
Create a
Procfile
:web: gunicorn app:app
-
Push code:
git init heroku create git add . git commit -m "Initial commit" git push heroku master
-
Open your app:
heroku open
-
Create a
Dockerfile
:FROM python:3.10 WORKDIR /app COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
-
Build & run:
docker build -t flask-app . docker run -p 5000:5000 flask-app
-
Push to Docker Hub, deploy anywhere (AWS ECS, Kubernetes, etc.).
✅ Summary:
- Development:
flask run
(not for production). - Production: Use Gunicorn/uWSGI + Nginx.
- Easy hosting: Use Heroku, Render, or PythonAnywhere.
- Scalable hosting: Use Docker + Cloud VM/Kubernetes.
Do you want me to show you a step-by-step guide with commands for deploying Flask on Ubuntu with Gunicorn + Nginx (most common real-world method)?