Monday, January 5, 2015

Set up Django on shared hosting environment - Hostgator

Before setting up Django on Hostgator, ensure you have access to shell. Hostgator grants jailed shell access.

1. Create a virtual environment, activate

Hostgator provides multiple versions of virtualenv. Check and use latest available version. Being a shared hosting, we do not have permissions to global install packages. We will have to live with packages provided.

Create virtual environment with --system-site-packages option to make use of system python packages eg. mysql-python

# Create virtual environments directory
cd ~
mkdir -p virtualenvs

cd virtualenvs

# Create virtual environement
/opt/python27/bin/virtualenv-2.7 --system-site-packages myenv


# Activate the virtual environment
source ~/virtualenvs/myenv/bin/activate


2. Create Django project

Hostgator provides an older version of Django

pip freeze | grep Django 
#Django==1.3.1


Install latest version of Django.

#Install Django 1.7
pip install Django==1.7



Follow Django tutorial to create a project.
cd ~
django-admin.py startproject mysite

Open settings file and modify as required.
vim ~/mysite/mysite/settings.py

# Modify database settings, to use mysql
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'user_testdb',
        'USER': 'user_testdbuser',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

# Add static root to server static files in the settings.py
STATIC_ROOT = '/home/user/www/static/'

3. Change .htaccess file

AddHandler fcgid-script .fcgi

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]

4. Point fcgi to your project

Put following content in index.fcgi
#!/home/user/virtualenvs/myenv/bin/python
import sys, os, user

# # Add a custom Python path.
sys.path.insert(0, "/home/user/virtualenvs/vaakya/bin/python")
sys.path.insert(0, "/home/user/mysite")
sys.path.insert(0, "/home/user/mysite/mysite")

# Switch to the directory of your project.
os.chdir("/home/user/mysite")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

5. Generate static file in the configured folder

cd ~/mysite/
python manage.py collectstatic

Done !!

9 comments:

  1. Very good information about web hosting, I think dedicated server hosting is the best one for business hosting. By the way i am a blogger and i have written an article about
    shared hosting

    ReplyDelete
  2. Very good review ! very helpful for the newbies, who just want to choose best one !
    by the here i have written an article about the bluehost dedicated server, dedicated server is one of the best one for small business & it's performs better that other hosting mode. Here the review about
    bluehost dedicated server thank

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Webcare360 Provides you safe and secure cheap Offshore Hosting Best offshore hosting and best offshore hosting with 99.9% Up time Guarantee, with Ddos protection.

    ReplyDelete
  5. Webcare360 Provides you safe and secure cheap Offshore Hosting Best offshore hosting and offshore hosting by Webcare360 with 99.9% Up time Guarantee, with Ddos protection.

    ReplyDelete
  6. I'm impressed, I must say. Very rarely do I come across a blog thats both informative and entertaining, and let me tell you, you ve hit the nail on the head. Your blog is important.. Shared Hosting

    ReplyDelete
  7. Did you realize there is a 12 word sentence you can speak to your partner... that will induce intense emotions of love and instinctual attraction to you buried inside his heart?

    That's because hidden in these 12 words is a "secret signal" that fuels a man's instinct to love, cherish and guard you with his entire heart...

    12 Words That Trigger A Man's Desire Instinct

    This instinct is so hardwired into a man's brain that it will make him work better than ever before to do his best at looking after your relationship.

    Matter-of-fact, fueling this all-powerful instinct is absolutely essential to getting the best possible relationship with your man that the instance you send your man one of the "Secret Signals"...

    ...You will instantly find him expose his soul and mind to you in such a way he haven't expressed before and he'll distinguish you as the only woman in the galaxy who has ever truly interested him.

    ReplyDelete