Tutorial: Installing Django on Shared Hosting Service Such as Bluehost.com

This guide will walk you through how to install and run Django on a shared host such as Bluehost. Because I use Bluehost, I was able to verify the steps you see below, but you might need to modify some of the steps to work with your specific host. The whole thing should take less than 10 minutes. If I’ve done my job right, you should be able to copy/paste multiple lines to run all commands in a block as if it were a script. If you feel more comfortable, you can run each command line by line. Let’s get to it…

Step 1: Installing Python 2.7

This step is for those of you who have an older installation of Python on your webhost. In the case of Bluehost (as of 12/6/2012), the version that is installed is 2.6.6. If you have version 2.7.0 and up (but not including version 3.0), then you can skip this step. Otherwise, follow along (you should be able to copy/paste this entire block into the terminal to run it all at once):

cd ~
mkdir python27
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar xf Python-2.7.3.tgz
cd Python-2.7.3/
./configure -prefix=$HOME/python27/ --enable-unicode=ucs4
make && make install
mv ~/python27/bin/python ~/python27/bin/python27
echo "PATH=\$PATH:\$HOME/python27/bin" >> ~/.bashrc
echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc
echo "export LANG=en_US.UTF-8" >> ~/.bashrc
. ~/.bashrc

At this point, you should be able to run which python27 successfully like this:

# which python27

Step 2: Installing SetupTools and PIP

Now that Python 2.7 has been installed, we’ll need to install setuptools and pip (you should be able to copy/paste this entire block into the terminal to run it all at once):

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar xf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python27 setup.py install
cd ~
wget http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz
tar xf pip-1.1.tar.gz
cd pip-1.1
python27 setup.py install

At this point, you should be able to run which pip successfully like this:

# which pip

Step 3: Use pip to Install Modules

We’re going to install MySQL-python, flup, and Django using pip (Note: If you plan on using PosgreSQL, you’ll need to install psycopg2):

pip install Django MySQL-python flup #psycopg2

At this point, you should be able to run which django-admin.py successfully like this:

# which django-admin.py

Step 4: Setup an Environment for the Project

I’m going to name the project ‘myproject’. When you go to create your own, you’ll want to replace anything called ‘myproject’ with the project name of your choice (you should be able to copy/paste this entire block into the terminal to run it all at once):

mkdir ~/public_html/myproject
cd ~/public_html/myproject

cat > myproject.fcgi << EOF
import sys, os
project_name = "myproject"

# Add a custom Python path.
sys.path.insert(0, os.path.expanduser("~") + "/python27")
sys.path.insert(13, os.getcwd() + "/" + project_name)

os.environ&#91;'DJANGO_SETTINGS_MODULE'&#93; = project_name + '.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

cat > .htaccess << EOF
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myproject.fcgi/$1 &#91;QSA,L&#93;

chmod 0755 myproject.fcgi


<h3><span style="color: #800000;">Step 4: Create the Django Project</span></h3>
<p>Lastly, we're going to use django-admin to start our project called 'myproject' making sure to be in the correct directory first:</p>
cd ~/public_html/myproject/
django-admin.py startproject myproject

Now, visit your website at http://mydomain/myproject and you should see the Django start page! Have fun with it.

A final note: If you want to use the admin pages, you’ll need to follow a few more steps to get the page to show up properly with css, js, and images (you’ll need to replace mydomainname with your actual domain name).

ln -s $HOME/python27/lib/python2.7/site-packages/django/contrib/admin/static $HOME/public_html/myproject/static
sed -i "s/^STATIC_ROOT = ''/STATIC_ROOT = 'admin'/g" $HOME/public_html/myproject/myproject/myproject/settings.py
sed -i "s/^STATIC_URL = ''/STATIC_URL = 'http:\/\/mydomainname.com\/myproject\/static'/g" $HOME/public_html/myproject/myproject/myproject/settings.py

Sources: Simply Argh Blog

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:

There's 8 Comments So Far

  • matt
    March 6th, 2013 at 11:41 pm

    thanks for the recipe. The last 4 lines of Step 2 can be slightly simplified to:

    curl http://python-distribute.org/distribute_setup.py | python
    curl –insecure https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

    (adapted from http://www.pip-installer.org/en/latest/installing.html)

  • [email protected]
    March 6th, 2013 at 11:59 pm

    Awesome! Thanks for the tip. I didn’t know you could pipe curl into python (or at least never gave it a thought).

  • Borges
    February 1st, 2014 at 11:09 am

    Thanks a lot for the tutorial. It seems I almost got there already.

    After doing everything with no errors, I went to check myproject URL, and the result was this:

    Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
    The current URL, , didn’t match any of these.

    What should I do next?

    Thanks in advance

  • Kate
    February 17th, 2014 at 1:15 pm

    After I use startproject and try to go to the new web page I get a (404) error, I’m running Django 1.6. Please help :/

  • Simo
    July 26th, 2014 at 7:28 am

    I’ve faced same 404 error. Everything seems to be ok and I can run fcgi script from command line and it works fine and outputs expected html (status 200/ok) but when I try to access mysite via browser it will get 404.

    Python 2.7.8 & Django 1.6.5

  • Shane
    July 4th, 2016 at 2:55 pm

    fastcgi is not supported by Django 1.9 https://docs.djangoproject.com/en/1.8/howto/deployment/fastcgi/
    So what should be done? Use Flup for wsgi? Or Nginx and gunicorn?
    What’s the best way to proceed?

Who Linked To This Post?

  1. Installing NLTK On Your Web Server | Musings of a Computational Linguist
  2. Hosting Your Django Projects on Bluehost! – Hello World!

Share your thoughts, leave a comment!