I've found many similar articles with similar issues, and a couple of them concluded the Apache service just cannot be run at all for my situation. However I did get the service to run -- only it requires an undesirable setup in terms of security.
The situation is: I've got a Django 4.2.9 project hosted in Apache 2.4 using mod_wsgi 5.0.0, all on a Windows 2016 Server.
When I run Apache from the command line of the server's Administrator
account, it works perfectly.
When I install the Apache 2.4 service using httpd.exe -k install
and then manually reconfigure the service to log on as .\Administrator
, it also works perfectly.
However if I run the service logging on as LocalSystem
, or logging on as any other user account, the service fails to start. (Side note: I've confirmed that all accounts have the Log on as a service privilege enabled, so that's not the issue.) The error is as shown below:
And also -- if I try to run httpd.exe
from the command line of a different user account, the executable fails out. The generated error in the log file is:
[Fri Jan 05 14:28:08.201284 2024] [wsgi:info] [pid 5712:tid 372] mod_wsgi (pid=5712): Initializing Python.
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'D:\\Program Files\\Apache24\\bin\\httpd.exe'
sys.base_prefix = 'C:\\Program Files\\Python310'
sys.base_exec_prefix = 'C:\\Program Files\\Python310'
sys.platlibdir = 'lib'
sys.executable = 'D:\\Program Files\\Apache24\\bin\\httpd.exe'
sys.prefix = 'C:\\Program Files\\Python310'
sys.exec_prefix = 'C:\\Program Files\\Python310'
sys.path = [
'C:\\Program Files\\Python310\\python310.zip',
'.\\DLLs',
'.\\lib',
'D:\\Program Files\\Apache24\\bin',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000011d8 (most recent call first): <no Python frame>
[Fri Jan 05 14:28:08.217284 2024] [mpm_winnt:crit] [pid 4432:tid 368] AH00419: master_main: create child process failed. Exiting.
So the common factor here seems to be that it works with the Administrator
account but not with any other. I just can't work out why... or how to correct it.
Further relevant info: Here's the wsgi configuration information I obtained from the mod_wsgi-express module-config
command, and pasted into my httpd.conf
file:
LoadFile "C:/Program Files/Python310/python310.dll"
LoadModule wsgi_module "D:/CSCatalog/django-projects/code/cscr/.venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "D:/CSCatalog/django-projects/code/cscr/.venv"
ModuleNotFoundError: No module named 'encodings'
python manage.py runserver
. The app started normally, accepts requests and responds.