Django on Centos 5

From Proj3ctWiki
Jump to: navigation, search

Centos 5 utilise python 2.4.3 par défaut.

Contents

Installation de python 2.6

Pour utiliser une version plus récente, python 2.6 par exemple, on peut utiliser le repo "Extra Packages for Enterprise Linux" (EPEL). Ce repository va permettre d'installer python 2.6. La cohabitation des différentes versions ne pose aucun problème.

Installation du repo EPEL

TODO installation activation du repo EPEL

Installation de python

user@host# yum install python26

Après cela :

// Lancer la version par défaut
user@host$ python
// Lancer python 2.6
user@host$ python26

Installation de Django

Si Django a été installé via yum, il se trouve '/usr/lib/python2.4/site-packages/django' et la version 2.6 n'y pas accès. Nous allons installer une version à partir des sources et ce pour python 2.6.

Télécharger la dernière version officielle de Django

user@host# wget http://www.djangoproject.com/download/1.2.1/tarball/
user@host# tar xvfz Django-1.2.1.tar.gz
user@host# cd Django-1.2.1

Au moment de l'installation, il faut utiliser la version souhaitée de l'interpréteur, ici 2.6. Les fichiers seront alors copiés dans '/usr/lib/python2.6/site-packages/django'.

user@host# python26 setup.py install

Apache 2.2 et mod_wsgi

Par défaut, l'installation via yum de mod_wsgi ne conviendra pas, il utilisera en effet l'interpréteur par défaut à savoir la version 2.4.3.

mod_python et mod_wsgi ne fonctionnent pas ensemble, il faut désactiver mod_python. Autant le désinstaller.

user@host# yum remove mod_python

Compilation de mod_wsgi

Il nous faut donc compiler mod_wsgi en précisant la version de l'interpréteur python à utiliser. On commence par désinstaller la version existante si nécessaire. On récupère le code source de Apache et python.

user@host# yum remove mod_wsgi
user@host# yum install httpd-devel
user@host# yum install python26-devel

On télécharge les sources de mod_wsgi

user@host$ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
user@host$ tar xvfz mod_wsgi-3.3.tar.gz
user@host$ cd mod_wsgi-3.3
user@host$ ./configure --with-python=/usr/bin/python26
user@host$ make
user@host$ make install

Activation du mod

Activation du mod_wsgi (ci dessous le fichier original)

user@host# vi /etc/httpd/conf.d/wsgi.conf
#################################################################################
# mod_python and mod_wsgi compatibility note
#################################################################################
# mod_wsgi will deadlock if run in daemon mode while mod_python is enabled
# do not enable both mod_python and mod_wsgi if you are going to use the
# WSGIDaemonProcess directive
# In previous version of mod_wsgi, apache would segfault when both mod_wsgi
# and mod_python were enabled.  This update does not guarantee that will not
# happen.  
#################################################################################
# Do not enable mod_python and mod_wsgi in the same apache process.
#################################################################################

LoadModule wsgi_module modules/mod_wsgi.so
user@host# service httpd restart

Validation

Script de test

On crée un script de demo comme indiqué dans la doc.

user@host$ vi /var/www/wsgi_demo/hello.wsgi
# Attempt to import 'socket' module.
import socket
# Now for the hello world application.
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]

Alias

Créer un alias qui pointera vers le script précedent.

user@host# vi /etc/httpd/conf.d/[site].conf
// ajouter cette ligne
WSGIScriptAlias /hello /var/www/wsgi_demo/hello.wsgi

On faisant pointer le browser vers 'http://[site]/hello' un magnifique "Hello world!" apparait. Tout est OK, on peut tester Django.

Django

TODO terminer

Personal tools