ASOwiki : HOWTODjangoFastCGI

Welcome :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register

Using Django on ASO with FastCGI


I've managed to get django working on my tiny account, mostly following this document.

Installing django


We are going to work in /tmp to workaround some resitrictions with disk quotas.

$ mkdir /tmp/$USER
$ cd /tmp/$USER
$ wget http://media.djangoproject.com/releases/1.0/Django-1.0.tar.gz && tar xzf Django-1.0.tar.gz
$ wget http://www.saddi.com/software/flup/dist/flup-1.0.1.tar.gz && tar xzf flup-1.0.1.tar.gz
$ cd Django-1.0
$ python setup.py install --prefix $HOME/local/django-1.0
$ cd ..
$ cd flup-1.0.1
$ mkdir -p $HOME/local/flup-1.0.1/lib/python2.4/site-packages
$ cp -r flup $HOME/local/flup-1.0.1/lib/python2.4/site-packages


We can now remove temporary files

$ cd $HOME
$ rm -ri /tmp/$USER


Setting up paths


Now we have to tell python where to get the packages we have just installed. To do so, we create the "~/.pythonrc.py" script:

import sys
home = '/home/<MYUSER>'
sys.path += (home+'/local/django-1.0/lib/python2.4/site-packages',
	         home+'/local/flup-1.0.1/lib/python2.4/site-packages',
	         home+'/django-projects')


Now start the python interpreter and try the following:

>>> import django
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named django
>>> import flup
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named flup
>>> import user #this executes the ~/.pythonrc.py file
>>> import django
>>> import flup


Setup django dispatcher


We can now try to set up our first django site. After having uploaded a project into "~/django-projects" (i.e.: "~/django-projects/myproject") we need to create the script that will handle requests to django. Create a new file named "~/public_html/myproject.fcgi" and put the following content in it:

#!/usr/bin/env python
import os, user

# Switch to the directory of your project. (Optional.)
# os.chdir("/home/__MYUSER__/django-projects/myproject")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")


Give it the right permissions and execute it

$ chmod 755 ~/public_html/myproject.fcgi
$ ~/public_html/myproject.fcgi


If all goes right you should already be able to reach this installation at http://mydomain/myproject.fcgi/

But you probably want good-looking urls so add the following content to your "~/public_html/.htaccess" file:

AddHandler fastcgi-script .fcgi

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myproject.fcgi/$1 [QSA,L]


And if you cam reach django at http://mydomain/ you're done!

Testing FastCGI


If for some reason your django installation does not work, you can try to debug if FastCGI alone is working. Create a "~/public_html/hello.fcgi" file with the following content

#!/usr/bin/env python
import user

from flup.server.fcgi import WSGIServer
def test_app(environ, start_response):
	start_response('200 OK', [('Content-Type', 'text/plain')])
	yield 'Hello, world!\n'

WSGIServer(test_app).run()


Make it executable and run it from the commandline:

$ chmod 755 ~/public_html/hello.fcgi
$ ~/public_html/hello.fcgi


and you should see some output with "Hello, World!" in it. You can now point your browser to http://mydomain/hello.fcgi and you should see "Hello, world!"

Final words


I've choosed to use a separate directory for each package to ease upgrade of the single packages.

rm -ri $HOME/local/django-1.0
tar xzf Django-x.y.z.tar.gz
cd Django-x.y.z
python setup.py install --prefix $HOME/local/django-x.y.z
# remember to edit "~/.pythonrc.py" accordingly

With a tiny account as the one I have you probably want to use a media site that points to another location, but this is out of the scope of this article.
Have fun with django!

There are 7 comments on this page. [Display comments]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.0889 seconds