How to Setup Django on Bluehost Shared Hosting

There are already some guides on setting up Django on Bluehost available on the web. Unfortunately, these guides have become obsolete as technology has progressed. The two main guides that are dated back in 2013 and 2012 no longer work. Bluehost’s own guide is also not enough to get it working.

The primary problem is that the technology used to interface Django with the shared hosting, namely FastCGI, is now considered obsolete by Django. As a result, new versions do not support it.

After a great deal of struggle, I managed to get Django to run on a Bluehost shared hosting website. In this guide, I lay out all of the steps required to get it to work.

Step 1. Your Django Project

If you already have a Django project you want to deploy to your server, you can skip this step. Otherwise, follow the Django tutorial to make a new Django project. This guide will assume you have a Django project ready on your local machine.

Step 2. Installing Python on your Server

Bluehost already provides a python binary. However, you cannot install new Python modules (like Django) using this, due to the restricted directory permissions of shared hosting. Bluehost’s provided python binary is in /usr/bin, but on your shared hosting you can only modify the contents of your home directory.

To begin, SSH into your Bluehost account. Create a new directory for the source files you will download, and a directory to install into:

cd ~
mkdir src
mkdir local_usr
cd src

Next, download and install Python 2.7 from source, replacing {username} with your username:

wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -xzvf Python-2.7.9.tgz
cd Python-2.7.9
./configure -prefix=/home2/{username}/local_usr --enable-unicode=ucs4
make
make install

The Python binaries should now be installed into the local_usr directory. Now add them to your path. Edit the ~/.bashrc file, and append the following (replace {username} with your username):

PATH="/home2/{username}/local_usr/bin:$PATH"

Now create a symbolic link to the versioned python binary, and refresh the path:

cd ~/local_usr/bin/python2.7
ln -s python2.7 python
source ~/.bashrc

Check that Python is working correctly by running the following. It should output ‘Python 2.7.9’.

python --version

Step 3. Install Django

Version 1.9 of Django removed FastCGI support. Install the last supporting versions of Django and the necessary dependencies as follows:

cd ~/
pip install django==1.8.13
pip install flup==1.0.3
pip install mysql-python

All the necessary binaries should now be installed.

Step 4. Upload your Django project

Create a directory in your home directory to hold your Django projects (for example: mkdir ~/django_projects). Then exit SSH. Upload your Django project using SCP:

scp -r my_django_project {username}@myserver.com:~/django_projects/

Your Django project is now on the server and ready to be used.

Step 5. Serve the Django Project to the Web

SSH back into your Bluehost server. Create a new public directory for the Django project in your public_html directory. For example:

mkdir ~/public_html/my_django_project
cd ~/public_html/my_django_project

Make a new FastCGI .fcgi file and a .htaccess file in that new directory. These files direct the Apache server to your Django project.

Example .fcgi file:

#!/home2/{username}/local_usr/bin/python
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home2/{username}/local_usr/lib/python2.7/site-packages")
sys.path.append("/home2/{username}/django_projects/my_django_project")

os.environ['DJANGO_SETTINGS_MODULE'] = 'my_django_project.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

.htaccess file:

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

Finally, change the .fcgi file permissions with:

chmod 0755 my_django_project.fcgi

Your Django site should now be visible at /my_django_project on your server.

If run into any problems with this guide, or you have any questions or comments, please leave them in the comments section below.