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 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?

sdfsdgfd
Very informative post. Thanks for taking the time to share your view with us.

toast
The recommended index.fcgi did not work for me.

I needed to change the line:
runfastcgi(method="threaded", daemonize="false")

to

runfastcgi(method="prefork", daemonize="false")

manishroshan
Below is my settings based on Robert code. It is working

For curlgen.fcgi - change permission to 755 in your file manager and insert following code

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

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

# Switch to the directory of your project.
os.chdir("/home/r5tpl/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")

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

I hope it helps

manishroshan
Below are my working code based on Robert code.

1. For curlen.fcgi - change permission to 755

code is :
#!/usr/bin/python
import sys, os, user

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

# Switch to the directory of your project.
os.chdir("/home/r5tpl/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")

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

Brendan
Unfortunately, none of this is working for me. I either get an "Index of /" page, or a 500 error.

I am using

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

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

# Switch to the directory of your project.
os.chdir("/home/username/django/projects/projectname")

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

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

and for the htaccess

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


This gives me a 500 error.

44,072
Nice tip, toast!

lkj
In order to get these instructions to work I had to add the project path and the directory above the project path to the python path (it took me a long time to finally try this).

sys.path.insert(0,"/home/username/new_project")
sys.path.insert(0,"/home/username")

HostGator
Thank you for your comments. Just a quick reminder that the comments on this page are not monitored by technical support staff, and that for support issues, it is best to contact us by live chat, phone or email so we can assist you right away.

Although comments are not monitored by technical support staff, they are moderated and read by technical writers. Comments will need to be approved by a moderator before appearing.

Our technical writers do read the comments periodically for the purposes of updating the articles, and do appreciate your feedback, suggestions and corrections to the articles themselves, as well as any suggestions or tips for readers of this article. However, support questions posted here are not guaranteed to be replied to in a timely manner or at all. For support issues, it is best to contact our support staff instead by live chat, email or phone.



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