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`
    source ~/.local/bin/
  2. Now run the following commands:

    easy_install-2.7 --user pip && ~/.local/bin/pip install --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==1.0.2

    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:

./  ../  projectname/

./  ../  projectname/

./  ../

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:

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")
except ImportError:
    from flup.server.fcgi import WSGIServer
    from django.core.wsgi import get_wsgi_application


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?

 Print this Article

Also Read

Error Log

We have disabled the error log function in cPanel because it contains a quirk that causes it not...

What are the mailserver ports?

Secure POP3 is on port 995Non-secure POP3 is on port 110Secure SMTP is on port 465Non-secure SMTP...

I can't send email because my ISP blocks SMTP (port 25)

Some ISPs block their customers from accessing other ISPs mail servers, in an attempt to stop...

What if I reach my space/mailbox limit?

The majority of our customers don't go near their disk space or mailbox limits, but if you do we...

Remotely Connect to a MySQL Database

It is possible to connect to your MySQL database remotely. First you will need to allow the IP...