我正在 Webfaction 上设置实时服务器的开发版本,在我的本地机器 - XP 上的虚拟 Apache 服务器环境中运行 Django 应用程序(运行无任何错误),运行带有 Python 2.6 的 XAMPP Lite - 我可以通过 Git 提交更改。
XAMPP 使用 Python 正常运行,服务器完美启动并加载了 WSGI 模块。问题是当我设置我的 Python 路径时,它们一半设置为 'nix 格式(带/),一半设置为 Windows 格式(带反斜杠)。
这是本地机器 Apache 错误,显示损坏的 python 路径:
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] mod_wsgi (pid=1436): Exception occurred processing WSGI script 'C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/dev.wsgi'.
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] File "C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/lib/python2.5\\django\\core\\handlers\\wsgi.py", line 230, in __call__
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] self.load_middleware()
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] File "C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/lib/python2.5\\django\\core\\handlers\\base.py", line 42, in load_middleware
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] ImproperlyConfigured: Error importing middleware cms.middleware.multilingual: "No module named cms.middleware.multilingual"
还有违规的 .wsgi 文件内容:
import os, sys
sys.path.append('C:/SERVER/Python26/')
sys.path.append('C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django')
sys.path.append('C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/lib/python2.5')
from django.core.handlers.wsgi import WSGIHandler
#Add the path to Django itself
os.environ['DJANGO_SETTINGS_MODULE'] = 'website.settings'
application = WSGIHandler()
Apache httpd.conf 是 XAMPP(而不是虚拟实例)的默认设置,添加以下内容以加载 wsgi 模块
LoadModule wsgi_module modules/mod_wsgi-win32-ap22py26-3.3.so
& 指向 wsgi 文件:
WSGIScriptAlias / C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/dev.wsgi
我知道 XAMPP 服务器正在使用 Python2.6(我被迫使用 TortoiseGIT)并且生产是在 2.5 上(由网络主机强制执行),但这似乎不是罪魁祸首 - 我会还是希望至少能设置正确的路径!
欢迎所有关于获得 Python 路径的建议!
最佳答案
我的 PC 有 Python 2.6,所以我将在假设 Python 2.6 是目标版本的情况下使用所有配置。
- 下载最新的 xampp ( http://www.apachefriends.org/en/xampp-windows.html ),截至 2010 年 11 月 29 日,1.7.3 版是最新的。
- 安装xampp for windows,我安装在c:\xampp
- 下载并安装 Python 2.6 ( http://www.python.org/download/releases/2.6/ )
- 下载 wsgi for windows - http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2 如有必要,请参阅文档 - http://code.google.com/p/modwsgi/wiki/InstallationOnWindows
- 将so文件复制到模块目录C:\xampp\apache\modules,别忘了重命名为mod_wsgi.so
- 将以下行添加到C:\xampp\apache\conf\httpd.conf
- LoadModule wsgi_module modules/mod_wsgi.so
- 使用 C:\xampp\xampp-control.exe 重新启动 apache
为了进行测试,我执行了以下步骤。
- 创建C:\xampp\htdocs\wsgi\scripts 目录,并复制测试test.wsgi。
test.wsgi如下。
#!/usr/bin/env python
"""
A simple WSGI test application.
Its main purpose is to show that WSGI support works (meaning that the
web server and the WSGI adaptor / support module are configured correctly).
As a nice plus, it outputs some interesting system / WSGI values as a nice
HTML table.
The main use of this script will be using the WSGI "application" defined
below within your production WSGI environment. You will use some code similar
to what you see at the end of this script to use the application from that
environment. For the special case of apache2/mod_wsgi, it shoud be possible
to directly use this file.
If you start this script from the commandline either with python2.5 or with
and older python + wsgiref module installed, it will serve the content on
http://localhost:8000/ - this is mainly for debugging THIS script.
@copyright: 2008 by MoinMoin:ThomasWaldmann
@license: Python License, see LICENSE.Python for details.
"""
import os.path
import os
import sys
try:
__file__
except NameError:
__file__ = '?'
html_template = """\
<html>
<head>
<title>WSGI Test Script</title>
</head>
<body>
<h1>WSGI test script is working!</h1>
<table border=1>
<tr><th colspan=2>1. System Information</th></tr>
<tr><td>Python</td><td>%(python_version)s</td></tr>
<tr><td>Python Path</td><td>%(python_path)s</td></tr>
<tr><td>Platform</td><td>%(platform)s</td></tr>
<tr><td>Absolute path of this script</td><td>%(abs_path)s</td></tr>
<tr><td>Filename</td><td>%(filename)s</td></tr>
<tr><th colspan=2>2. WSGI Environment</th></tr>
%(wsgi_env)s
</table>
</body>
</html>
"""
row_template = " <tr><td>%s</td><td>%r</td></tr>"
def application(environ, start_response):
mysite = '/Users/smcho/Desktop/django'
if mysite not in sys.path:
sys.path.insert(0,'/Users/smcho/Desktop/django')
mysite = '/Users/smcho/Desktop/django/mysite'
if mysite not in sys.path:
sys.path.insert(0,'/Users/smcho/Desktop/django/mysite')
""" The WSGI test application """
# emit status / headers
status = "200 OK"
headers = [('Content-Type', 'text/html'), ]
start_response(status, headers)
# assemble and return content
content = html_template % {
'python_version': sys.version,
'platform': sys.platform,
'abs_path': os.path.abspath('.'),
'filename': __file__,
'python_path': repr(sys.path),
'wsgi_env': '\n'.join([row_template % item for item in environ.items()]),
}
return [content]
if __name__ == '__main__':
# this runs when script is started directly from commandline
try:
# create a simple WSGI server and run the application
from wsgiref import simple_server
print "Running test application - point your browser at http://localhost:8000/ ..."
httpd = simple_server.WSGIServer(('', 8000), simple_server.WSGIRequestHandler)
httpd.set_app(application)
httpd.serve_forever()
except ImportError:
# wsgiref not installed, just output html to stdout
for content in application({}, lambda status, headers: None):
print content
- 制作具有以下内容的C:\xampp\apache\conf\other\wsgi.conf
这是代码
<Directory "C:/xampp/htdocs/wsgi/scripts">
Options ExecCGI Indexes
AddHandler cgi-script .cgi
AddHandler wsgi-script .wsgi
Order allow,deny
Allow from all
</Directory>
Alias /wsgi/ "C:/xampp/htdocs/wsgi/scripts/"
<IfModule wsgi_module>
WSGIScriptAlias /test "C:/xampp/htdocs/wsgi/scripts/test.wsgi"
</IfModule>
- 将此行添加到 httpd.conf Include "conf/other/wsgi.conf"
- 重新启动 Apache。
- 当您在网络浏览器中输入“localhost/test”或“localost/wsgi/test.wsgi”时,您会看到 wsgi 信息。
关于python - 使用 WSGI 在 Windows XAMPP 中设置 Python 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891996/