Previous Page Next Page

Hour 24. Deploying Django

What You'll Learn in This Hour

  • How to deploy Django on an Apache web server with mod_python

  • How to use other deployment configurations

  • How to optimize a Django website

In this book, all the development testing has been done on Django's development server. The development server works well for testing your code as you are developing the website. However, you need to deploy your project to a full web server to put it in production.

This hour discusses how to deploy Django projects to a basic Apache web server with mod_python. The object of this hour is to provide you with enough information so that you can get some of the basic configurations deployed and running. Therefore, we will focus on Django-specific tasks, not on Apache, mod_python, or other components. Much documentation and many resources are available online to guide you in implementing web servers using Apache, MySQL, PostgreSQL, and mod_python on both Linux and Windows servers.

Specifically, we will discuss the steps to deploy a Django project on a basic Apache installation, a few custom configuration options, and some optimizations that you should consider when designing your website implementation.

Deploying Django to Apache with mod_python

This section discusses the components and steps required to deploy a Django project to an Apache server. Deploying your Django project to an Apache server is not too difficult as long as all the pieces are in place. The following sections discuss the components and steps necessary to deploy your projects.

Python, Django, a Database, Apache, and mod_python

The basic components that Django requires are Python 2.3 or later, Django, a supported SQL database, Apache 2.x, and mod_python 3.x. I won't recommend specific versions in this book. You should be able to determine that based on your needs.

By the Way

If you are using the same machine that you developed the project on, you should already have Python, Django, and the database installed.


You should refer to the documentation at the project sites for these components (see Appendix A, "Django Resources," for a list of reference sites) to get installation and configuration instructions.

After you have installed Apache and mod_python, make certain that the mod_python module is activated. The mod_python module is active if you find the following entry (not commented) in the Apache http.conf file:

LoadModule python_module /usr/lib/apache2/modules/mod_python.so

Watch Out!

The path to the mod_python.so file may be different depending on where Apache is installed on your server.


By the Way

If you are deploying the project on a different machine than it was developed on, the project needs to be copied to that machine.


Placing the Project in PYTHONPATH

You need to make certain that your project is in the PYTHONPATH. You can accomplish this by either placing the project in a directory that is in the PYTHONPATH or by adding your project directory to the PYTHONPATH environment setting.

Configuring Your Django Project

After you have the Python, Django, SQL database, Apache, and mod_python components installed and configured on your system, you may need to configure your project as well.

If you configured a new or different database on the production server, you need to modify the database settings in the settings.py file (as described in Hour 2, "Creating Your First Website").

You may also want to do the following:

Adding Admin Media Files to the Server

The Django development server automatically serves the admin media files. That is not the case with Apache. You need to either copy the django/contrib/admin/media directory to the Apache document root or at least create a symbolic link there. Using a symbolic link may be the best solution, because the files will need to be updated in only one place.

Setting up the httpd.conf File

After the components are all installed and configured and the project is configured, you add a <Location> directive to the httpd.conf file to begin serving your site from Apache.

The following snippet shows a sample <Location> directive for a project called myCoolSite:

<Location "/myCoolSite/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE myCoolSite.settings
    PythonDebug On
</Location>

Watch Out!

Django's URLconf files will not trim the path specified in the <Location> directive. For example, all URLs to the project defined by the sample code just shown will begin with /myCoolSite/. If possible, use the root path in the directive. For example:

<Location "/">


Did you Know?

You can add a PythonPath entry in the <Location> directive to add the project directory to the PYTHONPATH if it is not already there. For example:

<Location "/myCoolSite/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug On
    PythonPath "['/path/to/myCoolSite'] + sys.path"
</Location>


Try It Yourself: Deploy a Django Project

In this section, you deploy the iFriends project to an Apache web server that has the mod_python module active. Follow these steps:

1.
Stop the development server.

2.
Install Apache 2.x.

3.
Install mod_python 3.x.

4.
Verify that the mod_python module is installed by making certain that the mod_python.so file exists in the Apache modules directory.

5.
Open the Apache httpd.conf file in an editor.

6.
Verify that the mod_python module is active by making certain that the following line exists and is not commented out in the Apache httpd.conf file:

LoadModule python_module modules/mod_python.so

7.
Add the following <Location> directive to the file to enable the Django project:

<Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE iFriends.settings
    PythonDebug On
</Location>

8.
Save the httpd.conf file.

9.
Add the full path to the iFriends project to the PYTHONPATH environment setting. (Or you can copy the iFriends project to a directory that is in the PYTHONPATH.)

10.
Either copy the django/contrib/admin/media directory to the Apache document root, or create a symbolic link there.

11.
Stop and restart the Apache web server.

12.
Access the root URL of the Apache web server to verify that the home page, shown in Figure 24.1, is displayed.

Figure 24.1. The home_view() web page that is rendered for the iFriends site.



Previous Page Next Page