After discovering Python 2.6 on my shared hosting, the next step is to install Django. Since my user account is not a sudoer and I don’t have the root password, I cannot use the server’s package system for installation.
Fortunately, Python projects like Django are quite simple to install manually.
Installing Django#
Download and install Django using the following commands:
wget http://www.djangoproject.com/download/1.3/tarball/
tar xzvf Django-1.3.tar.gz
cd Django-1.3
python2.6 setup.py install --userNote: I used the python2.6 command to specify that I want to install and use Django with Python 2.6. If you have a python alias configured, that should be sufficient. For Python 2.4, the --user option does not work, so use this command instead:
python setup.py install --home $HOME/.localAdding Django to Your PATH#
Next, add your Django installation to your user PATH by editing your .bashrc file. In your home folder, run:
vi .bashrcAdd the following line:
export PATH=$HOME/.local/bin:$HOME/.local/usr/bin:$PATHAfter these two simple steps, Django is ready and you can create your first project or install an existing one.
Configuring Your Django Project for the Web#
Assuming you have your Django project installed in ~/projects/kermit-webui, you need to configure two components to make it accessible from a browser:
- A Python FastCGI script to load your Django application
- An
.htaccessfile to configure Apache and load the script
Creating the Public HTML Folder#
First, create the public_html folder where these components will reside:
mkdir ~/public_html/kermit
cd ~/public_html/kermitCreating the FastCGI Script#
Create the FastCGI script with the following content:
vi kermit.fcgi#!/usr/bin/python2.6
import sys, os
# Add a custom Python path.
sys.path.insert(0, "/home/user/.local/lib/python2.6")
sys.path.insert(13, "/home/user/projects/kermit")
os.environ['DJANGO_SETTINGS_MODULE'] = "webui.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")At the end of the script, as you can see, we start the FastCGI listener to accept incoming requests.
Configuring the .htaccess File#
Now configure the .htaccess file:
vi .htaccessAddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.*)$ kermit.fcgi/$1 [QSA,L]This configuration sets up the Apache RewriteEngine to direct all requests to the kermit.fcgi script, except for requests containing /static/ in the URL (where static files like CSS, images, and JavaScript are stored) and favicon.ico.
Setting File Permissions#
The FastCGI script needs executable permissions:
chmod 0755 kermit.fcgiInstalling Additional Packages#
That’s all for the basic setup. Depending on your hosting provider and project requirements, you may need to install additional packages. For BlueHost, using Django with FastCGI requires installing the flup project.
Download and install flup:
wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
tar xzvf flup-1.0.2.tar.gz
cd flup-1.0.2For Python 2.6/2.7:
python setup.py install --userFor earlier Python versions:
python setup.py install --home $HOME/.localConclusion#
Now you can browse your Django application! :D
To see a working example of this setup, point your browser to http://kermit.mornati.net.