j_kaden
j_kaden

Reputation: 35

Trying to start Django on Apache: Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding

the goal was to run my django project, which is created in a virtual environment from "virtualenv", on apache. I followed this tutorial, which worked well untill I wanted to start the apache as a windows service which resulted in:

[Tue Mar 14 14:43:28.277072 2023] [mpm_winnt:notice] [pid 19800:tid 412] AH00455: Apache/2.4.56 (Win64) mod_wsgi/4.9.4 Python/3.10 configured -- resuming normal operations
[Tue Mar 14 14:43:28.278069 2023] [mpm_winnt:notice] [pid 19800:tid 412] AH00456: Apache Lounge VS17 Server built: Mar  6 2023 10:53:37
[Tue Mar 14 14:43:28.278069 2023] [core:notice] [pid 19800:tid 412] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Tue Mar 14 14:43:28.283041 2023] [mpm_winnt:notice] [pid 19800:tid 412] AH00418: Parent: Created child process 8896
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 = 'C:\\Apache24\\bin\\httpd.exe'
  sys.base_prefix = 'C:\\miniconda3'
  sys.base_exec_prefix = 'C:\\miniconda3'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\Apache24\\bin\\httpd.exe'
  sys.prefix = 'C:\\miniconda3'
  sys.exec_prefix = 'C:\\miniconda3'
  sys.path = [
    'C:\\miniconda3\\python310.zip',
    '.\\DLLs',
    '.\\lib',
    'C:\\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 0x000050e4 (most recent call first):
  <no Python frame>
[Tue Mar 14 14:43:28.946306 2023] [mpm_winnt:crit] [pid 19800:tid 412] AH00419: master_main: create child process failed. Exiting.

my httpd.conf file looks like this:

<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

LoadFile "C:/miniconda3/python310.dll"
LoadModule wsgi_module "C:/Django/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Django/venv/Scripts/python.exe"
WSGIPythonPath "C:/Django/Lib/site-packages" 

<VirtualHost *:80>
ServerAlias www.server.com
ServerName server.com
ServerAdmin [email protected]
SetEnv PYTHONUTF8 1
WSGIScriptAlias / "C:/Django/project/project/wsgi.py"
  <Directory "C:/Django/project/project">
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

Alias /static/ "C:/Django/project/static/"
  <Directory "C:/Django/project/static">
    Require all granted
  </Directory>

ErrorLog "C:/Apache24/logs/apache.error.log"
CustomLog "C:/Apache24/logs/apache.custom.log" common
</VirtualHost>

the LoadFile I also tried to copy the python310.dll to the .venv/Scripts path (as described in the Tutorial) however this throws an error that the .dll can´t be found. Further I tried to set this part :

LoadFile "C:/miniconda3/python310.dll"
LoadModule wsgi_module "C:/Django/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Django/venv/Scripts/python.exe"
WSGIPythonPath "C:/Django/Lib/site-packages" 

as miniconda paths in case this may causes issues.

In the end i also tried to just make Django itself a Windows Service however the environment says it has pypiwin32 and pywin32 installed, however isn´t able to import

import win32serviceutil
import win32service
import win32event
import servicemanager

Idealy I could just fix the first problem with apache. However several articles didn´t help as they often just recommend not setting PYTHONHOME or PYTHONPATH (which in may case are both not set) or the problems are Linux based.

Further I suspect that miniconda3 in the system prefixes may cause problems, however miniconda is needed for a different project.

Has Someone an Idea what I´m doing wrong ? (further code can be provided, however this is already a long question)

Upvotes: 1

Views: 205

Answers (1)

j_kaden
j_kaden

Reputation: 35

If anynone comes across this with a similar Problem, following worked now:

I downloaded a new Python and installed to all users and added it (manually) to the Systempathvariables. For the LoadFile python310.dll the path is now from this new Python and also the WSGIPythonHome goes to the Python.exe at the new Python.

Furthermore I would like to add that this Tutorial which i followed, does recommends installing a new Python anyway, however the LoadFile is recommended to go to the Project-root/venv/Scripts path which Apache couldn´t find.

Furthermore when creating the venv and installing packages I opened the CMD Prompt for VS 2022 (which you get when installing the VS Build Tools) as admin. Before this cmd has thrown errors when installing mod_wsgi, however as admin this wasn´t a problem.

I hope this helps someone in the future.

Upvotes: 0

Related Questions