The goal of this post is to outline my process for setting up a Flask app on a WebFaction hosting account. I am sure there are better ways to do this, but this is the process that has worked for me so far. This guide is mostly for my own recollection, but perhaps it will be useful to someone else.
Creating the WebFaction application
In your WebFaction control panel, start by creating a new
mod_wsgi 3.3/Python 2.7 application and assigning it a corresponding domain and website. For the purposes of this guide, I will use
wfuser as my WebFaction username and
myflaskapp as the name of my newly created application.
Configuring the virtualenv
Next, we’ll want to setup virtualenv for our project’s python dependencies. SSH into your WebFaction server and navigate to our app’s directory.
$ cd ~/webapps/myflaskapp/
Assuming you already have virtualenv installed on WebFaction, we want to create a new virtualenv using the following command:
$ mkvirtualenv --no-site-packages myflaskapp -p python2.7
In my experience, it is important to include the
-p python2.7 flag. When I left this out, my virtualenv was created using Python 2.4 which prevented it from properly working.
Next, let’s edit the virtualenv’s postactivate script so that switching to it automatically places us in the app’s directory.
$ vim ~/.virtualenvs/myflaskapp/bin/postactivate
Edit the file so that it contains the following:
#!/bin/bash # This hook is run after this virtualenv is activated. cd /home/wfuser/webapps/myflaskapp/
Afterwards, import any requirements you’ve exported from your local machine’s virtualenv:
(myflaskapp)$ pip install -r requirements.txt
Setting up the Flask application
Now that the virtualenv is all squared away, let’s setup the actual Flask application.
Start by transferring your existing flask project into the
Next, backup and replace the index.py script automatically generated by WebFaction:
$ cd ~/webapps/myflaskapp/htdocs/ $ mv index.py index.py.original $ vim index.py
Your new index.py should include the following:
import os, sys, site #add virtualenv python libs site.addsitedir('/home/wfuser/.virtualenvs/myflaskapp/lib/python2.7/site-packages') # append the project path to the system path sys.path.insert(0, '/home/wfuser/.virtualenvs/myflaskapp/') sys.path.insert(0, '/home/wfuser/webapps/myflaskapp/myflaskapp') from app import app as application
This tells python to load any site-packages you’ve installed to your virtualenv as well as adds your project to the system path. The last line imports the flask
app variable needed by mod_wsgi. Depending on the structure of your flask project, this line will vary. My example assumes the following structure:
- myflaskapp/ - apache2/ - htdocs/ - myflaskapp/ - app/ - __init__.py <--- contains the actual app variable we wish to import - ... - ...
Next, we need to backup and edit the apache configuration file.
$ cd ~/webapps/myflaskapp/apache2/conf/ $ cp httpd.conf httpd.conf.original $ vim httpd.conf
Add the following to the end of the existing file:
WSGIPythonPath /home/wfuser/webapps/myflaskapp/htdocs/ # If you do not specify the next directive the app *will* work # but you will see index.py in the path of all subdir WSGIScriptAlias / /home/wfuser/webapps/myflaskapp/htdocs/index.py <Directory /home/wfuser/webapps/myflaskapp/htdocs/> AddHandler wsgi-script .py RewriteEngine on RewriteBase / WSGIScriptReloading On </Directory>
This will configure mod_wsgi to direct HTTP requests to
htdocs/index.py, which loads your Flask project and handles the request accordingly.
If you need to read the apache error log, it can be found at
~/logs/user/error_myflaskapp.log This comes in incredibly handy when debugging those awful 500 errors.