7
Dec

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
/home1/user/python27/bin/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
/home1/user/python27/bin/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
/home1/user/python27/bin/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
#!$HOME/python27/bin/python27
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['DJANGO_SETTINGS_MODULE'] = project_name + '.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
EOF

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

chmod 0755 myproject.fcgi

Step 4: Create the Django Project

Lastly, we’re going to use django-admin to start our project called ‘myproject’ making sure to be in the correct directory first:

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 5 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)

  • Derek@TheDailyLinux
    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).

  • tony
    December 30th, 2013 at 12:11 am

    thank you very much, I benefit from your article a lot. Some additional notes about why Python3.3 is not used by django developers from my side, the reason is simply that flup is coded in python2.x, some syntax is not compatible with python3.3, so better avoid using python3.3 on those cases where fcgi must be used for django.

  • 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:
    ^admin/
    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 :/

Share your thoughts, leave a comment!