Contact Us
 
 
 
 
  Browse by category :
 

Bob and the Knowledge Factory / Apache Web Server / General Issues / DJANGO

DJANGO

Add comment
Views: 493
Votes: 0
Comments: 0
Posted: 17 May, 2010
by: Revathi R.
Updated: 20 May, 2010
by: Harish R.

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

A high-level Web framework is software that eases the pain of building dynamic Web sites. It abstracts common problems of Web development and provides shortcuts for frequent programming tasks.

Django grew organically from real-world applications written by a Web development team in Lawrence, Kansas. It was born in the fall of 2003, when the Web programmers at the Lawrence Journal-World newspaper, Adrian Holovaty and Simon Willison, began using Python to build applications.

Django is now an open source project with contributors across the planet, the original World Online developers still provide central guidance for the framework’s growth, and World Online contributes other important aspects such as employee time, marketing materials, and hosting/bandwidth for the framework’s Web site

Django was designed to handle two challenges: the intensive deadlines of a newsroom and the stringent requirements of the experienced Web developers who wrote it. It lets you build high-performing, elegant Web applications quickly.

How we can setup Django in cPanel Server?

In order to setup Django, there are certain server requirements. Lets move on to that.

Requirements

1. Python

2. mod_wsgi

3. Apache

4. Django

Install Python

Being a Python Web framework, Django requires Python.

It works with any Python version from 2.4 to 2.6 (due to backwards incompatibilities in Python 3.0, Django does not currently work with Python 3.0 ).

Before installing Python, its dependencies needed to be installed in the server.

Installing Python 2.5 dependencies

Install sqlite3:

$ wget http://www.sqlite.org/sqlite-amalgamation-3.6.4.tar.gz
$ tar xvfz sqlite-amalgamation-3.6.4
$ cd sqlite-amalgamation-3.6.4.tar.gz
$ ./configure
$ make
$ make install

Install Python 2.5

Download Python 2.5 source package, unzip, and enter working directory:

$ cd
$ wget http://python.org/ftp/python/2.5/Python-2.5.tgz
$ tar xvfz Python-2.5.tgz
$ cd Python-2.5

Configure Python 2.5 so that we're not overwriting the system's python installation:

$ ./configure --prefix=/opt/python2.5 --with-threads --enable-shared

Make and install:

$ make
$ make install

Add an alias to root's .bash_profile:

alias python='/opt/python2.5/bin/python'

Make a symbolic link:

$ ln -s /opt/python2.5/bin/python /usr/bin/python2.5

Configure ld to find your shared libs:

$ cat >> /etc/ld.so.conf.d/opt-python2.5.conf
/opt/python2.5/lib (hit enter)
(hit ctrl-d to return to shell)
$ ldconfig

Test python 2.5 installation:

$ python

You should get an interactive Python 2.5 session like:

Python 2.5 (r25:51908, Nov 9 2008, 23:18:24)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Exit

Install setuptools:

--------------------

Setuptools is a Python module that helps to compile, distribute and install Python packages by wrapping them into "egg"s. Eggs contain additional information to process dependencies, etc.

$ cd
$ wget http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py2.5.egg
$ sh setuptools-0.6c9-py2.5.egg --prefix=/opt/python2.5

Install MySQLdb package:

--------------------------------

If you plan to use Django’s database API functionality, you’ll need to make sure a database server is running. Django supports many different database servers and is officially supported with PostgreSQL, MySQL, Oracle and SQLite (although SQLite doesn’t require a separate server to be running).

In addition to a database backend, you’ll need to make sure your Python database bindings are installed.

  • If you’re using PostgreSQL, you’ll need the psycopg package. Django supports both version 1 and 2. (When you configure Django’s database layer, specify either postgresql [for version 1] or postgresql_psycopg2 [for version 2].)

    If you’re on Windows, check out the unofficial compiled Windows version.

  • If you’re using MySQL, you’ll need MySQLdb, version 1.2.1p2 or higher. You will also want to read the database-specific notes for the MySQL backend.

  • If you’re using SQLite and Python 2.4, you’ll need pysqlite. Use version 2.0.3 or higher. Python 2.5 ships with an SQLite wrapper in the standard library, so you don’t need to install anything extra in that case. Please read the SQLite backend notes.

  • If you’re using Oracle, you’ll need a copy of cx_Oracle, but please read the database-specific notes for the Oracle backend for important information regarding supported versions of both Oracle and cx_Oracle.

     

Here we go for MySQL, hence we need MySQLdb

$ cd
$ wget http://internap.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.2.tar.gz
$ tar xvfz MySQL-python-1.2.2.tar.gz
$ cd MySQL-python-1.2.2
$ python setup.py build
$ python setup.py install

Verify Python 2.5 Installation:

$ cd
$ python
Python 2.5 (r25:51908, Nov 9 2008, 23:18:24)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> import MySQLdb
>>>

Installing mod_wsgi

In order to use Django on a production site, use Apache with mod_wsgi. The mod_wsgi is similar to mod_perl – it embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements. Make sure you have Apache installed, with the mod_wsgi module activated. Django will work with any version of Apache that supports mod_wsgi.

Configure mod_wsgi to link with Python 2.5 shared libs:

$ cd /opt/python2.5/lib/python2.5/config
$ ln -s ../../libpython2.5.so .
$ cd
$ wget http://modwsgi.googlecode.com/files/mod_wsgi-2.3.tar.gz
$ tar xvfz mod_wsgi-2.3.tar.gz
$ cd mod_wsgi-2.3
$ ./configure --with-python=/opt/python2.5/bin/python

Make and install:

$ make
$ make install

Add mod_wsgi to Apache 2.x:

Simply add these lines in the apache configuration file.

LoadModule wsgi_module /usr/local/apache/modules/mod_wsgi.so
AddHandler wsgi-script .wsgi
Setup a Django 1.0 test project

Create a new user/domain via WHM/cPanel

Edit users .bash_profile:

alias python='/opt/python2.5/bin/python'
export PYTHONPATH='$PYTHONPATH:/home/username/sites/domain.com'

Create a new project directory:

$ mkdir -p /home/username/sites/domain.com
$ cd /home/username/sites/domain.com
$ svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk
$ ln -s django-trunk/django django
$ mkdir .python-eggs
$ chmod 777 .python-eggs
$ /home/username/sites/domain.com/django/bin/django-admin.py startproject testproject
$ chown -R username: /home/username/sites

Create your apps .wsgi script:

$ pico /home/username/public_html/test.wsgi
#!/opt/python2.5/bin/python
import os, sys
sys.path.insert(0,'/home/username/sites/domain.com')
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
os.environ['PYTHON_EGG_CACHE'] = '/home/username/sites/domain.com/.python-eggs'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
$ chown username: /home/username/public_html/test.wsgi

Setup a vhost include (as root)

$ mkdir -p /usr/local/apache/conf/userdata/std/2/username/domain.com
$ pico /usr/local/apache/conf/userdata/std/2/username/domain.com/vhost.conf
<IfModule mod_alias.c>
Alias /robots.txt /home/username/sites/domain.com/testproject/media/robots.txt
Alias /site_media /home/username/sites/domain.com/testproject/media
Alias /admin_media /home/username/sites/domain.com/django/contrib/admin/media
</IfModule>
<IfModule mod_wsgi.c>
# See the link below for an introduction about this mod_wsgi config.
# http://groups.google.com/group/modwsgi/browse_thread/thread/60cb0ec3041ac1bc/2c547b701c4d74aa
WSGIScriptAlias / /home/username/public_html/test.wsgi
WSGIDaemonProcess username processes=7 threads=1 display-name=%
WSGIProcessGroup username
WSGIApplicationGroup %
</IfModule>
# This fixes the broken ErrorDocument directive we inherit that breaks auth
# if we use a WSGI app.
ErrorDocument 401 "Authentication Error"
ErrorDocument 403 "Forbidden"
$ /scripts/verify_vhost_includes
$ /scripts/ensure_vhost_includes --user=username

domain.com should now be serving a django site.

To restart a django instance:

$ touch ~/public_html/test.wsgi

 

 

Others in this Category
document Apache failed to start with the "No space left on device" error.
document Enabling WebDav
document Fsck.ext3: Unable to resolve UUID
document Upgrading Openssh on CentOS And Chrooting a User When Connecting via SFTP
document Mod Security



RSS
Content is available under Creative Commons Attribution-Share Alike 3.0 License.