Exploring Django and the Real-time Web

http://arunrocks.com/real-time-applications-and-will-django-adapt-to-it/

use gevent or else go with separate node/socket.io and maybe redis – but then how to deal with auth across both?

The Zen of Python is always good to review

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

Installing Django on Ubuntu 10.04 LTS

check your hostname is set properly:

>hostname
>hostname -f

update the server:

apt-get update
>apt-get upgrade

install apache and mod_wsgi:

>sudo apt-get install apache2 libapache2-mod-wsgi

and install setup tools for python, and pip:

> sudo apt-get install python-setuptools
> sudo apt-get install python-pip

Pick a database:

>apt-get install mysql-server python-mysqldb
>apt-get install postgresql python-psycopg2
>apt-get install sqlite3 python-sqlite

Now install Django:

> sudo pip install django

and prepare the folders you will need :

> sudo mkdir /srv/www
> sudo mkdir /srv/www/wsgi
> sudo mkdir /srv/www/public_html

create a file to test if wsgi working:
sudo vim /srv/www/wsgi/app.wsgi

putting this content into the app.wsgi file:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]

create a new apache site to test wsgi:
sudo vim /etc/apache2/sites-available/wsgi

and add this content to the site, replacing “myDocRootUrl” as appropriate — NOTE: bug in my blog software, so left out the opening less than sign on the virtual host tags:
VirtualHost *:80>

ServerName myDocRootUrl.wsgi
DocumentRoot /srv/www/wsgi

Order allow,deny
Allow from all

WSGIScriptAlias / /srv/www/wsgi/app.wsgi

/VirtualHost>

Next, check for and remove any default sites in sites-enabled folder:

>ls /etc/apache2/sites-available
>rm whatever defaults in there...

Now activate the test of wsgi site:
> sudo a2ensite wsgi
> sudo /etc/init.d/apache2 reload

Now visit the url you specified as myDocRootUrl.wsgi and you should see “Hello World!”

If not, first thing, check your apache error log (you likely need to change to root user here, not just sudo):

>sudo su -
>cat /var/log/apache2/error.log

Create django project:

> cd /srv/www
> sudo django-admin.py startproject myProject

Your project folder should now contain these files:
__init__.py manage.py settings.py urls.py

Create a wsgi file for the project (rename myProject as appropriate):

> sudo mkdir /srv/www/myProject/apache
> sudo vim /srv/www/myProject/apache/django.wsgi

and put this text in that django.wsgi file (rename myProject as appropriate):
import os
import sys

path = '/srv/www'
if path not in sys.path:
sys.path.insert(0, '/srv/www')

os.environ['DJANGO_SETTINGS_MODULE'] = 'myProject.settings'

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

Create a new apache sites-available for your django project:

>sudo vim /etc/apache2/sites-available/myProject

and add starter virtual host content to it (rename myProject as appropriate)(virtual hosts tags missing leading <):

VirtualHost *:80>

ServerName myDocRootUrl.myProject
DocumentRoot /srv/www/myProject

Order allow,deny
Allow from all

WSGIDaemonProcess myDocRootUrl.myProject processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup myDocRootUrl.myProject

WSGIScriptAlias / /srv/www/myProject/apache/django.wsgi

/VirtualHost>

Activate the new project site:
> sudo a2ensite myProject
> sudo /etc/init.d/apache2 reload

browse to your project url and hopefully you see the default django welcome message!

Sources:
Mostly this article came from below, but I added to it:
http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

%d bloggers like this: