What You'll Learn in This Hour |
|
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.
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.
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.
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.
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:
Modify values in the settings.py file for productions. For example, you will want to set the DEBUG and TEMPLATE_DEBUG settings to False.
Add or remove middleware.
Configure languages.
Disable some of the views in the URLconf files by commenting out their URL patterns.
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.
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 ProjectIn this section, you deploy the iFriends project to an Apache web server that has the mod_python module active. Follow these steps:
|