Previous Page Next Page

Other Deployment Configurations

The preceding section discussed how to deploy a Django project to a basic Apache setup. You may need to configure your Django deployment a bit differently to meet certain needs. The following sections discuss multiple Django installations, media files, and Python eggs.

Implementing Multiple Django Installations in Apache

You can run multiple Django installations on the same Apache instance using <VirtualHost> directives in the Apache httpd.conf file. The following example shows how to use two different <VirtualHost> directives to implement multiple Django installations:

NameVirtualHost *
<VirtualHost *>
    ServerName www.myCoolSite.com
    # ...
    SetEnv DJANGO_SETTINGS_MODULE myCoolSite.settings
</VirtualHost>

<VirtualHost *>
    ServerName www2. myCoolSite.com
    # ...
    SetEnv DJANGO_SETTINGS_MODULE myCoolSite.other_settings
</VirtualHost>

By the Way

Some installations of apache will split the configuration entries into multiple files, so virtual host definitions may be in a different configuration file. For example, .../apache/conf/extra/httpd-vhosts.conf.


You can also implement multiple Django installations in a single <VirtualHost> directive as long as you include different <Location> directives with PythonInterpreter directives. The following example shows how to implement multiple Django installations in a single <VirtualHost> directive:

<VirtualHost *>
    ServerName www. myCoolSite.com
    # ...
    <Location "/myCoolSite/">
        SetEnv DJANGO_SETTINGS_MODULE myCoolSite.settings
        PythonInterpreter myCoolSite
    </Location>

    <Location "/myOtherCoolSite/">
        SetEnv DJANGO_SETTINGS_MODULE myCoolSite.other_settings
        PythonInterpreter myOtherCoolSite
    </Location>
</VirtualHost>

Serving Media Files on the Same Server

It is much better to serve media files from a different web server. However, if you absolutely need to serve them from the Django Apache server, turn off mod_python for the media files.

You can turn off mod_python for the media files by specifying a <Location> directive with a None handler. For example, the following directive turns off mod_python for files located in the myMedia subdirectory of the Apache root:

<Location "/myMedia/">
    SetHandler = None
</Location>

Configuring Eggs with mod_python

If you installed Django from a Python egg, or if you are using eggs in your project, you need to create an extra Python file in your project that sets up a directory for the Apache web server process to write to. For example:

import os
os.environ['PYTHON_EGG_CACHE'] = '/my/python/egg/directory'

You also need to tell mod_python to import this file using the PythonImport directive. Be certain that you specify a PythonInterpreter directive as well. The following shows an example of importing the Python file:

<Location "/myCoolSite/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug OnPythonInterpreter my_django
    PythonImport /path/to/my/project/file.py my_django
</Location>

Previous Page Next Page