Running Bottle Python Micro-Framework on Dreamhost

I've been experimenting with various micro-frameworks for a few weeks now and the one that really stands out is Bottle. Bottle is a single file framework with no external dependencies, this is perfect for my needs and allows for a nice clean application. However, as I chose Dreamhost as my hosting provider (not my smartest moment), running python applications on their environment can be a headache as I found when trying to install Django.

The good news is it's pretty simple to set-up Bottle on Dreamhost and I'm documenting the process here for future reference.

  1. First, manage you domain in the Dreamhost web panel, setting PHP mode: PHP 5 (FASTCGI).
    Note: I know you can use Passenger for better performance but I was just trying to get the framework running.

  2. SSH into your domain and download the latest version of Bottle (0.64 as of this post)
    cd example.comwget http://pypi.python.org/packages/source/b/bottle/bottle-0.6.4.tar.gzmv bottle-0.6.4/bottle.py ./
  3. Create an index page what will be used as your main application. This uses Python 2.5's wsgiref so make use you path to python2.5 in your file header.
    vim index.py
    #!/usr/bin/python2.5import bottlefrom bottle import route@route('/')def index():    return 'Hello World!'if __name__ == '__main__':    from wsgiref.handlers import CGIHandler    CGIHandler().run(bottle.default_app())
  4. Make the Python script executable
    chmod +x index.py
  5. Add a .htaccess file to redirect URLs to your index.py application
    DirectoryIndex index.py<ifmodule mod_rewrite.c="">RewriteEngine onRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ /index.py/$1  [L]</ifmodule>
That should be all you need to get going. I've used the above method to get a test application up successfully but it's still early days. The Bottle tutorial is a useful starting point and Dreamhost's wiki page contains useful information about enviroment configuration.