mod_wsgi: Problem to "deploy" using mod_wsgi on windows server 2019 local network.

I’ve been trying to put my django application in production for weeks and I got this situation:

Apache Lounge Version 2.4
Python 3.11
Error log:

[Mon Jul 03 13:33:05.482189 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012] mod_wsgi (pid=10664): Failed to exec Python script file 'C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/projeto_cad_usuarios/wsgi.py'.
[Mon Jul 03 13:33:05.482189 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012] mod_wsgi (pid=10664): Exception occurred processing WSGI script 'C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/projeto_cad_usuarios/wsgi.py'.
[Mon Jul 03 13:33:05.483187 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012] Traceback (most recent call last):\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/projeto_cad_usuarios/wsgi.py", line 16, in <module>\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     application = get_wsgi_application()\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]                   ^^^^^^^^^^^^^^^^^^^^^^\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:\\Apache24\\htdocs\\Cadastro\\env\\Lib\\site-packages\\django\\core\\wsgi.py", line 12, in get_wsgi_application\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     django.setup(set_prefix=False)\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:\\Apache24\\htdocs\\Cadastro\\env\\Lib\\site-packages\\django\\__init__.py", line 19, in setup\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]                       ^^^^^^^^^^^^^^^^^^^^^^^\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:\\Apache24\\htdocs\\Cadastro\\env\\Lib\\site-packages\\django\\conf\\__init__.py", line 57, in __getattr__\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     self._setup(name)\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:\\Apache24\\htdocs\\Cadastro\\env\\Lib\\site-packages\\django\\conf\\__init__.py", line 44, in _setup\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     self._wrapped = Settings(settings_module)\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]                     ^^^^^^^^^^^^^^^^^^^^^^^^^\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:\\Apache24\\htdocs\\Cadastro\\env\\Lib\\site-packages\\django\\conf\\__init__.py", line 107, in __init__\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     mod = importlib.import_module(self.SETTINGS_MODULE)\r
[Mon Jul 03 13:33:05.484185 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "C:\\Python311\\Lib\\importlib\\__init__.py", line 126, in import_module\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]     return _bootstrap._gcd_import(name[level:], package, level)\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 1206, in _gcd_import\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 1178, in _find_and_load\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 1128, in _find_and_load_unlocked\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 1206, in _gcd_import\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 1178, in _find_and_load\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012]   File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked\r
[Mon Jul 03 13:33:05.485181 2023] [wsgi:error] [pid 10664:tid 1248] [client ::1:53012] ModuleNotFoundError: No module named 'projeto_cad_usuarios'\r

mod_wsgi.py

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

httpd.conf

Alias /robots.txt "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/static/robots.txt"
Alias /favicon.ico "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/static/favicon.ico"

Alias /media/ "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/media/media"
Alias /static/ "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/static/"

<Directory "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/static">
Require all granted
</Directory>

<Directory "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/media/media">
Require all granted
</Directory>

LoadFile "C:/Python311/python311.dll"
LoadModule wsgi_module "C:/Apache24/htdocs/Cadastro/env/Lib/site-packages/mod_wsgi-4.9.5.dev1-py3.11-win-amd64.egg/mod_wsgi/server/mod_wsgi.cp311-win_amd64.pyd"
WSGIPythonHome "C:/Apache24/htdocs/Cadastro/env"
WSGIScriptAlias / "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/projeto_cad_usuarios/wsgi.py"
# WSGIPythonPath / "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/projeto_cad_usuarios/"

<Directory "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios/projeto_cad_usuarios">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Show this message on the browser:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

I don’t know where else to go, I’m using ApacheLounge after reading your recommendation in another post.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

Those last of logs with the exception traceback do not show any output that I can see corresponding to the lines:

import sys
print(sys.path)

which I told you to add. Were they added?

To make it more obvious, change it to:

import sys
print("PATH:", sys.path)

Ensure these two lines are the very first lines in the wsgi.py file.

Using your web browser go to the same URL you have been using to try and access the application. The output of any print() statements in the wsgi.py should be displayed in the Apache error log if things are set correctly in the Apache config.

So did you try and access your web site or not?

Also ensure that LogLevel is set to info and not err or warn so can see when mod_wsgi loads the wsgi.py. That will confirm you actually made a request and it was received properly.

You need to make a request to the WSGI application else you will not see the message as the wsgi.py file will only be loaded when first request arrives. You may need to restart the Apache service as well if how things are setup means the wsgi.py has been cached in memory.

At the very start of the wsgi.py file, before any imports, add:

import sys
print(sys.path)

What is the value for sys.path that is printed out into the logs?

My mistake, I just copied what you had, which was in part wrong to start with ignoring the path issue.

Use:

WSGIPythonPath "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios"

It should only be given one argument.

At a guess you should have:

WSGIPythonPath / "C:/Apache24/htdocs/Cadastro/projeto_cad_usuarios"

You had it pointing at the wrong directory.

It should point to the parent directory of where your project is, not the project directory itself.