Sie sind auf Seite 1von 4

(this probably out of date...

)
How to install Django on a basic shared hosting plan with plain CGI
I've successfully installed Django on my Hostgator account ('Baby Croc' plan) using these
steps. Hostgator has been really great for me, but they don't support fastCGI. Using vanilla
CGI with Django is not generally recommended and results in crappy performance, but for a
low traffic site it's bearable (barely).
Here are some links to source material for this document:
http://www.djangoproject.com/documentation/install/
http://code.djangoproject.com/ticket/2407
http://mysql-python.sourceforge.net/
http://www.python.org/dev/peps/pep-0333/
http://www.djangoproject.com/documentation/modpython/#serving-the-admin-files
Prerequisites
SSH shell access to your account
Python 2.3+ (Python 2.4 preferred)
MySQLdb module (ask your support person to install this)
Some knowledge of Django and Python
Install django and create a project directory
Create a directory in your home directory called "django" (or whatever) that is outside of
your document root.
mkdir~/django
cd~/django
svncohttp://code.djangoproject.com/svn/django/trunk/django
Create a directory for your Django projects.
mkdir~/django/projects
You should now have the following folders:
~/django/django
~/django/projects
Edit your .bash_profile and add the following:
exportPATH=".:/home/username/django/django/bin:$PATH"
exportPYTHONPATH=/home/username/django
Thenenablethechanges:
source.bash_profile
Add the following script to your cgi-bin directory and name it wcgi.py
#!/usr/local/bin/python
importos,sys
sys.path.insert(0,"/home/username/django/")
sys.path.insert(0,"/home/username/django/projects")
sys.path.insert(0,"/home/username/django/projects/newprojects")
importdjango.core.handlers.wsgi
os.chdir("/home/username/django/projects/newproject")#optional
os.environ['DJANGO_SETTINGS_MODULE']="newproject.settings"
defruncgi():
environ=dict(os.environ.items())
environ['wsgi.input']=sys.stdin
environ['wsgi.errors']=sys.stderr
environ['wsgi.version']=(1,0)
environ['wsgi.multithread']=False
environ['wsgi.multiprocess']=True
environ['wsgi.run_once']=True
application=django.core.handlers.wsgi.WSGIHandler()

ifenviron.get('HTTPS','off')in('on','1'):
environ['wsgi.url_scheme']='https'
else:
environ['wsgi.url_scheme']='http'

headers_set=[]
headers_sent=[]

defwrite(data):
ifnotheaders_set:
raiseAssertionError("write()beforestart_response()")

elifnotheaders_sent:
#Beforethefirstoutput,sendthestoredheaders
status,response_headers=headers_sent[:]=headers_set
sys.stdout.write('Status:%s\r\n'%status)
forheaderinresponse_headers:
sys.stdout.write('%s:%s\r\n'%header)
sys.stdout.write('\r\n')

sys.stdout.write(data)
sys.stdout.flush()

defstart_response(status,response_headers,exc_info=None):
ifexc_info:
try:
ifheaders_sent:
#Reraiseoriginalexceptionifheaderssent
raiseexc_info[0],exc_info[1],exc_info[2]
finally:
exc_info=None#avoiddanglingcircularref
elifheaders_set:
raiseAssertionError("Headersalreadyset!")

headers_set[:]=[status,response_headers]
returnwrite

result=application(environ,start_response)
try:
fordatainresult:
ifdata:#don'tsendheadersuntilbodyappears
write(data)
ifnotheaders_sent:
write('')#sendheadersnowifbodywasempty
finally:
ifhasattr(result,'close'):
result.close()
runcgi()
Edit your root .htaccess file and add the following lines
RewriteEngineon
RewriteRule^media[L]
RewriteRule^cgibin[L]
RewriteCond%{REQUEST_FILENAME}!f
RewriteRule^(.*)$/cgibin/wcgi.py/$1[QSA,L]
Create a new django project
(or upload your existing project)
It might be a good idea to create a new dummy project just to have a baseline to test your
installation.
cd~/django/projects
djangoadmin.pystartprojectnewproject
cdnewproject
manage.pystartappnewapp
Try it out
Since DEBUG is True by default (in settings.py) you should get the default Django 404 page.
Make sure you set DEBUG=False before you open your site to the public.
Notes:
The admin media files reside in django/contrib/admin/media and since they aren't accessible
from your document root you need to create a symbolic link to the media files (or just copy
them):
ln -s ~/django/django/contrib/admin/media ~/www/media
Where "~/www/" is your document root.

Das könnte Ihnen auch gefallen