hosting_with_cherrypy - dwilson2547/wiki_demo GitHub Wiki

Great question! ๐Ÿš€ Normally, Flask apps are served with Gunicorn or uWSGI, but you can also host Flask using CherryPy because CherryPy includes a robust, production-grade WSGI server.

Hereโ€™s a detailed breakdown:


๐Ÿ”น Why CherryPy?

  • CherryPy can act as a standalone WSGI HTTP server.
  • Itโ€™s lightweight, fast, and production-ready (unlike Flaskโ€™s built-in dev server).
  • You can run your Flask app directly on it, without needing Nginx (though you can still use Nginx as a reverse proxy if desired).

๐Ÿ”น Step 1: Install Flask + CherryPy

pip install flask cherrypy

๐Ÿ”น Step 2: Basic Flask App (app.py)

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from Flask on CherryPy!"

๐Ÿ”น Step 3: Run Flask with CherryPy

Create a server.py:

import cherrypy
from app import app  # Import your Flask app

if __name__ == "__main__":
    cherrypy.tree.graft(app, "/")  # Mount Flask app at root "/"
    cherrypy.config.update({
        "server.socket_host": "0.0.0.0",   # Accessible externally
        "server.socket_port": 8080,        # Run on port 8080
        "engine.autoreload.on": True,      # Restart on code changes (like Flask debug)
    })
    cherrypy.engine.start()
    cherrypy.engine.block()

Run:

python server.py

Now visit: http://localhost:8080


๐Ÿ”น Step 4: Running in Production

  • Detach CherryPy as a background service:

    nohup python server.py &
  • Or set it up as a systemd service (for auto-restart on reboot).

Example /etc/systemd/system/flask_cherrypy.service:

[Unit]
Description=Flask app with CherryPy
After=network.target

[Service]
User=www-data
WorkingDirectory=/path/to/your/app
ExecStart=/usr/bin/python3 /path/to/your/app/server.py
Restart=always

[Install]
WantedBy=multi-user.target

Then:

sudo systemctl daemon-reload
sudo systemctl enable flask_cherrypy
sudo systemctl start flask_cherrypy

๐Ÿ”น Step 5 (Optional): Add Nginx in Front

For SSL, load balancing, or serving static files efficiently, put Nginx in front and proxy to CherryPy:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

โœ… Summary

  • Flaskโ€™s dev server = good for development only.
  • CherryPy = production-ready WSGI server to host Flask apps directly.
  • Can run standalone or behind Nginx for more security & SSL.

Would you like me to also show you how to serve multiple Flask apps on different routes (e.g., /api, /dashboard) under the same CherryPy server? Thatโ€™s a neat trick CherryPy makes really easy.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ