Setup Django Web App in Godaddy linux economy host


In case you want to use Django framework in Godaddy’s Linux Economy Host, here is the steps:
1. Godaddy has virtualenv installed, so first, create a virtual environment venv: (I use $HOME/lib/ for all the installed stuff below)
cd ~/
mkdir lib
cd lib
virtualenv --no-site-packages venv
The python package folder is $HOME/lib/venv/lib/python2.7/site-packages
2. Install the latest Django through pip
pip install Django
3. After successfully installed Django 1.4, create a new project
django-admin.py startproject mysite
4. Because Django 1.4 only support MySQLdb 1.2.1 or above, and Godaddy only has 1.2.0, so we cannot use MySQL. Let’s use sqlite for our database
cd mysite
sqlite3 mydatabase.db ""
5. change the database configuration in mysite/setting.py file
(1) when set the path for the database file, please use the absolute path
(2) set the Locale information in $HOME/.bash_profile file, otherwise you cannot set the superuser when you sync the database.
    export LANG=en_US.UTF-8
    export LC_ALL=en_US.UTF-8
run $HOME/.bash_profile:
% source ~/.bash_profile
(3) now you should sync the database:
% python2.7 manage.py syncdb
There should be no error if you did (2)
6. Now we setup the dispatch functionality so we can access the webpage without running server through Django, we use flup here
cd ~/lib/venv/lib/python2.7/site-packages
wget http://pypi.python.org/packages/source/f/flup/flup-1.0.2.tar.gz#md5=24dad7edc5ada31dddd49456ee8d5254
tar -xvzf flup-1.0.2.tar.gz
mv flup-1.0.2/flup/ .
And you can delete the flup-1.0.2 folder if you want.
7. in $HOME/html folder, create dispatch.py and add the following codes:
#!/usr/local/bin/python2.7
import sys, os
sys.path += ['/your/home/path/lib/venv/lib/python2.7/site-packages']
sys.path += ['/your/home/path/lib/mysite/']
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from flup.server.fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
WSGIServer(WSGIHandler()).run()
8. in $HOME/html/.htaccess file, add the following codes:
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# everything else sent to django
RewriteRule ^(dispatch\.py/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.py/$1 [L]
9. Access your website http://your.website.com, it should work

addtional

1. add static path in mysite/settings.py, so Godaddy host can read the static folder correctly
from django.conf import settings

urlpatterns = patterns('',
    url(r'^static/(?P.*)$', 'django.views.static.serve',
 {'document_root': settings.STATIC_ROOT,
 'show_indexes': True}),
    #your other url settings
)
Important
Don’t forget to set the STATIC_ROOT and run % python2.7 manage collectstatic first.

Comments

Popular Posts