2
0

apache-auth.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. =========================================================
  2. Authenticating against Django's user database from Apache
  3. =========================================================
  4. .. warning::
  5. Support for mod_python has been deprecated within Django. At that
  6. time, this method of authentication will no longer be provided by
  7. Django. The community is welcome to offer its own alternate
  8. solutions using WSGI middleware or other approaches.
  9. Since keeping multiple authentication databases in sync is a common problem when
  10. dealing with Apache, you can configuring Apache to authenticate against Django's
  11. :doc:`authentication system </topics/auth>` directly. For example, you
  12. could:
  13. * Serve static/media files directly from Apache only to authenticated users.
  14. * Authenticate access to a Subversion_ repository against Django users with
  15. a certain permission.
  16. * Allow certain users to connect to a WebDAV share created with mod_dav_.
  17. .. _Subversion: http://subversion.tigris.org/
  18. .. _mod_dav: http://httpd.apache.org/docs/2.0/mod/mod_dav.html
  19. Configuring Apache
  20. ==================
  21. To check against Django's authorization database from a Apache configuration
  22. file, you'll need to use mod_python's ``PythonAuthenHandler`` directive along
  23. with the standard ``Auth*`` and ``Require`` directives:
  24. .. code-block:: apache
  25. <Location /example/>
  26. AuthType Basic
  27. AuthName "example.com"
  28. Require valid-user
  29. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  30. PythonAuthenHandler django.contrib.auth.handlers.modpython
  31. </Location>
  32. .. admonition:: Using the authentication handler with Apache 2.2
  33. If you're using Apache 2.2, you'll need to take a couple extra steps.
  34. You'll need to ensure that ``mod_auth_basic`` and ``mod_authz_user``
  35. are loaded. These might be compiled statically into Apache, or you might
  36. need to use ``LoadModule`` to load them dynamically (as shown in the
  37. example at the bottom of this note).
  38. You'll also need to insert configuration directives that prevent Apache
  39. from trying to use other authentication modules, as well as specifying
  40. the ``AuthUserFile`` directive and pointing it to ``/dev/null``. Depending
  41. on which other authentication modules you have loaded, you might need one
  42. or more of the following directives:
  43. .. code-block:: apache
  44. AuthBasicAuthoritative Off
  45. AuthDefaultAuthoritative Off
  46. AuthzLDAPAuthoritative Off
  47. AuthzDBMAuthoritative Off
  48. AuthzDefaultAuthoritative Off
  49. AuthzGroupFileAuthoritative Off
  50. AuthzOwnerAuthoritative Off
  51. AuthzUserAuthoritative Off
  52. A complete configuration, with differences between Apache 2.0 and
  53. Apache 2.2 marked in bold, would look something like:
  54. .. parsed-literal::
  55. **LoadModule auth_basic_module modules/mod_auth_basic.so**
  56. **LoadModule authz_user_module modules/mod_authz_user.so**
  57. ...
  58. <Location /example/>
  59. AuthType Basic
  60. AuthName "example.com"
  61. **AuthUserFile /dev/null**
  62. **AuthBasicAuthoritative Off**
  63. Require valid-user
  64. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  65. PythonAuthenHandler django.contrib.auth.handlers.modpython
  66. </Location>
  67. By default, the authentication handler will limit access to the ``/example/``
  68. location to users marked as staff members. You can use a set of
  69. ``PythonOption`` directives to modify this behavior:
  70. ================================ =========================================
  71. ``PythonOption`` Explanation
  72. ================================ =========================================
  73. ``DjangoRequireStaffStatus`` If set to ``on`` only "staff" users (i.e.
  74. those with the ``is_staff`` flag set)
  75. will be allowed.
  76. Defaults to ``on``.
  77. ``DjangoRequireSuperuserStatus`` If set to ``on`` only superusers (i.e.
  78. those with the ``is_superuser`` flag set)
  79. will be allowed.
  80. Defaults to ``off``.
  81. ``DjangoPermissionName`` The name of a permission to require for
  82. access. See :ref:`custom permissions
  83. <custom-permissions>` for more
  84. information.
  85. By default no specific permission will be
  86. required.
  87. ================================ =========================================
  88. Note that sometimes ``SetEnv`` doesn't play well in this mod_python
  89. configuration, for reasons unknown. If you're having problems getting
  90. mod_python to recognize your ``DJANGO_SETTINGS_MODULE``, you can set it using
  91. ``PythonOption`` instead of ``SetEnv``. Therefore, these two Apache directives
  92. are equivalent::
  93. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  94. PythonOption DJANGO_SETTINGS_MODULE mysite.settings