.. _howto-deployment-modwsgi:

==========================================
How to use Django with Apache and mod_wsgi
==========================================

Apache_ with `mod_wsgi`_ is one of the reccomended ways for using Django in 
production.

.. _Apache: http://httpd.apache.org/
.. _mod_wsgi: http://code.google.com/p/modwsgi/

mod_wsgi is an Apache module which can be used to host any Python application 
which supports the Python WSGI interface (`PEP 333`_), including Django.  Django 
will work with any version of Apache which supports mod_wsgi.

.. _PEP 333: http://www.python.org/dev/peps/pep-0333/

Basic Configuration
===================

First make sure you have Apache installed, and mod_wsgi installed and activated.  
Next edit your ``httpd.conf`` file and add::

    WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi

Where the first path is the url you want to be serving your application at (/ 
indicates the root url), and the second is the location of a WSGI file (to be 
explained momentarily) on your system, usually inside of your project.  This 
will tell Apache to serve any request at that URL using WSGI, configured with 
that file.  Next we create our WSGI file, at the location we just indicated and 
put in it::

    import os
    import sys

    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()

If your project is not on your PYTHONPATH by default you can add::

    sys.path.append('/usr/local/django')

to place it on the path.  Also replace 'mysite.settings' with your settings file.

See the :ref:`Apache/mod_python documentation<howto-deployment-modpython>` for 
directions on serving static media, and the `mod_wsgi documentation`_ for an 
explanation of other directives and configuration options you can use.

.. _mod_wsgi documentation: http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango