<commit_before>==========================================
How to use Django with Apache and mod_wsgi
==========================================

Deploying Django with Apache_ and `mod_wsgi`_ is a tried and tested way to get
Django into production.

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

mod_wsgi is an Apache module which can host any Python WSGI_ application,
including Django. Django will work with any version of Apache which supports
mod_wsgi.

.. _WSGI: http://www.wsgi.org

The `official mod_wsgi documentation`_ is fantastic; it's your source for all
the details about how to use mod_wsgi. You'll probably want to start with the
`installation and configuration documentation`_.

.. _official mod_wsgi documentation: http://code.google.com/p/modwsgi/
.. _installation and configuration documentation: http://code.google.com/p/modwsgi/wiki/InstallationInstructions

Basic configuration
===================

Once you've got mod_wsgi installed and activated, edit your Apache server's
``httpd.conf`` file and add the following. If you are using a version of Apache
older than 2.4, replace ``Require all granted`` with ``Allow from all``.

.. code-block:: apache

    WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
    WSGIPythonPath /path/to/mysite.com

    <Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
    Order deny,allow
    Require all granted
    </Files>
    </Directory>

The first bit in the ``WSGIScriptAlias`` line is the base URL path you want to
serve your application at (``/`` indicates the root url), and the second is the
location of a "WSGI file" -- see below -- on your system, usually inside of
your project package (``mysite`` in this example). This tells Apache to serve
any request below the given URL using the WSGI application defined in that
file.

The ``WSGIPythonPath`` line ensures that your project package is available for
import on the Python path; in other words, that ``import mysite`` works.

The ``<Directory>`` piece just ensures that Apache can access your
:file:`wsgi.py` file.

Next we'll need to ensure this :file:`wsgi.py` with a WSGI application object
exists. As of Django version 1.4, :djadmin:`startproject` will have created one
for you; otherwise, you'll need to create it. See the :doc:`WSGI overview
documentation</howto/deployment/wsgi/index>` for the default contents you
should put in this file, and what else you can add to it.

.. warning::

   If multiple Django sites are run in a single mod_wsgi process, all of them
   will use the settings of whichever one happens to run first. This can be
   solved with a minor edit to ``wsgi.py`` (see comment in the file for
   details), or by :ref:`using mod_wsgi daemon mode<daemon-mode>` and ensuring
   that each site runs in its own daemon process.


Using a virtualenv
==================

If you install your project's Python dependencies inside a `virtualenv`_,
you'll need to add the path to this virtualenv's ``site-packages`` directory to
your Python path as well. To do this, add an additional path to your
`WSGIPythonPath` directive, with multiple paths separated by a colon::

    WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python2.X/site-packages

Make sure you give the correct path to your virtualenv, and replace
``python2.X`` with the correct Python version (e.g. ``python2.7``).

.. _virtualenv: http://www.virtualenv.org

.. _daemon-mode:

Using mod_wsgi daemon mode
==========================

"Daemon mode" is the recommended mode for running mod_wsgi (on non-Windows
platforms). To create the required daemon process group and delegate the
Django instance to run in it, you will need to add appropriate
``WSGIDaemonProcess`` and ``WSGIProcessGroup`` directives. A further change
required to the above configuration if you use daemon mode is that you can't
use ``WSGIPythonPath``; instead you should use the ``python-path`` option to
``WSGIDaemonProcess``, for example::<commit_msg>[1.6.x] Fixed #22116 -- Added precision about WSGIPythonPath separator<commit_after>==========================================
How to use Django with Apache and mod_wsgi
==========================================

Deploying Django with Apache_ and `mod_wsgi`_ is a tried and tested way to get
Django into production.

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

mod_wsgi is an Apache module which can host any Python WSGI_ application,
including Django. Django will work with any version of Apache which supports
mod_wsgi.

.. _WSGI: http://www.wsgi.org

The `official mod_wsgi documentation`_ is fantastic; it's your source for all
the details about how to use mod_wsgi. You'll probably want to start with the
`installation and configuration documentation`_.

.. _official mod_wsgi documentation: http://code.google.com/p/modwsgi/
.. _installation and configuration documentation: http://code.google.com/p/modwsgi/wiki/InstallationInstructions

Basic configuration
===================

Once you've got mod_wsgi installed and activated, edit your Apache server's
``httpd.conf`` file and add the following. If you are using a version of Apache
older than 2.4, replace ``Require all granted`` with ``Allow from all``.

.. code-block:: apache

    WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
    WSGIPythonPath /path/to/mysite.com

    <Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
    Order deny,allow
    Require all granted
    </Files>
    </Directory>

The first bit in the ``WSGIScriptAlias`` line is the base URL path you want to
serve your application at (``/`` indicates the root url), and the second is the
location of a "WSGI file" -- see below -- on your system, usually inside of
your project package (``mysite`` in this example). This tells Apache to serve
any request below the given URL using the WSGI application defined in that
file.

The ``WSGIPythonPath`` line ensures that your project package is available for
import on the Python path; in other words, that ``import mysite`` works.

The ``<Directory>`` piece just ensures that Apache can access your
:file:`wsgi.py` file.

Next we'll need to ensure this :file:`wsgi.py` with a WSGI application object
exists. As of Django version 1.4, :djadmin:`startproject` will have created one
for you; otherwise, you'll need to create it. See the :doc:`WSGI overview
documentation</howto/deployment/wsgi/index>` for the default contents you
should put in this file, and what else you can add to it.

.. warning::

   If multiple Django sites are run in a single mod_wsgi process, all of them
   will use the settings of whichever one happens to run first. This can be
   solved with a minor edit to ``wsgi.py`` (see comment in the file for
   details), or by :ref:`using mod_wsgi daemon mode<daemon-mode>` and ensuring
   that each site runs in its own daemon process.


Using a virtualenv
==================

If you install your project's Python dependencies inside a `virtualenv`_,
you'll need to add the path to this virtualenv's ``site-packages`` directory to
your Python path as well. To do this, add an additional path to your
``WSGIPythonPath`` directive, with multiple paths separated by a colon (``:``)
if using a UNIX-like system, or a semicolon (``;``) if using Windows. If any
part of a directory path contains a space character, the complete argument
string to ``WSGIPythonPath`` must be quoted::

    WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python2.X/site-packages

Make sure you give the correct path to your virtualenv, and replace
``python2.X`` with the correct Python version (e.g. ``python2.7``).

.. _virtualenv: http://www.virtualenv.org

.. _daemon-mode:

Using mod_wsgi daemon mode
==========================

"Daemon mode" is the recommended mode for running mod_wsgi (on non-Windows
platforms). To create the required daemon process group and delegate the
Django instance to run in it, you will need to add appropriate
``WSGIDaemonProcess`` and ``WSGIProcessGroup`` directives. A further change
required to the above configuration if you use daemon mode is that you can't
use ``WSGIPythonPath``; instead you should use the ``python-path`` option to
``WSGIDaemonProcess``, for example::
{'fraction_non_alphanumeric': 0.11666869005705961, 'fraction_numerical': 0.0027922787422605317, 'mean_word_length': 4.426877470355731, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5548788', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2281, 'n_words': 1059}