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

What is the highest CHMOD value?

For security reasons we run suPHP, this means that the highest CHMOD value available on...

Some commands or files are missing in SSH (or the mysql command-line client won't connect)

Sometimes when you log into SSH, you might notice that some commands or files (such as the error...

Remote Backups

We know your website data is valuable so as a result our Shared Cloud platform has been designed...

I can't access Webmail

Webmail uses a secure connection and many proxy filters used in offices and schools block them....

I can't restore a large database backup

Due to a limitation in cPanel it is not possible to restore a database that is larger than 2Mb in...