giovedì 5 aprile 2012

Django 1.4 for Ubuntu 12.04

Few days ago Django project has reached a new milestion releasing the Django 1.4.
Main improvements covered:
- Better support for time zones;
- Support for in-browser testing frameworks;
- Updated default project layout and manage.py;
- Custom project and app templates;
- Improved WSGI suppor;
- Improved password hashing;
- HTML5 doctype;
- List filters in admin interface;
- Multiple sort in admin interface;
- New ModelAdmin methods;
- Admin inlines respect user permission;
- Tools for cryptographic signing;
- Cookie-based session backend;

So I decided to try it on my brand new Ubuntu 12.04 server machine.
Django 1.4 requires python 2.5 but Ubuntu comes with python 2.7, so this requirement is satisfied.

1. Install the server

Unfortunately on Ubuntu 12.04 Django 1.4 is not yet packaged so we need to install it manually:

 wget "http://www.djangoproject.com/download/1.4/tarball/" -O Django-1.4.tar.gz
 tar xzvf Django-1.4.tar.gz
 cd Django-1.4
 sudo python setup.py install

After that you can install your preferred web server:

 apt-get install apache2
 apt-get install libapach2-mod-wsgi

2. Test if Django is working

Ok now let’s configure your 1st Django site. We assume your site will be called "HelloDjango".

 cd /var/www/
 django-admin.py startproject HelloDjango

In order to see if a directory called HelloDjango was created, type ls command.

 ls
 cd HelloDjango

Now we create a wsgi file for the site typing:

mkdir apache
vi ./apache/django.wsgi

Add to the file this content:

import os
import sys
 
path = '/var/www'
if path not in sys.path:
    sys.path.append(0, path)
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'HelloDjango.settings'
 
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Configure Apache

Now it's time to create a brand new Virual Host configuration. So create a file called HelloDjango

vi /etc/apache2/sites-available/HelloDjango

Put this lines to that file

<VirtualHost *:80>
 
    ServerName HelloDjango.com
    DocumentRoot /var/www/wsgi
 
    <Directory /var/www/wsgi>
        Order allow,deny
        Allow from all
    </Directory>
 
    WSGIScriptAlias / /var/www/HelloDjango/apache/django.wsgi
 
</VirtualHost>

Activate the site and restart Apache

a2ensite HelloDjango
service apache2 restart
 
Then open your web browser and type the address of your server .. you should see the Django default installation message.

That's All and now enjoy the Django web framework on your Ubuntu system.

6 commenti:

Anonimo ha detto...

Olá amigo, no seu tutorial existe um erro.
Na linha de instalação do "mod-wsgi" está escrito "libapach-mod", quando deveria ser "libapache-mod".

Unknown ha detto...

I have some questions. I was unable to make this work; firstly, in my apache error logs, i had wxgi complaining that HelloDjango.settings does not exist (it doesnt) and secondly, I found that you were pointing the virtual host file to /var/www/wxgi. Surely this should be /var/www/HelloDjango?


Somebody has already noted the spelling mistake installing libapache2-mod-wxgi.

Unknown ha detto...

Did't work for me. I got a 500 error when I run HelloDjango.com. Shouldn't be a database configuration somewhere?

Unknown ha detto...

Internal error was caused by django.wsgi

this line was wrong in python 2.7.x
sys.path.append(0, path)

should be sys.path.append(path)

Unknown ha detto...

I got 500 too. (0, path) -> (path) no help at all. Any other ideas?

Unknown ha detto...

Solved. Just add to django.wsgi after "sys.append(path)" also "sys.append('/var/www/HelloDjango')"