==============================================================How to authenticate against Django's user database from Apache==============================================================Since keeping multiple authentication databases in sync is a common problem whendealing with Apache, you can configure Apache to authenticate against Django's:doc:`authentication system </topics/auth/index>` directly. This requires Apacheversion >= 2.2 and mod_wsgi >= 2.0. For example, you could:* Serve static/media files directly from Apache only to authenticated users.* Authenticate access to a Subversion_ repository against Django users witha certain permission.* Allow certain users to connect to a WebDAV share created with mod_dav_... note::If you have installed a :ref:`custom user model <auth-custom-user>` andwant to use this default auth handler, it must support an ``is_active``attribute. If you want to use group based authorization, your custom usermust have a relation named 'groups', referring to a related object that hasa 'name' field. You can also specify your own custom mod_wsgiauth handler if your custom cannot conform to these requirements... _Subversion: https://subversion.apache.org/.. _mod_dav: https://httpd.apache.org/docs/2.2/mod/mod_dav.htmlAuthentication with ``mod_wsgi``================================.. note::The use of ``WSGIApplicationGroup %{GLOBAL}`` in the configurations belowpresumes that your Apache instance is running only one Django application.If you are running more than one Django application, please refer to the`Defining Application Groups`_ section of the mod_wsgi docs for moreinformation about this setting.Make sure that mod_wsgi is installed and activated and that you havefollowed the steps to set up :doc:`Apache with mod_wsgi</howto/deployment/wsgi/modwsgi>`.Next, edit your Apache configuration to add a location that you wantonly authenticated users to be able to view:.. code-block:: apacheWSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.pyWSGIPythonPath /path/to/mysite.comWSGIProcessGroup %{GLOBAL}WSGIApplicationGroup %{GLOBAL}<Location "/secret">AuthType BasicAuthName "Top Secret"Require valid-userAuthBasicProvider wsgiWSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.py</Location>The ``WSGIAuthUserScript`` directive tells mod_wsgi to execute the``check_password`` function in specified wsgi script, passing the user name andpassword that it receives from the prompt. In this example, the``WSGIAuthUserScript`` is the same as the ``WSGIScriptAlias`` that defines yourapplication :doc:`that is created by django-admin startproject</howto/deployment/wsgi/index>`... admonition:: Using Apache 2.2 with authenticationMake sure that ``mod_auth_basic`` and ``mod_authz_user`` are loaded.These might be compiled statically into Apache, or you might need to useLoadModule to load them dynamically in your ``httpd.conf``:.. code-block:: apacheLoadModule auth_basic_module modules/mod_auth_basic.soLoadModule authz_user_module modules/mod_authz_user.soFinally, edit your WSGI script ``mysite.wsgi`` to tie Apache's authenticationto your site's authentication mechanisms by importing the ``check_password``function::import osos.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'from django.contrib.auth.handlers.modwsgi import check_passwordfrom django.core.handlers.wsgi import WSGIHandlerapplication = WSGIHandler()Requests beginning with ``/secret/`` will now require a user to authenticate.The mod_wsgi `access control mechanisms documentation`_ provides additionaldetails and information about alternative methods of authentication... _Defining Application Groups: https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups.. _access control mechanisms documentation: https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.htmlAuthorization with ``mod_wsgi`` and Django groups-------------------------------------------------mod_wsgi also provides functionality to restrict a particular location tomembers of a group.In this case, the Apache configuration should look like this:.. code-block:: apacheWSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.pyWSGIProcessGroup %{GLOBAL}WSGIApplicationGroup %{GLOBAL}<Location "/secret">AuthType BasicAuthName "Top Secret"AuthBasicProvider wsgiWSGIAuthUserScript /path/to/mysite.com/mysite/wsgi.pyWSGIAuthGroupScript /path/to/mysite.com/mysite/wsgi.pyRequire group secret-agentsRequire valid-user</Location>To support the ``WSGIAuthGroupScript`` directive, the same WSGI script``mysite.wsgi`` must also import the ``groups_for_user`` function whichreturns a list groups the given user belongs to... code-block:: pythonfrom django.contrib.auth.handlers.modwsgi import check_password, groups_for_userRequests for ``/secret/`` will now also require user to be a member of the"secret-agents" group.