1

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:

enter image description here

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"
5
  • did you add the user to the run as services permission?
    – djdomi
    Commented Jan 10 at 17:58
  • Yes, as noted in the OP.
    – JDM
    Commented Jan 11 at 18:24
  • after using gpt, it said that the module encodings seems to be missing ModuleNotFoundError: No module named 'encodings'
    – djdomi
    Commented Jan 14 at 9:40
  • Seems like the actual problem is running python and django as a non-admin user, not running apache. You should try running your django app in python cli, while logged in as another user. If the "missing" module is really the problem, then it might just be a problem inside your system path? See if you get the same errors, or any additional information running django manually.
    – GChuf
    Commented Jan 14 at 10:35
  • @GChuf -- excellent suggestion about running the CLI from the non-admin account, I didn't think to try that as a diagnostic. However on trying it now, the app starts normally. I signed on as a non-admin user, opened Powershell, activated the virtual enviroment then entered python manage.py runserver. The app started normally, accepts requests and responds.
    – JDM
    Commented Jan 17 at 15:07

2 Answers 2

1
+100
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
[...]
ModuleNotFoundError: No module named 'encodings'

is the error message. encodings is a system module, so it should be available in the default installation path.

My guess is that Adminsitrator user has the environment variables PYTHONPATH set, but the other users doesn't, thus the error message.

You can set the environment variables for a service.

2
  • 1
    I wasn't able to find anyplace where these environment variables were set for Administrator, or for the system -- so initially I wasn't sure this was actually the problem. However, based on your answer and the linked article, I went ahead and added PYTHONPATH and PYTHONHOME, pointing both of them to the server's Python 3.10 installation directory. (Not the virtual environment, but the "real" Python.) And.... success! With those entries added to the System environment variables, the service now starts under my non-admin account, and also LocalSystem. So, thanks and bounty awarded.
    – JDM
    Commented Jan 17 at 16:54
  • Glad to be of assistance :)
    – vidarlo
    Commented Jan 17 at 17:27
0

Try to configure the service to start with the specific user you want to use, but previously you need to grant the "Logon as a service" permission to that user

Policy configuration to grant the "logon as service" permission

1
  • As noted in the OP and also repeated in the comments: I already confirmed this and it is not the issue.
    – JDM
    Commented Jan 17 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .