<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
          "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">


<article lang="fr">
  
 <articleinfo>
  <title>
  Installing/Configuring/Caching Django on your Linux server
  </title>
    
  <subtitle>
  Gazette&nbsp;Linux n°180 &mdash; novembre 2010
  </subtitle>
    
  <author>
    <firstname>Anderson</firstname>
    <surname>Silva</surname>
    <email>afsilva CHEZ gmail POINT com</email>
  </author>
  
  <author>
    <firstname>Steve</firstname>
    <othername>'Ashcrow'</othername>
    <surname>Milner</surname>
  </author>
    
  <othercredit role="traduction" class="translator">
   <firstname>Prénom</firstname>
   <surname>Nom du traducteur</surname>
   <contrib>Adaptation française </contrib>
   <email>adresse_électronique CHEZ fournisseur POINT code_pays</email>
  </othercredit>
    
  <othercredit role="relecture" class="translator">
    <firstname>Prénom</firstname>
    <surname>Nom du relecteur</surname>
    <contrib>Relecture de la version française </contrib>
    <email>adresse_électronique CHEZ fournisseur POINT code_pays</email>
  </othercredit>
    
  <legalnotice>
    <para>Article paru dans le n°180 de la Gazette&nbsp;Linux de novembre 2010.</para>
    <para>Cet article est publié selon les termes de la <ulink url="http://linuxgazette.net/copying.html">Open Publication License</ulink>.
    La <citetitle>Linux&nbsp;Gazette</citetitle> n'est ni produite, ni sponsorisée, ni avalisée par notre hébergeur principal, SSC, Inc.</para>
  </legalnotice>
    
  <copyright>
    <year>2010</year>
    <holder>Anderson Silva</holder>
    <holder>Steve 'Ashcrow' Milner</holder>
  </copyright>
    
  <copyright>
    <year>Année de traduction</year>
    <holder>Prénom Nom du traducteur</holder>
  </copyright>
    
  <copyright>
    <year>Année de relecture</year>
    <holder>Prénom Nom du relecteur</holder>
  </copyright>
 </articleinfo>
  

<para>
In today's world, web development is all about turnaround. Businesses want to
 maximize production outcome while minimizing development and production time. 
Small, lean development teams are increasingly becoming the norm in large 
development departments. Enter <ulink url="http://www.djangoproject.com/">Django</ulink>: 
a popular <ulink url="http://www.python.org/">Python</ulink> web framework 
that invokes the <acronym>RWAD</acronym> (rapid web application development) and <acronym>DRY</acronym> (don't repeat 
yourself) principles with clean, pragmatic design.
</para>

<para>
This article is not about teaching you how to program in <application>Python</application>, nor how to use 
the Django framework. It's about showing how to promote your Django applications
 onto an existing <application>Apache</application> or <application>Lighttpd</application> environment.
</para>

<para>
We will conclude with a simple way that you can improve the performance of your 
Django application by using caching to speed up access time. This article also 
assumes that you are running Fedora as your web application server, but all the 
packages mentioned in this article are also available in many OS's including under the 
<ulink url="http://fedoraproject.org/wiki/EPEL">Extra Packages for Enterprise Linux
 repository</ulink>, which means these instructions should also be valid under 
<systemitem class="osname">>Red Hat Enterprise Linux</systemitem> or <systemitem class="osname">CentOS</systemitem> servers.
</para>

<section id="what_you_need"><title>What you need</title>

<para>
You must have Django installed:
<programlisting>
$ yum install Django
</programlisting>
</para>

<para>
If you want to serve Django apps under <application>Apache</application> you will need 
<ulink url="http://code.google.com/p/modwsgi/">mod_wsgi</ulink>:
<programlisting>
$ yum install httpd mod_wsgi
</programlisting>
</para>

<para>
If you want to serve Django apps under <application>Lighttpd</application>:
<programlisting>
$ yum install lighttpd lighttpd-fastcgi python-flup
</programlisting>
</para>

<para>
Installing memcached to <quote>speed up</quote> Django apps:
<programlisting>
$ yum install memcached python-memcached
</programlisting>
</para>
</section>

<section id="new_django_project"><title>Starting a new Django project</title>

<procedure>

<step>
<para>
Create a development workspace.
<programlisting>
$ mkdir -p $LOCATION_TO_YOUR_DEV_AREA 
$ cd $LOCATION_TO_YOUR_DEV_AREA
</programlisting>
</para>
</step>

<step>
<para>
Start a new base Django project. This creates the boiler plate project 
structure.
<programlisting>
$ django-admin startproject my_app
</programlisting>
</para>
</step>

<step>
<para>
Start the Django development web server on port 8080 (or whatever other port 
you'd like).

<note>
<para>
The development web server is just for testing and verification. Do not 
use it as a production application server!
</para>
</note>
<programlisting>
$ python manage.py runserver 8080
</programlisting>
</para>
</step>

<step>
<para>
Run your Django project under <application>Apache</application> with mod_wsgi by enabling mod_wsgi.
<emphasis>Note</emphasis> that to do this you will need to have your project in an SELinux
friendly location (don't use home directories!) as well as readable by <application>apache</application>.
On Fedora mod_wsgi is auto added upon install via <filename>/etc/httpd/conf.d/wsgi.conf</filename>.
Upon restarting of <application>apache</application>, the module will be loaded.
</para>
</step>

<step id="create_virtual_hosts" xreflabel="5">
<para>
Create virtual hosts by creating a new file at 
<filename>/etc/httpd/conf.d/myapp.conf</filename>.
<programlisting>WSGIScriptAlias / /path/to/myapp/apache/django.wsgi

DocumentRoot /var/www/html/
ServerName your_domain_name
ErrorLog logs/my_app-error.log
CustomLog logs/my_app-access_log common
</programlisting>
</para>
</step>

<step>
<para>
In step <xref linkend="create_virtual_hosts"/> we defined a script alias and now we need to create the wsgi file
it references.
<programlisting>import os
import sys

import django.core.handlers.wsgi

# Append our project path to the system library path
sys.path.append('/path/to/')

# Sets the settins module so Django will work properly
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

# sets application (the default wsgi app) to the Django handler
application = django.core.handlers.wsgi.WSGIHandler()
</programlisting>
</para>
</step>
</procedure>

</section>

<section id="run_django_project"><title>Running your Django project under <application>Lighthttpd</application> with fastcgi</title>

<para>
The first thing you must do is start up your FastCGI server.

<programlisting>./manage.py runfcgi method=prefork socket=/var/www/myapp.sock pidfile=django_myapp.pid</programlisting>
</para>

<para>
Then modify your <filename>lighttpd.conf</filename> file to use the FastCGI server.
<programlisting>
server.document-root = "/var/www/django/"
fastcgi.server = (
&nbsp;&nbsp;&nbsp; "/my_app.fcgi" =&gt; (
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "main" =&gt; (
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Use host / port instead of socket for TCP fastcgi
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # "host" =&gt; "127.0.0.1",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # "port" =&gt; 3033,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "socket" =&gt; "/var/www/my_app.sock",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "check-local" =&gt; "disable",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )
&nbsp;&nbsp;&nbsp; ),
)
alias.url = (
&nbsp;&nbsp;&nbsp; "/media/" =&gt; "/var/www/django/media/",
)
url.rewrite-once = (
&nbsp;&nbsp;&nbsp; "^(/media.*)$" =&gt; "$1",
&nbsp;&nbsp;&nbsp; "^/favicon\.ico$" =&gt; "/media/favicon.ico",
&nbsp;&nbsp;&nbsp; "^(/.*)$" =&gt; "/my_app.fcgi$1",
)
</programlisting>
</para>

</section>

<section id="caching_django"><title>Setting up caching in Django</title>

<para>
  Django has many different caching backends, including database, memory, 
filesystem, and the ever popular memcached. According to 
<ulink url="http://www.danga.com/memcached/">http://www.danga.com/memcached/</ulink>, 
memcached is <quote>a high-performance, distributed memory object caching system, 
generic in nature, but intended for use in speeding up dynamic web applications 
by alleviating database load.</quote> It's used by high traffic sites such as 
<ulink url="http://www.slashdot.org/">Slashdot</ulink> and 
<ulink url="http://www.wikipedia.com/">Wikipedia</ulink>. This makes it a prime candidate
 for caching in your cool new web app.
</para>

<para>
First, install memcached.
<programlisting>
$ yum install memcached
</programlisting>
</para>

<para>
Next, install the python bindings for memcached.
<programlisting>
$ yum install python-memcached
</programlisting>
</para>

<para>
  Next, verify that memcached is running using the memcached's init script.
<screen>
$ /etc/init.d/memcached status 
memcached (pid 6771) is running...
</screen>
</para>

<para>
  If it's not running, you can manually start it.
<programlisting>
$ /sbin/service memcached start
</programlisting>
</para>

<para>
  If you want to make sure it will automatically start every time after a reboot:
<programlisting>
$ /sbin/chkconfig --level 35 memcached on
</programlisting>
</para>

<para>
  Now that you have verified that memcached is running, you will want to tell 
your Django application to use memcached as it's caching backend. You can do 
this by adding a <literal>CACHE_BACKEND</literal> entry to your <filename>settings.py</filename> file.
<programlisting>
CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
</programlisting>
</para>

<para>
  The format is "backend://host:port/" or "backend:///path" depending on the 
backend chosen. Since we are using memcached, we have the option to run multiple
 daemons on different servers and share the cache across multiple machines. If 
you want to do this all you must do is add in the servers:port combinations in 
the <literal>CACHE_BACKEND</literal> and separate them by semicolons. In this example we share the 
cache across three different memcached servers:
<programlisting>
CACHE_BACKEND = 'memcached://127.0.0.1:11211;192.168.0.10:11211;192.168.0.11/'
</programlisting>
</para>

<para>
  For more information on the different types of caching that can be performed 
in the Django framework, please refer to their 
<ulink url="http://www.djangoproject.com/documentation/cache/">official documentation</ulink>.
</para>

<para>
  Finally, whenever you are ready to get your applications into production, 
you can always write your own Django 
<ulink url="http://code.djangoproject.com/wiki/InitdScriptForLinux">service script</ulink>, 
so your application can start up at boot time.
</para>

<para>
  The original article was published on June 5th, 2008 by Red Hat Magazine,
 and revised for the 2010 November Issue of Linux Gazette.
</para>

</section>

<bibliography id="bibliography"><title>À propos des auteurs</title>
<bibliomixed>
<authorblurb><title>Anderson Silva</title>
<para>
Anderson Silva works as an IT Release Engineer at Red Hat, Inc. He holds a
BS in Computer Science from Liberty University, a MS in Information Systems
from the University of Maine. He is a Red Hat Certified Architect and has
authored several Linux based articles for publications like: Linux Gazette,
Revista do Linux, and Red Hat Magazine. Anderson has been married to his
High School sweetheart, Joanna (who helps him edit his articles before
submission), for 11 years, and has 3 kids. When he is not working or
writing, he enjoys photography, spending time with his family,  road
cycling, watching Formula 1 and Indycar races, and taking his boys karting,
</para>
</authorblurb>
<authorblurb><title>Steve 'Ashcrow' Milner</title>
<para>
Steve 'Ashcrow' Milner works as a Security Analyst at Red Hat, Inc. He
 is a Red Hat Certified Engineer and is certified on ITIL Foundations.
 Steve has two dogs, Anubis and Emma-Lee who guard his house. In his
 spare time Steve enjoys robot watching, writing open code, caffeine,
 climbing downed trees and reading comic books.
</para>
</authorblurb>
</bibliomixed>
</bibliography>

<colophon id="a-propos-de-la-gazette">

<title>Adaptation française de la Gazette Linux</title>

<para>
L'adaptation française de ce document a été réalisée dans le cadre du <citetitle>Projet de traduction de la Gazette&nbsp;Linux</citetitle>.

</para>

<para>
Vous pourrez lire d'autres articles traduits et en apprendre plus sur ce projet en visitant notre site&nbsp;: <ulink
url="http://www.traduc.org/Gazette_Linux"/>.
</para>

<para>
Si vous souhaitez apporter votre contribution, n'hésitez pas à nous rejoindre, nous serons heureux de vous accueillir.
</para>

</colophon>


</article>
