Apache24 上的 DJango 部署无法正常工作

aja*_*808 5 python apache django virtualhost virtualenv

我是 Web 框架的新手,我使用 DJango-3.1.7 设计了小型网站。

我正在尝试在 Windows 10 平台的 Apache 服务器上部署 DJango 网站,但似乎有些东西不正确,或者我错过了它。如果有人可以帮助解决这个问题,我们对我来说很珍贵。

我已经为此应用程序配置了虚拟环境

问题:

出现错误 PYTHONHOME =(未设置) PYTHONPATH =(未设置)

错误日志

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.
Run Code Online (Sandbox Code Playgroud)

等/主机

127.0.0.2    stack-hack.com
Run Code Online (Sandbox Code Playgroud)

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 info@admin.com
    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>
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

项目结构

Stack-Hack  [Virtual env]
- Lib
- Scripts
+ src
  - home
  - media
  + stackhack
    - asgi.py
    - settings.py
    - urls.py
    - wsgi.py
  - static
  - manage.py
- pyvenv.cfg
Run Code Online (Sandbox Code Playgroud)

我所做的解决方法:


所有其他 django 设置都正常并且工作正常。我遵循了各种博客文章来配置 Apache 服务器

  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

HB2*_*B21 2

尝试这个

unset PYTHONPATH

unset PYTHONHOME

基于此解决方案