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


1. settings.py
DEBUG = False
TEMPLATE_DEBUG = False

2. root template directory
404.html

{% 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 %}

500.html
 (not rely on other templates)
unhandled Python exception

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
    <title>Page unavailable</title>
</head>
<body>
    <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>
</body>
</html>3. Setting Up Error Alters and Setting Up Broken Link Alerts
Need Email Set up

ADMINS = (
    ('John Lennon', 'jlennon@example.com'),
    ('Paul McCartney', 'pmacca@example.com'),
)

http://www.djangobook.com/en/2.0/chapter12.html


Using Multiple settings.py for Development and Production


Three ways:

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

DEBUG = True
TEMPLATE_DEBUG = DEBUG

DATABASE_ENGINE = 'postgresql_psycopg2'
DATABASE_NAME = 'devdb'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_PORT = ''

# ...

# settings_production.py

from settings import *

DEBUG = TEMPLATE_DEBUG = False
DATABASE_NAME = 'production'
DATABASE_USER = 'app'
DATABASE_PASSWORD = 'letmein'

override
3. use of Python logic

# settings.py

import socket

if socket.gethostname() == 'my-laptop':
    DEBUG = TEMPLATE_DEBUG = True
else:
    DEBUG = TEMPLATE_DEBUG = False

# ...

IF the name of settings changed:
 You can fix this  by editing manage.py 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/mod_python.so
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
</Location>


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

Alternative: 

Using Django with FastCGI

http://www.djangobook.com/en/2.0/chapter12.html



Scaling

_images/scaling-1.png
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.
_images/scaling-2.png
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.
_images/scaling-3.png
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
_images/scaling-4.png
Figure 12-4: A load-balanced, redundant server setup.
_images/scaling-5.png
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.
done


done

No comments:

Post a Comment