Login

Django with Fast CGI (Server Conf)

Author:
jackoder
Posted:
May 6, 2010
Language:
Python
Version:
1.1
Score:
-2 (after 2 ratings)

I've taken it from hostgator help page.

I hope it will be useful for you.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Django can be installed on a shared environment if you use FastCGI with it.

We need to install the flup module and django first of all. 

We can use easy_install to do that.

Run these commands:
easy_install flup
easy_install django

If easy_install doesn't existed 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.

It's okay from now on.

Note: This next part can be done by any user who has SSH access. 

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.

Create a new project with these commands...
django-admin.py startproject newproject
cd newproject
chmod +x manage.py
./manage.py startapp newapp


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]

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.