如何在Windows上配置Apache和mod_wsgi以进行本地Django开发

1 投票
1 回答
926 浏览
提问于 2025-04-16 20:36

我现在所在的团队已经不再适合使用django开发服务器来运行我们的django应用程序了,因为我们的本地开发环境已经变得比较复杂。这个环境(服务器是2008年的虚拟机)包含了一些基于IIS7的.net应用程序,还有几个django应用。

我们需要能够在本地开发环境中同时运行所有应用,这样方便开发和测试。因此,我们决定使用一个完整的apache实例,与IIS一起运行,这样就能更接近我们的生产和测试环境(当然,apache的主机是linux而不是windows)。

我们已经配置好了mod_wsgi和apache在本地运行,但似乎我们的python或django的路径没有配置正确,因为在运行时我们的应用程序报错,提示找不到视图,错误信息像这样:

无法导入 reporting.views。 错误是:DLL加载失败:找不到指定的模块。

django的异常位置显示:

Exception Location: C:\Python27\lib\site-packages\django\core\urlresolvers.py in _get_callback, line 132

所以我们猜测这可能是某种路径问题,但到目前为止我们还没有找到具体出错的地方。

谢谢大家。

LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome X:\PathToApplication\venv\Scripts

<VirtualHost *:8000>
    ServerName applicationdomain
    ServerAlias applicationapidomain

    SetEnv DJANGO_ENV local

    WSGIScriptAlias / X:/PathToApplication/apache/django.wsgi
    <Directory X:/PathToApplication/ >
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:8001>
    ServerName applicationdomain

    SetEnv DJANGO_ENV local

    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
    SSLCertificateFile "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\wildcard.crt"
    SSLCertificateKeyFile "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\wildcard.key"

    WSGIScriptAlias / X:/PathToApplication/apache/django.wsgi
    <Directory X:/PathToApplication/ >
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

1 个回答

4

你的问题跟编译模块和mod_wsgi有关。

从Python.org下载的Python版本里,有一个内嵌的清单文件,这个文件可以让它加载DLL文件。以前在编译C模块时,Python会把这个清单嵌入到编译好的模块里,但现在默认情况下它会把这个清单去掉。问题在于,mod_wsgi自带了一个Python解释器,而这个解释器里没有包含那个清单文件。

我觉得要让这个工作正常,你需要选择以下几种方法之一:用MingW编译,给Apache嵌入一个清单,或者修改Python,让它在编译模块时嵌入清单。

http://www.mail-archive.com/modwsgi@googlegroups.com/msg06255.html 上有一个人分享了他是如何把清单嵌入到apache2里的。

如果我没记错的话,在Python27/Lib/distutils/msvc9compiler.py的680行左右,应该有一段代码看起来像这样:

try:
    # Remove references to the Visual C runtime, so they will
    # fall through to the Visual C dependency of Python.exe.
    # This way, when installed for a restricted user (e.g.
    # runtimes are not in WinSxS folder, but in Python's own
    # folder), the runtimes do not need to be in every folder
    # with .pyd's.
    manifest_f = open(manifest_file)
    try:
        manifest_buf = manifest_f.read()
    finally:
        manifest_f.close()
    pattern = re.compile(
        r"""<assemblyIdentity.*?name=("|')Microsoft\.""" \
        r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""",
        re.DOTALL)
    manifest_buf = re.sub(pattern, "", manifest_buf)
    pattern = "<dependentAssembly>\s*</dependentAssembly>"
    manifest_buf = re.sub(pattern, "", manifest_buf)
    manifest_f = open(manifest_file, 'w')
    try:
        manifest_f.write(manifest_buf)
    finally:
        manifest_f.close()
except IOError:
    pass

去掉或者注释掉这段代码,应该就能阻止Python去掉清单文件。

撰写回答