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

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.

2. Hosting Options

You have several ways to host Flask depending on your needs:

(a) Local/On-Premises Hosting

  • 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.

(b) Cloud Hosting

  • 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.

3. Production Setup (Gunicorn + Nginx on Linux)

Here’s a common setup:

  1. Install Gunicorn

    pip install gunicorn
  2. Run Flask with Gunicorn

    gunicorn -w 4 app:app
    • -w 4 → 4 worker processes.
    • app:appapp.py file and app Flask object.

    This runs on http://127.0.0.1:8000.

  3. 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
  4. Run Gunicorn as a Service (Optional) Use systemd so it restarts automatically on reboot.


4. Quick Hosting with Heroku (Example PaaS)

  1. Install Heroku CLI.

  2. Create a Procfile:

    web: gunicorn app:app
    
  3. Push code:

    git init
    heroku create
    git add .
    git commit -m "Initial commit"
    git push heroku master
  4. Open your app:

    heroku open

5. Using Docker (Portable Option)

  1. 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"]
  2. Build & run:

    docker build -t flask-app .
    docker run -p 5000:5000 flask-app
  3. 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)?

⚠️ **GitHub.com Fallback** ⚠️