How to authenticate against Django’s user database from Apache

47 views
Skip to first unread message

Marcel Müller

unread,
Sep 6, 2023, 4:23:04 PM9/6/23
to modwsgi
Hi,

when I follow the documentation I get an 500 Internal Server Error.
I want to protect images (uploads by the user) from the public and other users.

Everything works until I get to the part like in the subject described.

When I follow the documentation like "Finally, edit your WSGI script mysite.wsgi to tie Apache’s authentication to your site’s authentication mechanisms by importing the check_password function:" the provided code gives me the mentioned 500 error and the entire website is not available anymore. When I stick to the default code in wsgi.py it works and the <location> part works, where I get an apache authentication dialogue.
But I guess this is not how it is suppose to work. When I type in my credentials the 500 error shows again.

I am a python/django/apache newbie and I am stuck to this for days now.

Any help would be appreciated.

Best,
Marcel

Graham Dumpleton

unread,
Sep 6, 2023, 4:26:52 PM9/6/23
to mod...@googlegroups.com
What errors are logged to the Apache error log?

What does the Python code you have added look like?

What does the Apache configuration you have added look like?

Without more information it is hard to guess what the issue may be.

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/b26a327c-00ad-439e-b4c3-f1e171a7e03dn%40googlegroups.com.

Marcel Müller

unread,
Sep 7, 2023, 5:08:57 AM9/7/23
to modwsgi
Hi Graham,
you're right. Here are parts of the following files:

  1. /apache.conf
  2. /000-default.conf
  3. /wsgi.py #default 
  4. /wsgi.py #with code from documentation
  5. Error message #when I use code from 4.

-------------------------------------------------------------------------------------------------------------------
1. /apache2/apache.conf

#...
WSGIScriptAlias / /var/www/solutions/mysite/mysite/wsgi.py
WSGIPythonHome /var/www/solutions/venv
WSGIPythonPath /var/www/solutions/mysite

<Directory /var/www/solutions/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIApplicationGroup %{GLOBAL}
<Location "/media/uploads">
    AuthType Basic
    AuthName "Top Secret"
    Require valid-user
    AuthBasicProvider wsgi
    WSGIAuthUserScript /var/www/solutions/mysite/mysite/wsgi.py
</Location>
#...
-------------------------------------------------------------------------------------------------------------------
2. /apache2/000-default.conf

<VirtualHost *:80>
        ServerName localhost
        DocumentRoot /var/www/solutions/mysite
        ServerAdmin webmaster at localhost

        Alias /favicon.ico /var/www/solutions/mysite/static/tickets/img/ibe.ico
        Alias /media/ /var/www/mysite/media/
        Alias /static/ /var/www/mysite/static/

        <Directory /var/www/mysite/static/>
                Require all granted
        </Directory>

        <Directory /var/www/mysite/media>
                Require all granted
        </Directory>

        <Directory /var/www/solutions/mysite/mysite>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

-------------------------------------------------------------------------------------------------------------------
3. No apache error with default django /mysite/mysite/wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_wsgi_application()

-------------------------------------------------------------------------------------------------------------------
4. When I add the code from the documentation, like this

import os

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

from django.contrib.auth.handlers.modwsgi import check_password

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()

-------------------------------------------------------------------------------------------------------------------
5. I get the following apache error message, when I reload the page at 11:02am:

[Thu Sep 07 11:02:22.125710 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596] mod_wsgi (pid=1310): Failed to exec Python script file '/var/www/solutions/mysite/mysite/wsgi.py'.
[Thu Sep 07 11:02:22.125750 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596] mod_wsgi (pid=1310): Exception occurred processing WSGI script '/var/www/solutions/mysite/mysite/wsgi.py'.
[Thu Sep 07 11:02:22.126119 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596] Traceback (most recent call last):
[Thu Sep 07 11:02:22.126324 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]   File "/var/www/solutions/mysite/mysite/wsgi.py", line 14, in <module>
[Thu Sep 07 11:02:22.126330 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]     from django.contrib.auth.handlers.modwsgi import check_password
[Thu Sep 07 11:02:22.126339 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]   File "/var/www/solutions/venv/lib/python3.11/site-packages/django/contrib/auth/handlers/modwsgi.py", line 4, in <module>
[Thu Sep 07 11:02:22.126341 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]     UserModel = auth.get_user_model()
[Thu Sep 07 11:02:22.126343 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]                 ^^^^^^^^^^^^^^^^^^^^^
[Thu Sep 07 11:02:22.126357 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]   File "/var/www/solutions/venv/lib/python3.11/site-packages/django/contrib/auth/__init__.py", line 170, in get_user_model
[Thu Sep 07 11:02:22.126360 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]     return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
[Thu Sep 07 11:02:22.126361 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Thu Sep 07 11:02:22.126368 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]   File "/var/www/solutions/venv/lib/python3.11/site-packages/django/apps/registry.py", line 203, in get_model
[Thu Sep 07 11:02:22.126370 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]     self.check_apps_ready()
[Thu Sep 07 11:02:22.126375 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]   File "/var/www/solutions/venv/lib/python3.11/site-packages/django/apps/registry.py", line 138, in check_apps_ready
[Thu Sep 07 11:02:22.126377 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596]     raise AppRegistryNotReady("Apps aren't loaded yet.")
[Thu Sep 07 11:02:22.126390 2023] [wsgi:error] [pid 1310] [client 127.0.0.1:58596] django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

-------------------------------------------------------------------------------------------------------------------



Marcel Müller

unread,
Sep 7, 2023, 6:01:39 AM9/7/23
to modwsgi
And when I keep the default /wsgi.py but follow the documentation with <location> etc, I get the following apache error:
mod_wsgi (pid=6277): Target WSGI user authentication script '/var/www/solutions/mysite/mysite/wsgi.py' does not provide 'Basic' auth provider., referer: http://localhost/tickets/ticket/3/
Reply all
Reply to author
Forward
0 new messages