'DJango deployment on Apache24 not working

I am new to web frameworks and I have designed small website using DJango-3.1.7.

I am trying to deploy DJango website on Apache server in Windows 10 platform but it seems something not correct or I missed out it. If anyone could help with this we be precious for me.

I have configured virtual environment for this application

Problem:

Getting error PYTHONHOME = (not set) PYTHONPATH = (not set)

error.log

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:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python38'
  sys.base_exec_prefix = 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python38'
  sys.executable = 'C:\\Apache24\\bin\\httpd.exe'
  sys.prefix = 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python38'
  sys.exec_prefix = 'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python38'
  sys.path = [
    'C:\\Users\\Hp\\AppData\\Local\\Programs\\Python\\Python38\\python38.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 0x00002838 (most recent call first):
<no Python frame>
[Sat Jun 26 21:56:47.558067 2021] [mpm_winnt:crit] [pid 5344:tid 780] AH00419: master_main: create child process failed. Exiting.

etc/hosts

127.0.0.2    stack-hack.com

httpd.conf

    LoadFile "c:/users/hp/appdata/local/programs/python/python38/python38.dll"
    LoadModule wsgi_module "e:/Code-Stack/stack-hack/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
    WSGIPythonHome "e:/Code-Stack/stack-hack"
    WSGIPythonPath "e:/Code-Stack/stack-hack/Lib/site-packages"

    <VirtualHost *:80>
    ServerAlias www.stack-hack.com
    ServerName stack-hack.com
    ServerAdmin [email protected]
    WSGIScriptAlias / "E:/Code-Stack/Stack-Hack/src/stackhack/wsgi.py"
    <Directory "E:/Code-Stack/Stack-Hack/src/stackhack">
        <Files wsgi.py>
        Require all granted
        </Files>
    </Directory>

    Alias /static/ "E:/Code-Stack/Stack-Hack/src/static/"
    <Directory "E:/Code-Stack/Stack-Hack/src/static">
        Require all granted
    </Directory>

    ErrorLog "E:/Code-Stack/Stack-Hack/logs/apache.error.log"
    CustomLog "E:/Code-Stack/Stack-Hack/logs/apache.custom.log" common
    </VirtualHost>

wsgi.py

    import os
    import sys
    from django.core.wsgi import get_wsgi_application
    from pathlib import Path

    path_home = str(Path(__file__).parents[1])
    if path_home not in sys.path:
        sys.path.append(path_home)

    os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'

    application = get_wsgi_application()

Project Structure

Stack-Hack  [Virtual env]
- Lib
- Scripts
+ src
  - home
  - media
  + stackhack
    - asgi.py
    - settings.py
    - urls.py
    - wsgi.py
  - static
  - manage.py
- pyvenv.cfg

Workaround I did:


All other django settings are ok and working fine. I have followed various blog post for configuration of Apache Server

  1. https://montesariel.com/blog/post-3
  2. https://www.codementor.io/@aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e


Solution 1:[1]

Finding a possible fix for each error present in the trackback provided, here is what I found that might help:

This error:

Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized

has been discussed in this post, and the working solution was:

I see your PYTHONHOME is set to PYTHONHOME = '/home/env3/educ'. Try to check if it is really there.

The solution for me was to remove the PYTHONHOME environment variable. For you, it can be just that, or setting that variable to another value.

Another possible solution is from reddit here:

I was getting the same error but when I changed the full path of my python virtual environment to the relative path it worked for me, try to change this

virtualenv = /home/env3/%(projectname)

to

virtualenv = your_env

in your uwsgi.ini file


This error:

ModuleNotFoundError: No module named 'encodings'

has been discussed in this post, and the working solution was:

For Python-3 try removing virtual environment files. And resetting it up.

rm -rf venv
virtualenv -p /usr/bin/python3 venv/
source venv/bin/activate
pip install -r requirements.txt

This error:

AH00419: master_main: create child process failed. Exiting.

has been discussed in this post, and the working solution was

I added following lines to my httpd.conf

WSGIPythonPath "C:/Python33/Lib;C:/Python33/Lib/site-packages;C:/Python33/DLLs" WSGIPythonHome "C:/Python33"

Then Apache started to work fine.

Solution 2:[2]

try this

unset PYTHONPATH

unset PYTHONHOME

Based on this solution

Solution 3:[3]

Set PYTHONHOME in your SYSTEM environment, then reboot.

In your case, set it to "C:\Users\Hp\AppData\Local\Programs\Python\Python38".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ann Zen
Solution 2 HB21
Solution 3 gaggleweed