Django with fastcgi

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

Note: You will need the FLUP module and Django installed. VPS and Dedicated plans can use these instructions, but shared and reseller plans already have these modules installed.

Here we go!

First, we need to install the flup module and django. We can use easy_install to do that.

Run these commands...

easy_install flup
easy_install django

No easy_install on your server?

  1. Go to http://pypi.python.org/pypi/setuptools#files and download the appropriate egg for your version of Python, i.e. setuptools-0.6c11-py2.4.egg. Do NOT rename it.
  2. Run it as if it were a shell script, i.e. sh setuptools-0.6c11-py2.4.egg . Setuptools will install itself using the matching version of Python (normally python2.4), and will place the easy_install executable in the default location for installing Python scripts.
  3. Go back to the top of this page and try the two easy_install lines, again.

Almost done!


Note: This next part can be done by any user who has SSH access. This means VPS, dedicated, shared, and reseller.

Create a new django project or upload your existing project (not shown). It might be a good idea to create a new dummy project just to have a baseline to test your installation.

To start a new project, run these commands...

django-admin.py startproject newproject
cd newproject
chmod +x manage.py
./manage.py startapp newapp


Note: These last parts can be done by anyone with access to a VPS, dedicated, shared, or reseller plan.

Create the file index.fcgi and place it inside your www directory (same as public_html) or the document root you desire. Change the file's permissions to 0755. Next, edit the file and enter this code:

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

# Add a custom Python path. (optional)
sys.path.insert(0, "/home/username")

# Switch to the directory of your project.
os.chdir("/home/username/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 code above will work for anyone who literally followed the previous commands to install Django and create a new project. If you changed the project or directory names, you will need to make those same changes to the above code.

Finally, here are the .htaccess rewrite rules which belong in the same directory as your new index.fcgi file.

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

*All finished.*


Note: If you have more than one python installation, you may need to specify for which one you're installing setuptools. This seems to only be necessary for Resellers.

Here's how to fix the problem from SSH:

wget http://peak.telecommunity.com/dist/ez_setup.py
/usr/bin/python ez_setup.py
/usr/bin/easy_install django flup==1.0.2

WGET is not enabled by default, so you will need to contact HostGator at least once and request for us to enable WGET.


Article Comments

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.

K Turner
How would you suggest installing the FLUP and pysqlite2 modules for python2.4 in a shared environment?


HostGator
All module requests must be sent to our server administrators via email to support@hostgator.com

HostGator
I have updated the instructions on this page, and personally tested them. Hopefully, this helps everyone with the installation.

Ryan Brady
If you get a server error, run "which python" and make sure python is installed at /usr/bin/python. If not, update the script to point at the other location (possibly /usr/local/bin/python)

Cody M
I'm installing a Django project on a shared account and having an issue. The FastCGI dispatcher, flup, and django are working, but the thread module is reporting that it can't create a new thread. The FastCGI dispatcher is, aside from being formatted slightly differently, exactly the same as the one listed on this page. We've only had everything enable on the account for a few hours at the time of this posting. Is it possible that changes are still being made to the account?


Your comments help us keep the knowledge base updated. This is not a medium for support. If you have questions or need help, please contact us via email, phone or live chat for fast assistance.

Post Comment