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?
- ๐น Step 1: Install Flask + CherryPy
- ๐น Step 2: Basic Flask App (
app.py
) - ๐น Step 3: Run Flask with CherryPy
- ๐น Step 4: Running in Production
- ๐น Step 5 (Optional): Add Nginx in Front
- 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).
pip install flask cherrypy
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello from Flask on 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
-
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
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.