Installing Django on a shell hosting account

Requirements

Django can only be installed on our shell hosting accounts. This is because setting up Django requires you to install some Python packages. Django also needs to be configured from the shell by running commands, making it impossible to do on your own if you have a non-shell hosting account.

Start by logging into your shell account. Change to the directory where you want to install Django. Typically this would be either the root level of a website directory tree such as ~/www/example.com, or a subdirectory of a website, such as ~/www/example.com/blog.

Installing Django dependencies

Before we can install Django, we need to install a Python module called 'flup'. Flup allows Python to work with FastCGI, which is a way of executing server side applications. Assuming that you are in the directory where you want to install Django, make a directory called installation (you can remove this later) and then change directory into it.

mkdir installation
cd installation

Once you are in the installation folder, you need to find the latest version of Flup. Look for the newest version of 'flup-x.x.x.tar.gz', where the x's represent a version number. Once you find this file, right-click on the name of the file and copy it's link to your clipboard. You then need to switch back to your terminal and download the file using the command wget [link you just copied to the clipboard]. Once the file has downloaded, use the command tar -xzvf flup-x.x.x.tar.gz, to extract the files from the tar.gz archive. Change directory into the flup-x.x.x directory that has just been extracted using the command cd flup-x.x.x.

To install the Flup module, use the command python setup.py install --user. You won't have permissions to install the module system wide, which is why the --user option is used. This means that the module is only installed for your user.

Before continuing, change directory up a level to the installation folder, using the command cd ...

Installing Django

Assuming that you are now in the installation folder, you need to find the latest version of Django from here. Right-click on the download link for 'Django-x.x.x.tar.gz' under 'Option 1. Get the latest official version'. You then need to switch back to your terminal and download the file using the command wget [link you just copied to the clipboard] -O django.tar.gz. Once the file has downloaded, use the command tar -xzvf django.tar.gz, to extract the files from the tar.gz archive. Change directory into the 'Django-x.x.x' directory that has just been extracted using the command cd Django-x.x.x.

To install Django, use the command python setup.py install --user.

You now need to change back into the directory that you would like to create your Django site in. Assuming you are in the 'Django-x.x.x' directory, you will be able to do this will the command cd ../... You can now remove the installation directory using rm -r installation.

Creating a Django project

It's now time to create a Django project. You can do this using the command python ~/.local/bin/django-admin.py startproject [project name]. If you use the command ls, you should see that a folder has been created with the name of your project.

Creating a .htaccess file

Create a .htaccess file to tell the Apache web server to execute FastCGI scripts.

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

The FastCGI script

Create the django.fcgi script that we just told Apache to run.

#!/usr/bin/python
import sys, os

# Add a custom Python path to your project directory.
sys.path.append("[project foder]")

# Switch to the directory of your project.
os.chdir("[project folder]")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "[project name].settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

You can find the full path of your project by changing directory into your project folder using the command cd [project name] and then running the command pwd. Replace each [project folder] with the output of the pwd command and replace [project name] with the name of your project.

You need to run the command chmod 755 django.fcgi, which effectively means that only your user is allowed to read, write and execute the script, and that anyone in the same user group as you, or any other users, can only read and execute the file. This is needed because FastCGI will not execute the script if other users/groups have write access to it. You will also need to change the permissions of the directory which contains the FastCGI script. Assuming you are currently in the directory containing the FastCGI script, you can do this using the command chmod 755 ..

Your first Django-powered page

Open up a web browser and head over to the site where you have just installed Django. You should see a page with the title 'It Worked!'.

Further reading

The Django Project have a collection of introduction tutorials that will take your new site further. We suggest you keep reading there!