Jul
5th
Wed
5th
Tutorial : Setting up Django on a shared host
In this tutorial, I will walk you through the process of installing Django on a shared host.
Before we proceed any further, I would like to mention a few things first:
- The instructions I provide over here work well for my hosting space provider - A Small Orange. But you may need to adapt a few things here and there to suit your setup which may vary from hosting service provider to provider.
- There are two popular ways of deploying Django :-
- Apache/mod_python/Django
- Apache/mod_fcgi/Flup/Django
Installation
-
Get the latest version of Django from SVN
cd ~ svn co http://code.djangoproject.com/svn/django/trunk/ django_src
-
Edit your PATH and PYTHONPATH environment variables
export PATH=$PATH:$HOME/django_src/django/bin export PYTHONPATH =$PYTHONPATH:$HOME/django_src:$HOME/django_projects
-
Create a directory for your Django projects and create your first project
mkdir django_projects cd django_projects django-admin.py startproject myproject
-
Change the user permissions on the project settings file (this is to prevent others on the shared host from looking at your settings file which happens to include the database password in plain text)
chmod 600 myproject/settings.py
- Add your database information to the project settings file (If you’re using ASO, they normally do not have the MySQLdb python module which Django needs in order to talk to the MySql database. Post a ticket and someone will install it for you!).
- Install Flup. Flup includes a WSGI server which can speak FastCGI and is implemented in Python. Grab the latest snapshot of Flup from here. Remember to include Flup folder in your PYTHONPATH as we’ll be using it in the next step. To check if Flup is properly installed, get into the Python shell, and try importing flup module.
-
Now create a publicly accessible folder under your web root (~/www/ or ~/public_html/) for your new Django project.
mkdir ~/www/myproject
-
Add this file called django.fcgi to the folder created above (You can call it anything you want. But the file needs to have the .fcgi extension for Apache to handle it properly)
#!/usr/bin/env python2.4 import sys, os # your custom PYTHONPATH entries go here: sys.path.append('/home/harish/django_src') sys.path.append('/home/harish/django_projects') sys.path.append('/home/harish/flup-r2016') sys.path.append('/home/harish/apps') # your settings module goes here: os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' # invoke FastCGI server (using Django's flup wrapper) from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded", daemonize="false")You have to edit the sys.path setting in the above code according to your server setup! -
Make the file django.fcgi executable.
chmod 755 django.fcgi
-
Now you’re good to go. Point your browser at http://www.yourdomain.com/myproject/django.fcgi/ and you should be able to see the default Django “Congratulations!” page.
- Proceed to create a new app in your myproject folder - follow the instructions at the Django Project Documentation site.
Other useful links:
- A Small Orange Support Forums
- List of Django Friendly Web Hosts
- Original thread at the ASO support forums based on which this tutorial has been written.
- Django Documentation page on FastCGI configuration.
Updated : Modified step 8 (django.fcgi) to make it much cleaner.