Run Django on Bluehost
Posted: October 28th, 2008 | Author: mike | Filed under: Django, Python |Last weekend, I took some time to setup Python and Django on our hosting server which is bought from Bluehost. I also bound a domain name to my working space afterwards. Here is a walk-though for all work need to do.
- Install Python
There is an old version of shared Python on the server, but even it’s not too old to be used, I’d rather to install my own one. Because I will use some Python libraries and applications, and I can only install them by my permission level. It’s quite easy if you already have SSH access. One one thing may need to be concerned, that is to configure installation path. Such as, "./configure -prefix=/home/USERNAME/python -enable-unicode=ucs4".
Here is a full list how to do that from a Chinese blog. - Install flup/mysql-python
Before you install these components, I’d like to suggest you install EasyInstall at first. Then you can run it to install others:
easy_install mysql-python
easy_install flup
Maybe you need to install other libraries here, just go ahead. - Install Django
I didn’t find SVN was working on server. And I chose a release version of Django(1.0). So pick up a folder and run wget command, and install it with classic 3 commands. - .htaccess and fastcgi settings
New a web folder under public_html folder(which will be published), and create both files under it:# .htaccess file AddHandler fcgid-script .fcgi RewriteEngine On RewriteBase / RewriteRule ^(media/.*)$ - [L] RewriteRule ^(static/.*)$ - [L] RewriteCond %{REQUEST_URI} !(dispatch.fcgi) RewriteRule ^(.*)$ dispatch.fcgi/$1 [L] # dispatch.fcgi file #!/home/USERNAME/python/bin/python import sys, os # Add a custom Python path. sys.path.insert(0, "/home/USERNAME/python") sys.path.insert(0, "/home/USERNAME/Django-1.0") os.chdir("/home/USERNAME/Django-1.0/PROJECTNAME") os.environ['DJANGO_SETTINGS_MODULE'] = "PROJECTNAME.settings" from django.core.servers.fastcgi import runfastcgi runfastcgi(["method=threaded", "daemonize=false"]) -
You need to set proper rights(chmod +x) for dispatch.fcgi file. In dispatch.fcgi file, you need to replace USERNAME and PROJECTNAME with your own names. "Django-1.0" is my folder where my Django installed.
And do remember that place your project folder under your Django installation folder.(It kept telling me Unhandled Exception error before I figured this out).
Besides, you probably want to confirm FastCGI is installed from your hosting settings, I tried that also but I didn’t find it. So far as I know, FastCGI has been enabled by default on Bluehost. And the file name "dispath.fcgi" is from somewhere, somebody’s experience that corn jobs on server will search and kill suspended processes without these names.
-
Start project
It should be ready so far. Go to your Django folder,and rock your project with the name specified before:
django-admin.py startproject PROJECTNAME
- Setup domain name
It’s quite easy to add a new domain in cPanel. Just add a new addon domain and match the domain folder with the one where you place .htaccess and dispatch.fcgi files. Copy the A record IP address and save it to your IP settings. Restart VDNS if you have that. You can also use the subdomain you just created from cPanel. That’s all!
- When you domain name is active you should be able to see something familiar as below:
If you meet problems do check thing below:
- Don’t confuse yourself by different path of your Python, Django, Project and publish folder.
- You need to have permissions run dispatch.fcgi file.(chmod +x dispatch.fcgi)
- If you see Unhandled Exception errors, please be sure that you place your project under Django installed folder.(I haven’t figured why should be like this.)
- If you still have problems, don’t hesitate to leave your comments here.
Hope this helps for you.
First off - GREAT walkthrough. I wish I had found this earlier - MUCH earlier.
However, I’m having issues getting it to actually WORK. I’ve finished the walkthrough, but I’m having issues. Specifically I’m getting a 500 error when I try to access the site.
cPanel is spitting out these two errors:
[Tue Nov 04 22:10:04 2008] [warn] (104)Connection reset by peer: mod_fcgid: read data from fastcgi server error.
[Tue Nov 04 22:10:04 2008] [error] [client 64.139.240.191] Premature end of script headers: dispatch.fcgi
I’ve looked at just about everything I can find, and I can’t seem to get this resolved. Any help would be appreciated.
Bryan,
I didn’t meet this error while I have been using this setup for a few weeks. Here are my suggestions:
1. I know it’s basic knowledge but do check the permission of dispatch.fcgi has 755 above.
2. Check the content of dispatch.fcgi file, and make sure you specify right location of Python execution. You should remove the first line of my sample(file name comment)-that’s just for a notice.
3. You should avoid to input any print statement in dispatch.fcgi file.
4. try to run ./dispatch.cfgi in shell and check what it says.
Hope this will work for you.
It was one of THOSE errors. The one’s that are SO obvious that I wouldn’t have EVER caught it on my own.
I had put a notice to myself above the shebang in the dispatch.fcgi file, meaning that bash was attempting to run the file instead of my version of python. Thanks for the help!
I put together a more long-winded version for those that haven’t got easy-install, it walks the user through installing latest version of python, subversion etc. by hand. Might come in useful:
http://alextreppass.co.uk/blog/2008/11/14/getting-django-working-on-bluehost