在Windows XAMPP中使用WSGI设置Python路径

2 投票
2 回答
17435 浏览
提问于 2025-04-16 05:12

我正在Webfaction上设置一个开发版本的在线服务器,运行Django应用程序,环境是一个虚拟的Apache服务器(没有任何错误),我在本地机器上使用的是XP系统,运行XAMPP Lite和Python 2.6,并且可以通过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服务器使用的是Python 2.6(我必须使用TortoiseGIT),而生产环境是2.5(这是网络主机强制要求的),但这似乎不是问题所在——我仍然希望能够正确设置路径!

欢迎大家提供任何关于如何让Python路径正常工作的建议!

2 个回答

1

我也遇到了“服务器错误”,于是去查看了Apache的错误日志文件。发现问题是因为注释行“它的主要目的是...”后面有一些空格或者换行符。

9

我的电脑上装的是Python 2.6,所以我会在这个基础上进行所有的配置。

  1. 首先,下载最新的xampp,链接是http://www.apachefriends.org/en/xampp-windows.html,截至2010年11月29日,最新版本是1.7.3。
  2. 安装xampp,我把它安装在c:\xampp这个路径下。
  3. 接着,下载并安装Python 2.6,链接是http://www.python.org/download/releases/2.6/
  4. 然后,下载适用于Windows的wsgi,链接是http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2。如果需要,可以参考文档,链接是http://code.google.com/p/modwsgi/wiki/InstallationOnWindows
  5. 把下载的so文件复制到模块目录 C:\xampp\apache\modules,别忘了把它重命名为mod_wsgi.so。
  6. C:\xampp\apache\conf\httpd.conf 文件中添加以下一行:
    • LoadModule wsgi_module modules/mod_wsgi.so
  7. 使用 C:\xampp\xampp-control.exe 重新启动apache。

为了测试,我做了以下步骤。

  1. 创建一个目录 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
  1. 创建一个文件 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>
  1. 在 httpd.conf 文件中添加这一行 Include "conf/other/wsgi.conf"
  2. 重新启动apache。
  3. 在你的网页浏览器中输入 'localhost/test' 或 'localhost/wsgi/test.wsgi',你会看到wsgi的信息。

撰写回答