Mischiefblog
I make apps for other people

A simple FastCGI bottle.py Python web application for Dreamhost

Posted by Chris Jones
On August 18th, 2012 at 16:55

Permalink | Trackback | Links In |

Comments Off on A simple FastCGI bottle.py Python web application for Dreamhost
Posted in General

Here are instructions for setting up a simple bottle.py Python web application running on Dreamhost (shared) with virtualenv.

  1. Set up virtualenv
    1. cd mydomain.com
    2. virtualenv myapp
    3. source myapp/bin/activate
    4. pip install -U bottle
  2. Fix the directory permissions
    1. chmod -R 755 myapp
  3. Set up .htaccess
    1. cd myapp
    2. vi .htaccess

      Options +ExecCGI
      AddHandler cgi-script .py
      DirectoryIndex index.py/
      <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_URI} !^/favicon.ico$
      RewriteCond %{REQUEST_URI} !^/static/(.*)$
      RewriteCond %{REQUEST_URI} !^(/.*)+index.py/
      RewriteRule ^(.*)$ /myapp/index.py/$1 [PT]
      </IfModule>
  4. vi index.py

    #!/usr/bin/python
     
    import os
    import sys
     
    libpath = os.getcwd() + os.sep
    sys.path.extend([libpath + 'lib/python2.6', libpath + 'bin'])
     
    from bottle import request, route
    import bottle
    from wsgiref.handlers import CGIHandler
     
    @route('/')
    def index():
        return "OK"
     
    @route('/hello/:name')
    def hello(name):
        return "Hello %s" % (name)
     
    if __name__ == '__main__':
        CGIHandler().run(bottle.default_app())
  5. Make index.py executable with chmod +x index.py
  6. Test by pointing your browser to http://mydomain.com/myapp/

Comments are closed.