HostGator.com » Support Portal

Django with fastcgi

Django can be installed on a shared environment if you use FastCGI with it.

Note: You will need the FLUP module for Python 2.4 to run FastCGI, and you will need the pysqlite2 module to run SQLite3.

In your public_html directory, add this to your file named .htaccess:

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ mysite.fcgi/$1 [QSA,L]

Then, create a small script that tells Apache how to spawn your FastCGI program. Create a file mysite.fcgi and place it in your Web directory, and be sure to make it executable:

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

sys.path.insert(0, "/home/USERNAME/django/")
sys.path.insert(0, "/home/USERNAME/django/projects")
sys.path.insert(0, "/home/USERNAME/django/projects/newproject")


# Switch to the directory of your project. (Optional.)
#os.chdir("/home/USERNAME/django/projects/newproject")

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

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

The paths are assuming that the following URL was used.
http://docs.google.com/Doc?id=dhhpr5xs_463522g


Article Comments

The above rewrite rule
RewriteRule ^(.+)$ mysite.fcgi/$1 [QSA,L]
would convert your entire site to being just a Django site - which may not be your intention.
Most likely you want one rewrite rule for your Django Admin and one for your Django project itself.

Going to the URL
http://your-site.com/my-own.fcgi
should result in something other than a page error.

You cannot test the FCGI script from a shell: it must run in the context of the web server to have its WSGI envrionement set.

Minimally if you run it in a shell from your web root, it should output the contents of the 500.html file in your project's admin templates.

Robert
In mycase the top of my public_html/.htaccess became

AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

and it now ends with

RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ curlgen.fcgi/$1 [L]
RewriteRule ^(macros/.*)$ curlgen.fcgi/$1 [L]

where curlgen.fcgi is the name of my FCGI script

That script is simply

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

# sys.path.insert(0, "/usr/lib/python2.4")
sys.path.insert(0, "/home/robert/django")
sys.path.insert(0, "/home/robert/django/projects")
sys.path.insert(0, "/home/robert/django/projects/curlgen")

# Switch to the directory of your project. (Optional.)
# os.chdir("/home/robert/django/projects/curlgen")

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

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

BECAUSE:
I am building a project in ./projects/curlgen

You will place your user name and your project name in place of my 'robert' and 'curlgen' and 'macros'

Be sure your FCGI script is set to 755 permissions so that it will execute.


If you have read our knowledge base and still have questions or need more information, please contact us via phone or live chat for immediate assistance.

Post Comment