Harish Mallipeddi RSS

Avid Pythonista with a secret love for Erlang.

harish.mallipeddi at gmail

 Photos

 LinkedIn

 Twitter

 Projects

Older posts

Jul
5th
Wed
permalink

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 :-
    1. Apache/mod_python/Django
    2. Apache/mod_fcgi/Flup/Django
    If your hosting space provider does not support mod_python (like mine), then you need to choose the second option. As you might imagine, in this tutorial I’m going to restrict myself to describing the second way of deploying Django. If you want to use mod_python, please refer to some other tutorial online.

Installation

  1. Get the latest version of Django from SVN
  2. Edit your PATH and PYTHONPATH environment variables
    export PATH=$PATH:$HOME/django_src/django/bin
    export PYTHONPATH =$PYTHONPATH:$HOME/django_src:$HOME/django_projects
    
  3. Create a directory for your Django projects and create your first project
    mkdir django_projects
    cd django_projects
    django-admin.py startproject myproject 
    
  4. 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 
    
  5. 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!).
  6. 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.
  7. Now create a publicly accessible folder under your web root (~/www/ or ~/public_html/) for your new Django project.
    mkdir ~/www/myproject
    
  8. 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!
  9. Make the file django.fcgi executable.
    chmod 755 django.fcgi
    
  10. 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.
  11. Proceed to create a new app in your myproject folder - follow the instructions at the Django Project Documentation site.

Other useful links:

Updated : Modified step 8 (django.fcgi) to make it much cleaner.

Comments
blog comments powered by Disqus