Posts Tagged ‘wsgi’

Deploying Flask to Apache2 on Ubuntu 13.04

09/07/2013

I had some trouble deploying a Flask app with Apache2, so here’s a minimum working configuration.

Virtual environment setup

virtualenv appname
cd appname
source bin/activate
pip install flask
mkdir -p src/appname www
echo "site.addsitedir(os.path.join(base, 'src'))" >> bin/activate_this.py
echo "from .appname import *" > src/appname/__init__.py
cat > src/appname/appname.wsgi <<EOF
activate_this = '/path/to/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from appname import app as application
EOF
cat > src/appname/appname.py <<EOF
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Success?"

if __name__ == '__main__':
    app.run()
EOF

Virtual host configuration

<VirtualHost *:80>
    ServerName host.name
    ServerAlias www.host.name
    DocumentRoot /path/to/virtualenv/www
    <Directory /path/to/virtualenv/www>
        Order allow,deny
        Allow from all
    </Directory>
    WSGIScriptAlias /connote /path/to/virtualenv/src/connote/connote.wsgi
    WSGIDaemonProcess host.name processes=2 threads=15
    WSGIProcessGroup host.name
    <Directory /path/to/virtualenv/src/connote>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog /path/to/virtualenv/var/error.log
    LogLevel debug
</VirtualHost>