Webfaction is a Python friendly hosting with affordable pricing and tons of useful features.
To deploy a Flask application on Webfaction you should follow these steps:
webapps/yourapplication/ |-- apache2 | |-- bin | |-- conf | |-- lib | |-- logs | `-- modules `-- htdocs
easy_install:
foo@bar:~$ easy_install-2.6 Flask
Note that Webfaction gives you different easy_install executables: use the one that meets the Python version you choose when the application was created.
webapps/yourapp/index.py delete the content and add the following line:
from yourapp import app as application
webapps/yourapp/apache2/conf/httpd.conf. At the bottom of the file add the following lines changing yourapp and yourusername according to your setup:
WSGIPythonPath /home/yourusername/webapps/yourapp/htdocs/
#If you do not specify the following directive the app *will* work but you will
#see index.py in the path of all URLs
WSGIScriptAlias / /home/yourusername/webapps/yourapp/htdocs/index.py
<Directory /home/yourusername/webapps/yourapp/htdocs/>
AddHandler wsgi-script .py
RewriteEngine on
RewriteBase /
WSGIScriptReloading On
</Directory>
If you have choosed / as the mout point for your application you are done.
If you mounted the application somewhere else (i.e. /blog) there is some additional work to do.
You need to write a WSGI middleware that prefixes SCRIPT_NAME with that prefix otherwise the url_for function will not be able to create the correct URLs for you (the following snippets is kindly provided by Armin himself).
class WebFactionMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = '/yourapp'
return self.app(environ, start_response)
app.wsgi_app = WebFactionMiddleware(app.wsgi_app)
You can put this snippet in the application's __init__.py.
Happy Flasking!
This snippet by esaurito can be used freely for anything you like. Consider it public domain.
Comments
Inconsistent by dAnjou on 2012-07-01 @ 20:04
I don't use webfaction but I tried to help someone setting up his app and I noticed that the author used "yourapplication" and "yourapp" in several paths. Is that supposed to be the same? If yes, where is "index.py" in the directory tree?
Wrong location of index.py by codecool on 2012-07-10 @ 12:17
index.py is located inside /webapps/youapplication/htdocs.
Comment by Nick on 2013-01-19 @ 23:51
Thank you, your writeup was a great help.
Webfaction seem to be using the WSGIDaemonProcess directive now, so the WSGIPythonPath line was not needed for me. However, I DID need WSGIScriptAlias for the url rewrite to work. I also omitted the WSGIScriptReloading line, since it seems to be On by default.
flask is lowercase for easy_install by CS777 on 2013-02-17 @ 00:20
foo@bar:$ easy_install-2.6 flask
Updated install guide by Ryan Sanden on 2013-03-13 @ 03:25
An updated WebFaction install guide for Flask is now available here:
http://community.webfaction.com/questions/12718/installing-flask
This also automates most of the process into copy-paste commands.