HOWTO run a flask app under apache

In an effort to switch my php based web sites to python, here are the steps required to make a python based web app available under an apache server, using wsgi.

under /var/www/hello:

hello <-- folder with flask app hello/venv <-- virtualenv with all dependencies installed hello.wsgi <-- file with following contents:

#!/usr/bin/python
activate_this = '/var/www/hello/hello/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/hello/")

from hello import app as application
application.secret_key = 'GENERATED_SECRET_CHANGE_AND_DO_NOT_SHOW_TO_ANYONE'

Howto generate a secret (from flask documentation):

python -c 'import os; print(os.urandom(16))'

And then in the apache site configuration the following will make the app active:

WSGIDaemonProcess flaskapp user=www-data group=www-data threads=5 home=/var/www/hello/hello
WSGIScriptAlias /hello /var/www/hello/hello.wsgi


WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Allow from all

Alias /hello/static /var/www/hello/hello/static

Order allow,deny
Allow from all

And that should make the site available under the path /hello

This entry was posted in Programming. Bookmark the permalink.