Categories
Uncategorized

How to install Django on a Hostgator shared hosting

Thanks to that help full document that I found on a Forum (I can’t )

https://docs.google.com/document/d/1jhvixMmTRGYHRbHaYlTHTNrtpziGotQMc0iBO0sTbIo/edit

Here is a step by step tutorial to install Django framework on your shared hosting. But first of all it is important to be aware that the performance is not the best on a shared Hosting. This solution is a good one for a small scaled solutions, on for a website that doesn’t have a lot of visitors. Otherwise it is better to purchase a dedicated server for a better performance and experience for your application users. Also you are limited in term of Python Modules the Hostgator list above show what you have access to (http://support.hostgator.com/articles/hosting-guide/hardware-software/python-modules) according to this forum post : http://grokbase.com/t/gg/django-users/1313jnz7hv/django-installation-on-hostgator

 

1- First of all you need to enable your access via SSH to your server. The best way to do this (and the only way I found) is by contacting via the chat (chat.hostgator.com) or phone the customer service.

2 – Once you access is enabled connect to your server via SSH (Windows users can access trough Putty)

For Linux / Mac users type the following command in the Terminal:

ssh -p 2222 cpanelusr@ip.add.re.ss

For Windows Putty users :

Hotsname / Server IP : yourmaindomain.com / or the server IP (you can find this information on your Cpanel on the right sidebar in the general information section)

Username and Password : Use your Cpanel Credentials

Port : 2222 (for shared hosting otherwise it’s 22 for dedicated servers according to Hostgator help page http://support.hostgator.com/articles/how-do-i-get-and-use-ssh-access)

The following steps are from the source document linked above :

 

(this probably out of date…)

How to install Django on a basic shared hosting plan with plain CGI

I’ve successfully installed Django on my Hostgator account (‘Baby Croc’ plan) using these steps. Hostgator has been really great for me, but they don’t support fastCGI. Using vanilla CGI with Django is not generally recommended and results in crappy performance, but for a low traffic site it’s bearable (barely).

 

Here are some links to source material for this document:

 

http://www.djangoproject.com/documentation/install/

http://code.djangoproject.com/ticket/2407

http://mysql-python.sourceforge.net/

http://www.python.org/dev/peps/pep-0333/

http://www.djangoproject.com/documentation/modpython/#serving-the-admin-files

 

Prerequisites

 

SSH shell access to your account

Python 2.3+ (Python 2.4 preferred)

MySQLdb module (ask your support person to install this)

Some knowledge of Django and Python

 

Install django and create a project directory

 

Create a directory in your home directory called “django” (or whatever) that is outside of your document root.

 

mkdir ~/django

cd ~/django

svn co http://code.djangoproject.com/svn/django/trunk/django

 

Create a directory for your Django projects.

 

mkdir ~/django/projects

 

You should now have the following folders:

 

~/django/django

~/django/projects

 

Edit your .bash_profile and add the following:

 

export PATH=”.:/home/username/django/django/bin:$PATH”

export PYTHONPATH=/home/username/django

Then enable the changes:

source .bash_profile

 

Add the following script to your cgi-bin directory and name it wcgi.py

 

#!/usr/local/bin/python

 

import os, sys

 

sys.path.insert(0, “/home/username/django/”)

sys.path.insert(0, “/home/username/django/projects”)

sys.path.insert(0, “/home/username/django/projects/newprojects”)

 

import django.core.handlers.wsgi

 

os.chdir(“/home/username/django/projects/newproject”)    # optional

os.environ[‘DJANGO_SETTINGS_MODULE’] = “newproject.settings”

 

def runcgi():

    environ                      = dict(os.environ.items())

    environ[‘wsgi.input’]        = sys.stdin

    environ[‘wsgi.errors’]       = sys.stderr

    environ[‘wsgi.version’]      = (1,0)

    environ[‘wsgi.multithread’]  = False

    environ[‘wsgi.multiprocess’] = True

    environ[‘wsgi.run_once’]     = True

 

    application = django.core.handlers.wsgi.WSGIHandler()

   

    if environ.get(‘HTTPS’,’off’) in (‘on’,’1′):

        environ[‘wsgi.url_scheme’] = ‘https’

    else:

        environ[‘wsgi.url_scheme’] = ‘http’

   

    headers_set  = []

    headers_sent = []

   

    def write(data):

        if not headers_set:

             raise AssertionError(“write() before start_response()”)

       

        elif not headers_sent:

             # Before the first output, send the stored headers

             status, response_headers = headers_sent[:] = headers_set

             sys.stdout.write(‘Status: %s\r\n’ % status)

             for header in response_headers:

                 sys.stdout.write(‘%s: %s\r\n’ % header)

             sys.stdout.write(‘\r\n’)

       

        sys.stdout.write(data)

        sys.stdout.flush()

   

    def start_response(status,response_headers,exc_info=None):

        if exc_info:

            try:

                if headers_sent:

                    # Re-raise original exception if headers sent

                    raise exc_info[0], exc_info[1], exc_info[2]

            finally:

                exc_info = None     # avoid dangling circular ref

        elif headers_set:

            raise AssertionError(“Headers already set!”)

       

        headers_set[:] = [status,response_headers]

        return write

   

    result = application(environ, start_response)

    try:

        for data in result:

            if data:    # don’t send headers until body appears

                write(data)

        if not headers_sent:

            write(”)   # send headers now if body was empty

    finally:

        if hasattr(result,’close’):

            result.close()

 

runcgi()

 

Edit your root .htaccess file and add the following lines

 

RewriteEngine on

RewriteRule ^media – [L]

RewriteRule ^cgi-bin – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ /cgi-bin/wcgi.py/$1 [QSA,L]

 

Create a new django project

 

(or upload your existing project)

It might be a good idea to create a new dummy project just to have a baseline to test your installation.

 

cd ~/django/projects

django-admin.py startproject newproject

cd newproject

manage.py startapp newapp

 

Try it out

 

Since DEBUG is True by default (in settings.py) you should get the default Django 404 page.

Make sure you set DEBUG=False before you open your site to the public.

 

Notes:

 

The admin media files reside in django/contrib/admin/media and since they aren’t accessible from your document root you need to create a symbolic link to the media files (or just copy them):

 

ln -s ~/django/django/contrib/admin/media ~/www/media

 

Where “~/www/” is your document root.

By gharib larbi

I am an electrical engineer who loves programming. I like drawing and think and imagine my next video about cooking or how to fix my laptop or my cell. I gave myself a goal to fly a plane. I still have a lot of ideas to come.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.