首页 > 解决方案 > 在 Windows 中使用 Apache + mod_wsgi + Python Django 的 408 请求超时

问题描述

我有一个 Django 应用程序,它在 Windows 下使用 Apache + mod_wsgi 部署在本地网络中。当我跑步时python manage.py runserver,一切正常。但是当我启动 Apache 服务时,我无法访问该应用程序。我从 access.log 得到的唯一响应是错误代码408。下面是我的httpd.conf

LoadFile "c:/users/felix/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/felix/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/felix/appdata/local/programs/python/python37"

ServerName localhost

WSGIScriptAlias / "D:/dev/test_server/django/django/wsgi_windows.py"
Listen 8000
<VirtualHost *:8000>
    WSGIPassAuthorization On
    ErrorLog "logs/django.error.log"
    CustomLog "logs/django.access.log" combined

    Alias /static "D:/dev/test_server/staticfiles"
    <Directory "D:/dev/test_server/staticfiles">
        Require all granted
    </Directory>

    <Directory "D:/dev/test_server/django/django">
        <Files wsgi_windows.py>
            Require all granted
        </Files>
    </Directory>

    <Directory />
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

下面是wsgi_windows.py文件:

# Activate the virtualenv
activate_this = 'D:/dev/test_server/.venv/Scripts/activate_this.py'
exec(open(activate_this).read(), dict(__file__=activate_this))

import os  # noqa
import sys  # noqa
import site  # noqa

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('D:/dev/test_server/.venv/Lib/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('D:/dev/test_server/django')
sys.path.append('D:/dev/test_server/django/django')

os.environ['DJANGO_SETTINGS_MODULE'] = 'django.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django.settings")

from django.core.wsgi import get_wsgi_application  # noqa
application = get_wsgi_application()

我将不胜感激有关该问题的任何想法或提示。

标签: pythondjangoapache

解决方案


  1. 使用“httpd”在命令行启动 apache 并查找错误消息。如果 Apache 在启动时出现问题,则日志文件中没有消息。
  2. 检查 error.log 您甚至可以将 'print('xyz') 放在 settings.py 和其他地方,通过检查 error.log 来查看您的应用程序的设置方式以及请求的处理程度。如果您的应用程序像这样卡在某个地方,您会找到卡住的代码

推荐阅读