Wednesday, May 02, 2012

Recently I have been doing some development with Python/Django. I have been working with Alan Viars of Videntity and working with his Restcat open source activity tracker.

One thing I wanted to do was to get my own copy of Restcat running on my bluehost account. I have used Bluehost for many years. It has proven to be a reliable shared hosting environment for me offering good value for money.

One of the challenges with getting Python running with Apache is that in a shared hosting environment you can't necessarily get at the Apache Configuration file or the virtual server configuration file. This leaves you with a more limited subset of options that can be applied using the .htaccess file.

After a lot of experimentation I finally succeeded in getting Python running and I thought I would document the setup here. I am doing this for two reasons:

1. Other may find this useful in tackling a similar challenge.

2. I do not profess to be an expert in Apache configuration so I am sure that other experts can point out improvements to the configuration I ended up with.

First, let's start with an outline of the configuration.

The web documents used by apache are stored in {User Account}/public_html

I didn't want my Python files in the web document folders, so I created a Python Apps folder. I placed this in {User Account}/pyapps

I followed the instructions from Bluehost to install Python in my user account. I also used VirtualEnv to be able to isolate the setup for individual python apps. This resulted in python being installed in {User Account}/python. Python 2.7.2 is the version that is installed.

I think the .htaccess file still needs optimizing in order to point to the location of the Static files and admin media files before getting to the python/fcgi stage but this gets the app going.

What is in .htaccess?

AddHandler tells Apache to use the FCGI daemon when it encounters a file with a .fcgi extension.

I will leave experts in Apache configuration to explain most of this file but the last two lines are important.

The ErrorDocument 500 line will write something to the browser if the .htaccess file is executed but the application doesn't run (in this case the mysite.fcgi file)

The RewriteRule basically passes anything that hasn't been executed up to this point and passes it to the mysite.fcgi file. It is the mysite.fcgi file that will execute the python application. So let's look at that next.

Getting this file working was troublesome because you can get different errors between Apache executing the file and running the file directly from the SSH command line. In the end this mostly seems to come down to getting all the necessary components added to the PATH before Apache tries to execute them. Once again I am sure there are Python and Shell experts out there that can tell me far more efficient ways to accomplish this, but at least this worked!

Next we add a series of directories to the PATH so that this routine can find all the components it needs. For me this involved pointing to the python installation, site-packages - so django and other components could be found, the flup egg file (Bluehost recommends flup as a fcgi to wsgi interface and who am I to argue). I also needed to point to the python-omhe installation which is needed by RESTCat.

I also added the location of my Python apps and RESTCat specifically to the PATH.

I then pointed to the RESTCat settings file.

The final step was to load the fastCGI interface from the django libraries and execute.

When you put all these pieces together you can point at a sub-domain that is hosted on an Apache Shared Host have it execute a python application.

If I want to add another Python Application I can create an additional folder in the {USER ACCOUNT}/public_html/apps directory and tweak the mysite.fcgi file in that folder to point to a different python application.

I welcome any suggestions to optimize this setup, in particular to see if it is possible to write an Apache rule in .htaccess to point to static files in a directory located elsewhere in the Apache document root, rather than being a sub-directory of the apps/{python app} folder in the web document area.

About Me

Consultant, Project Recovery Specialist, Strategist, and Blogger focusing on the convergence of Cloud, Mobile and Social Media. with a particular emphasis on the implications for the Health Care sector and Patient Engagement.