Django on Hostmonster
Contents
Introduction
This setup uses virtualenv. It does not use the --no-site-packages option, because I want to rely on the global packages.
This puts your django site under your home site in its own directory. This need not be so. You can tweak the location of the .htaccess and mysite.fcgi script as needed. I won't go into detail here because this is generic fcgi stuff.
Note that this does not "hotlink" the media directory. You can figure out how to do that yourself, and it's a trick that's useful for other projects as well.
Setting up
- Remember:
- Your home directory. I'll refer to this as $HOME. In some of the scripts, you'll have to manually replace it.
- What you want to call your project. I call it 'mysite'.
- Create a virtualenv:
virtualenv ~/env/django
- Activate virtualenv:
. ~/env/django/bin/activate
- Install django:
easy_install django
- Create your project:
mkdir ~/Projects; cd ~/Projects; jango-admin.py startproject mysite
- Create a site root:
mkdir ~/public_html/mysite; cd ~/public_html/mysite
- Create a fcgi script (see below)
- Set perms to 755:
chmod 755 ~/public_html/mysite/mysite.fcgi
- Test fcgi script.
~/public_html/mysite/mysite.fcgi
- You should see an HTML page spit out. Any other errors, such as import errors or interpreter errors, need to be fixed at this point.
- Create a .htaccess file (see below)
- Test: go to
http://example.com/mysite
- Look at the error log from the hostmonster cpanel IMMEDIATELY after you load the page and get a 500
mysite.fcgi
I like to name my fcgi after the project it is running for. This means I can kill it with pkill without killing my other fcgi scripts.
The first line, the shebang line, is important. By using the virtualenv python interpreter, we are getting, for free, all the modules installed there. Unfortunately, django doesn't turn projects into python packages, and so we still have to do some hacking to get things to work right.
You MUST replace the $HOME with your actual home directory.
#!/$HOME/env/django/bin/python import sys, os sys.path.insert(0, "/$HOME/Projects/mysite") # Switch to the directory of your project. (Optional.) # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings" from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded", daemonize="false")
Don't forget to make this 755 so it can be executed.
Don't forget to run this script, see if you actually get a page back.
BONUS: Move the ~/Projects/mysite/mysite/settings.py
to ~/public_html/mysite/settings.py
. Then change the reference to mysite.settings
to just settings
in mysite.fcgi
.
.htaccess
AddHandler fcgid-script .fcgi RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]
settings.py
It seems the default settings.py is pretty dumb. It doesn't know that you are running from under a sub-directory of your main site.
- Change STATIC_URL to point to /mysite/static.
Developing
You can restart your fcgi by running: pkill mysite.fcgi
. Hostmonster will also kill your fcgi script after 5 minutes or so of inactivity.
Don't forget to re-activate your environment when you login again. . ~/env/django/bin/activate
. Otherwise, you'll be missing key scripts.
PostgreSQL
Unfortunately, psycopg2 is not included with Hostmonster accounts. You need to run easy_install psycopg2
.