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.
- 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. - 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 ./
- 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()) - Make the Python script executable
chmod +x index.py
- 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>