Thursday, October 6, 2011

Installing Django over Zend Server with mod_wsgi

The architecture
We will guide you through the installation of a web server based RHEL 5 to run Django websites using Apache 2 (from Zend Server CE), Python 2.7 with virtualenv, linked by an WSGI interface.


The Tools


Zend Server CE


For our development environment, we are using the Zend Server CE, which besides coming with an administration interface out-of-the-box, it's a complete and stable web server stack, offering apache 2, php, lighttpd, etc.
The installer guides you throw a few installation steps and which, in the end, puts your new server up and running and tells you the address where the hosted pages and the administration page are available, tipically https://localhost:10088 and https://localhost:10082 respectively.
Apart from the functional point of view, when using the installer, Zend Server CE completely installs under the directory you've chosen. That means minimal system modification and better organization.

Python 2.7 and Virtualenv


We want to take advantage of the new features and performance improvements of Python 2.7. Unfortunately RHEL5 ships with Python 2.4 only. Python 2.7 was installed from source in it's own directory, leaving system Python untouched, while alias enable transparent access to the latest binaries.

Furthermore, in order to run out web page with it's dedicated python libraries, we used Virtualenv.

mod_wsgi


It implements a simple to use Apache module which can host any Python application which supports the Python WSGI interface, In our case DJango.


Installation steps
Configure Zend Server CE
  1. Download it from http://www.zend.com/en/products/server-ce/downloads. Choose the tar.gz file.
  2. Extract it
    $ tar -xzf
  3. Install it
    $ cd ZendServer...
    $ ./install.sh
Install python 2.7
  1. Install pysqlite-devel
    $yum install sqlite-devel
  2. Installing Python 2.7 into alternate location since we don’t want to break Centos 5.6 (yum) that uses Python 2.4
  3. $ cd /usr/src/python2.7/
    $ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
    $ tar zxvf Python-2.7.2.tgz
    $ cd Python-2.7.2
    $ ./configure --prefix=/opt/python2.7 --with-threads --enable-shared
    $ make
    $ make install

  4. Creating symbolic link to the alternate Python version
  5. $ ln -s /opt/python2.7/bin/python /usr/bin/python2.7
    $ echo '/opt/python2.7/lib'>> /etc/ld.so.conf.d/opt-python2.7.conf
    $ ldconfig

  6. Let’s test if new Python version works/usr/bin/python2.7

  7. If successful you will see something like this:
    Python 2.7.2 (default, Sep 3 2011, 18:28:42)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2

    press control+D to exit

  8. Now we need to install Python setup-tools
  9. cd /usr/src/python2.7/
    wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
    sh setuptools-0.6c11-py2.7.egg --prefix=/opt/python2.7

  10. Installing virtualenv to our Python 2.7
  11. cd /opt/python2.7/bin
    ./easy_install virtualenv

  12. Create Python links and alias to newest version (optional but recommended)
    $ ln -s /usr/local/python2.7/bin/easy_install-2.7 /usr/bin/easy_install-2.7
    $ ln -s /usr/local/python2.7/bin/easy_install-2.7 /usr/bin/easy_install-2.7
    $ ln -s /usr/local/python2.7/bin/virtualenv-2.7 /usr/bin/virtualenv-2.7
    $ echo "alias python=python2.7
              alias pip=pip-2.7
              alias easy_install=easy_install-2.7
              alias virtualenv=virtualenv-2.7" >> ~/.bashrc
    $ source ~/.bashrc
Installing mod_wsgi (this module will work only with python 2.7)
    $ cd /opt/python2.7/lib/python2.7/config/
    $ ln -s ../../libpython2.7.so
    $ cd /usr/src/python2.7/
    $ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
    $ tar zxvf mod_wsgi-3.3.tar.gz
    $ cd mod_wsgi-3.3
    $ ./configure --with-python=/opt/python2.7/bin/python
    $ make
    $ make install
Configure System + Apache
  1. Create a directory structure for static/php pages, wsgi scripts and django sites
    $ mkdir /var/www
    $ mkdir /var/www/html
    $ mkdir /var/www/wsgi-scripts
    $ mkdir /var/www/sites
  2. Create python virtual environment for django websites and activate it
    $ mkdir /usr/local/python2.7/virtualenvs; cd /usr/local/python2.7/virtualenvs
    $ virtualenv --no-site-packages --distribute websites
    $ source websites/bin/activate
  3. Install core Django and required site libraries
    $ pip install django
    $ wget http://bitbucket.org/jespern/django-piston/downloads/django-piston-0.2.2.tar.gz
    $ tar -xzf django-piston-0.2.2.tar.gz
    $ cd django-piston
    $ python setup.py install
    $ pip install django-grappelli
    $ pip install django-filebrowser
  4. Configure mod_wsgi.
    Create file wsgi.conf under /usr/local/zend/apache2/conf.d/ with contents:
    #The aim of mod_wsgi is to implement a simple to use Apache module
    #which can host any Python application which supports the Python WSGI interface.
    #
    LoadModule wsgi_module modules/mod_wsgi.so
    AddHandler wsgi-script .wsgi
    WSGIPythonHome /usr/local/python2.7/virtualenvs/websites

    ######## Configuration entries ##############

        Order allow,deny
        Allow from all


    # WSGI aliases
    WSGIScriptAlias /radioclass /var/www/wsgi-scripts/radioclass.wsgi
  5. Create the website wsgi script for the site
    create file as specified before ( /var/www/wsgi-scripts/radioclass.wsgi ) with contents:
    import os
    import sys
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
    ys.path.insert(0, '/var/www/sites/radioclass/')

    #Load WSGI Handler
    from django.core.handlers.wsgi import WSGIHandler
    application = WSGIHandler()
  6. Create (or move) your django website for the specified directory (/var/www/sites/radioclass/)
    $ cd /var/www/sites/
    $ django-admin startproject radioclass
  7. Apache settings and reload
    check /usr/local/zend/apache2/conf/httpd.conf, and eventually change ListenPort to 80
    restart zend
    $ /usr/local/zend/bin/zendctl restart

1 comment: