Django with CERN Web Services
Is it at all possible to set up Django for an AFS web site with CERN Web Services? Yes it is. The next question – how? – is what this posts aims at answering.
If putting together a dynamic web site with PHP is easy, the same cannot be said of Python; while you can easily inject PHP code into HTML, that's something you can't normally do with Python, where the custom is more to use web frameworks and templates to generate complete pages.
Setting up a web framework in a web server where you don't have total access, as is the case with shared-hosting providers such as the CERN Web Services, generally means you have to fly blind until you reach your goal in having a working setup. I guess you can see this recipe as a flight plan.
Note that what follows applies for so-called AFS folder web sites. I seem to remember I've done something similar for web sites Centrally hosted on DFS but the approach is different.
1 Setting up a Django Project in Your Box
Loosely following the Django tutorial you need to:
- Create a project with e.g.
django-admin.py startproject foo
. - Create an application with e.g.
python manage.py startapp website
. - Add the application to the project by adding
'website'
toINSTALLED_APP
insettings.py
. - Edit URLs in
urls.py
. The first example is actually a good inspiration and you may want to use something along the lines ofurl(r'^.*$', 'website.views.home', name='home')
. You may also want to have any URLs call the same function to start with. Write the corresponding views to
website/views.py
:from django.http import HttpResponse def home(request): return HttpResponse("Hullo")
- I haven't seen it explicitly advised, but it may be wise to turn
DEBUG = False
insettings.py
for security reasons. Try the project by running
python manage.py runserver
and browsing http://127.0.0.1:8000.
2 Deploying Your Web App on AFS via LXPLUS
Since we can't seem to be able to run
runfastcgi()
with Python 2.6, better use Python 2.4. Get Django and flup.Technically, you need to make the destination directory read-accessible by the web server (
After doing anfs sa -dir python -acl webserver:afs read
). You might want to think about the security implications, though.export PYTHONPATH=~/python/lib/python2.4/site-packages
you need to fire uppython setup.py install --prefix ~/python
for both.- Copy your Django project somewhere outside
~/www
(security), e.g.~/projects
which, technically, should also be read-accessible by the web server (again,fs sa -dir projects -acl webserver:afs read
once you've pondered the security consequences). In the e.g.
~/www
directory which, technically, would need to be read-accessible by the web server (fs sa -dir www -acl webserver:afs read
– again, think about security), create an.htaccess
file containing the following:Note that you should omitRewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ cgi-bin/foo.fcgi/$1 [QSA,L]
AddHandler fastcgi-script .fcgi
, otherwise the request will hang forever, for some reason. Also note that we're going to dump thefcgi
file intocgi-bin
, just for convenience because scripts this directory are allowed to be executed by default, although you can set up other directories for this purpose in https://webservices.cern.ch.Create an executable
~/www/cgi-bin/foo.fcgi
file with the following lines:#! /usr/bin/env python import sys, os sys.path.insert(0, "../../python/lib/python2.4/site-packages") sys.path.insert(0, "../../projects/foo") sys.path.insert(0, "../../projects") os.environ['DJANGO_SETTINGS_MODULE'] = "foo.settings" from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded", daemonize="false")
Note that in this case the script is run from
~/cgi-bin
. I haven't found changing directory to be useful. It also turns out that different parts of Django import packages from different locations, hence the need to specify the twoprojects
andprojects/foo
paths.