Sunday, December 23, 2012

Chapter 12: Deploying Django

Deploying Django Application to a Production Server
Apache: industrial-strength Web Server

Preparing Your Codebase for Production

DEBUG = False

2. root template directory

{% extends "base.html" %}

{% block title %}Page not found{% endblock %}

{% block content %}
<h1>Page not found</h1>

<p>Sorry, but the requested page could not be found.</p>
{% endblock %}

 (not rely on other templates)
unhandled Python exception

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
<html lang="en">
    <title>Page unavailable</title>
    <h1>Page unavailable</h1>

    <p>Sorry, but the requested page is unavailable due to a
    server hiccup.</p>

    <p>Our engineers have been notified, so check back later.</p>
</html>3. Setting Up Error Alters and Setting Up Broken Link Alerts
Need Email Set up

    ('John Lennon', ''),
    ('Paul McCartney', ''),

Using Multiple for Development and Production

Three ways:

  1. Two independent settings files
  2. base setting file and inherited setting file
  3. one single with Python logic to change the settings based on context
1. Copy as

DEBUG = True

DATABASE_ENGINE = 'postgresql_psycopg2'

# ...


from settings import *

DATABASE_NAME = 'production'

3. use of Python logic


import socket

if socket.gethostname() == 'my-laptop':

# ...

IF the name of settings changed:
 You can fix this  by editing to change settingsto the name of your module

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quality.settings_XXX")

Apache + mod_python

Basic Configuration

make sure you have Apache installed with the mod_python module activated.
LoadModule python_module /usr/lib/apache2/modules/
Then, edit your Apache configuration file and add a <Location> directive that ties a specific URL path to a specific Django installation.
<Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug Off

Make sure to replace mysite.settings with the appropriate DJANGO_SETTINGS_MODULE for your site.

This tells Apache, “Use mod_python for any URL at or under ‘/’, using the Django mod_python handler.” It passes the value of DJANGO_SETTINGS_MODULE so mod_python knows which settings to use.

Note that we’re using the <Location> directive, not the <Directory> directive. The latter is used for pointing at places on your filesystem, whereas <Location> points at places in the URL structure of a Web site. <Directory> would be meaningless here.

Apache likely runs as a different user than your normal login and may have a different path and sys.path. You may need to tell mod_python how to find your project and Django itself.
PythonPath "['/path/to/project', '/path/to/django'] + sys.path"

Running Multiple Django Installations on the Same Apache Instance

to be continued

Running a Development Server with mod_python

Serving Django and Media Files from the Same Apache Instance


Using Django with FastCGI


Figure 12-1: a single server Django setup.
However, as traffic increases you’ll quickly run into resource contention between the different pieces of software.
Figure 12-2: Moving the database onto a dedicated server
As far as Django is concerned, the process of separating out the database server is extremely easy: you’ll simply need to change the DATABASE_HOST setting to the IP or DNS name of your database server. It’s probably a good idea to use the IP if at all possible, as relying on DNS for the connection between your Web server and database server isn’t recommended.
Figure 12-3: Separating out the media server.
For sites heavy in static content (photos, videos, etc.), moving to a separate media server is doubly important. Three-server setup: 10 million hits a day
Figure 12-4: A load-balanced, redundant server setup.
Figure 12-5. An example large-scale Django setup.

Performance Tuning

There’s No Such Thing As Too Much RAM

This shouldn’t be too hard; we’ve developed a site with more than half a million newspaper articles, and it took under 2GB of space.

Turn Off Keep-Alive

Use memcached

Of course, selecting memcached does you no good if you don’t actually use it. Chapter 15 is your best friend here: learn how to use Django’s cache framework, and use it everywhere possible. Aggressive, preemptive caching is usually the only thing that will keep a site up under major traffic.


No comments:

Post a Comment