So you have built your awesome django app and now want to deploy your app on web.Following the trend , you read django documentation for deployement and get to know that apache mod_wsgi is the best option for deployement of your django app, which indeed is . But whats unfortunate is that mosts shared hosts doesnt provide access to apaches settings and preferences and hence you cannot use mod_wsgi.And now if you contact your hosting customer support they will advise you to buy VPS(virtual Private Server) ,too expensive for devs for experimental purpose.Even google search for django deployement on shared hosts lacks useful links.However, it can be done in much convinient manner by following these steps.

You need SSH access to your hosting account in order to install python and deploy django.

First following some guidelines from django Book

  • As django book suggest, there are 3 ways to keep developmet and production settings different.viz

    • Set up two full-blown, independent settings files. , seperate settings file and manually changing.
    • Set up a “base” settings file (say, for development) and a second (say, production) settings file that merely imports from the first one and defines whatever overrides it needs to define.
    • Use only a single settings file that has Python logic to change the settings based on context.

I recommend using 1st option (though there will be code repeating but it'll be easy to manage and maintain.

  • In file set ALLOWED_HOSTS to [''] , where is your host URL, to allow django to be used on all subdomains.If you omit the period , django will allow only as domain and consider as illegal host and issued warnings , so its advisable to use period , unless you need custom behaviour.
  • Add your name and email id in ADMINS field in as tuple of 2 strings. eg ('abc', '').This is really crucial as django mails you whenever there is exception generated in production version of your app.

Log in using SSH access

  • In the Security section of your control panel click "SSH/Shell Access" -> "Manage SSH Keys."
  • Click "Generate a new key", enter a password and click "Generate Key." After the key is generated click "Go back."
  • Click the "Manage Authorisation" and click "Authorize."
  • Now download the private key to the folder in which you would like to keep it.
  • Add the key to your SSH Client.
  • Connect to your primary domain using your JustHost login information (the same as logging in to your cPanel).

You can add ssh config files to speed up everytime you ssh into our host , by following this post.

Install Python 2.7.2 (for 2.7.3 replace 2.7.2 with 2.7.3)

  1. mkdir ~/python
  2. cd ~/python
  3. wget
  4. tar zxfv Python-2.7.2.tgz
  5. rm -rf Python-2.7.2.tgz
  6. find ~/python -type d | xargs chmod 0755
  7. cd Python-2.7.2
  8. ./configure --prefix=$HOME/python
  9. make
  10. make install
  11. cd ..
  12. rm -rf Python-2.7.2

Modify ~/.bashrc

  1. vim ~/.bashrc # Press i and enter the following:
  2. export PATH=$HOME/python/bin:$PATH # press <escape>:wq<enter>
  3. source ~/.bashrc # Whenever you want to work using Python 2.7 in the
  4. # console you will need to enter this

Test your Python install

  1. python -V # This should output "Python 2.7.2"

Install setuptools

  1. wget
  2. tar xzvf setuptools-0.6c11.tar.gz
  3. rm setuptools-0.6c11.tar.gz
  4. cd setuptools-0.6c11
  5. python install
  6. cd ..
  7. rm -rf setuptools-0.6c11

Install pip

  1. wget
  2. tar xzvf pip-1.2.1.tar.gz
  3. rm pip-1.2.1.tar.gz
  4. cd pip-1.2.1
  5. python install
  6. cd ..
  7. rm -rf pip-1.2.1

Install Django, flup and MySQL Python Module

(same goes for any other module used by your app)

  1. pip install Django
  2. pip install flup
  3. pip install MySQL-python

FLUP is python module that provides wsgi servers to interact with fastcgi.

Create .htaccess

  1. cd ~/public_html/mysite # where mysite is the root folder of your site
  2. vim .htaccess # Press i then enter the following:
  3. AddHandler fcgid-script .fcgi
  4. RewriteEngine On
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L] # Press <escape>:wq<enter>

Configure Your Website

  1. mkdir ~/django-projects
  2. cd ~/django-projects
  3. startproject myproject # I use the same name as mysite
  4. # without any periods
  5. cd ~/public_html/mysite
  6. vim mysite.fcgi # Press i then enter the following:
  7. #!/home/your_username/python/bin/python
  8. import sys, os
  9. # Where /home/your_username is the path to your home directory
  10. sys.path.insert(0, "/home/your_username/python")
  11. sys.path.insert(13, "/home/your_username/django-projects/myproject")
  12. os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
  13. from django.core.servers.fastcgi import runfastcgi
  14. runfastcgi(method="threaded", daemonize="false") # Press <escape>:wq<enter>
  15. chmod 0755 mysite.fcgi

Test your configuration

  1. python mysite.fcgi

Important : Install every python package using pip Every time you ssh into your account , remember to load custm bash settings by executing source ~/.bashrc.

This should set your django environment on your shared hosting account.However there are still some common error, people have faced. If python is not able to execute mysite.fcgi you need to check that permissions of mysite.fcgi are set to 777 If python throws import errors in your mysite.fcgi pointing to your django project, check you have entered the correct path.If even that doesnt solves the problem add sys.path.insert(0, "/home/your_username/django-projects") in your mysite.fcgi * If django gives you memory error when trying to fetch data from database , you can try commenting out CFUNCTYPE(c_int)(lambda: None) line in $HOME/lib/python2.7/ctypes/ the last line in python 2.7.2 and line #279 in 2.7.3

Serving static and media files from apache server

  • Create a folder by name of 'static' in public_html directory of your hosting account.Now set STATIC_ROOT in to absolute URL of static folder just made.Now cd to your django project directory and execute python collectstatic.

TIP : use gitftp as your ftp.It makes syncing your app a bliss.

This sets up your django instance on your shared hosting platform and deploys your app on it.Please feel free to point out error in the process and give suggestions.