Knowledgebase

Setting up Django

All hosting accounts come ready to make use of Django, a popular web application framework. To use Django, you will first set up a virtual environment to contain all of your Python packages, then you will upload your Django project and configure it to run on our servers.

Setting Up a Virtual Environment

To use Django, you will need to use virtualenv and virtualenvwrapper to allow you to install and manage your own Python packages in your home directory. Setting this up is very simple, you just need to do the following in SSH:

  1. First, add the following three lines to your .bashrc file using a text editor:

    export PATH=~/.local/bin/:$PATH
    export VIRTUALENVWRAPPER_PYTHON=`which python2.7`
    . ~/.local/bin/virtualenvwrapper.sh
  2. Now run the following commands:

    easy_install-2.7 --user virtualenvwrapper
    source ~/.bashrc
    mkvirtualenv newenvname

    Where newenvname is whatever you'd like to call the environment (for simplicity you might want to make this the same as the name of your Django project).

  3. Your virtual environment is now set up, and you can install new Python packages using pip. Start by installing Django (the flup package is also required):

    pip install django flup

    If there are any other packages you require, they can be installed in the same way.

Upload and Configure Your Django Project

Now you should create a directory called django and upload your project to this directory. For example, if your project is called projectname, your directory structure might look something like this:

django/:
./  ../  projectname/

django/projectname:
./  ../  manage.py  projectname/

django/projectname/projectname:
./  ../  __init__.py  settings.py  urls.py  wsgi.py

Now create a file in public_html (or in the root directory of the relevant subdomain or addon domain if you're not setting up Django on your main domain) called django.fcgi. This file should be made executable (chmod 755), and should contain the following:

#!/home/username/.virtualenvs/newenvname/bin/python
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home/username/django/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")

Replace username with your cPanel/SSH user name and projectname with the name of your Django project.

Finally, edit the .htaccess file in the same directory as django.fcgi (creating the file if it does not already exist) and add the following to it:

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

You should now see your Django project when visiting your web site.

What Next?

Now that your project is up and running, you'll want to configure your database through cPanel and run syncdb in SSH as you normally would. You'll also want to set STATIC_ROOT if you're using the staticfiles app to tell collectstatic to place your static files somewhere in public_html.

There are a few more things to remember:

  • Activating the virtual environment — If the virtual environment isn't already activated (i.e. each time you log into SSH) you will need run the command workon newenvname before you can run any management commands or install any new packages.
  • Making changes to your code — Note that any changes to your Python code will not be immediately picked up, due to a bug in mod_fcgid. To get around this, after updating your files you should run django-kill in SSH to force any FCGID processes to exit.
  • Debugging errors — Any errors generated by your Django applications will be logged into the "tmp/error.log" file in your home directory. This includes any backtraces from unhandled exceptions, and any other text output on stderr.


Was this answer helpful?

Also Read

Remote Backups (Views: 267)
Using Python 2.6 (Views: 836)
Error Log (Views: 1883)

Get Support