简体   繁体   English

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

[英]Setting Python Path in Windows XAMPP using WSGI

I'm setting up a development version of a live server on Webfaction, running Django apps in a virtual Apache server environment (running without any errors) on my local machine - XP, running XAMPP Lite with Python 2.6 - which I can commit changes from via Git. 我正在Webfaction上设置一个实时服务器的开发版本,在我的本地机器上运行Django应用程序(在没有任何错误的情况下运行) - XP,运行带有Python 2.6的XAMPP Lite - 我可以提交更改通过Git。

XAMPP is up and running OK with Python, and the server starts perfectly with WSGI module loaded. XAMPP启动并运行正常,并且服务器在加载WSGI模块时完美启动。 The problem is when I set my Python paths, they are set half in 'nix format (with /), and half in Windows (with backslashes). 问题是当我设置我的Python路径时,它们以'nix格式(带/)设置为一半,在Windows中设置一半(带反斜杠)。

Here's the local machine Apache error, showing the corrupted python paths: 这是本地机器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"

And the offending .wsgi file contents: 和违规的.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()

The Apache httpd.conf is the default for XAMPP (and not a virtual instance), with the following added to load the wsgi module Apache httpd.conf是XAMPP(而不是虚拟实例)的默认设置,添加了以下内容以加载wsgi模块

LoadModule wsgi_module modules/mod_wsgi-win32-ap22py26-3.3.so

& to point to the wsgi file: &指向wsgi文件:

WSGIScriptAlias / C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/dev.wsgi

I know the XAMPP server is using Python2.6 (I'm forced to to use TortoiseGIT) and the production is on 2.5 (enfordced by the web host), but that doesn't seem to be the culprit - I would still expect to be able to set the correct path at least! 我知道XAMPP服务器正在使用Python2.6(我被迫使用TortoiseGIT)并且生产是2.5(由网络主机提供),但这似乎不是罪魁祸首 - 我仍然希望能够至少设置正确的路径!

All suggestions on getting the Python path to play ball welcomed! 关于获取Python路径的所有建议都欢迎!

My PC has Python 2.6, so I'll use all the configuration under the assumption that Python 2.6 is the target version. 我的PC有Python 2.6,因此我假设Python 2.6是目标版本,我将使用所有配置。

  1. Download newest xampp ( http://www.apachefriends.org/en/xampp-windows.html ), as of Nov 29, 2010, ver 1.7.3 is the newest. 下载最新的xampp( http://www.apachefriends.org/en/xampp-windows.html ),截至2010年11月29日,ver 1.7.3是最新版本。
  2. Install xampp for windows, I installed it c:\\xampp 安装xampp for windows,我安装了c:\\ xampp
  3. Download and install Python 2.6 ( http://www.python.org/download/releases/2.6/ ) 下载并安装Python 2.6( http://www.python.org/download/releases/2.6/
  4. Download wsgi for windows - http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2 Refer to the documentation if necessary - http://code.google.com/p/modwsgi/wiki/InstallationOnWindows 下载wsgi for windows - http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2如有必要,请参阅文档 - http://code.google.com/p/modwsgi/wiki/InstallationOnWindows
  5. Copy the so file to module directory C:\\xampp\\apache\\modules , don't forget to rename it mod_wsgi.so 将so文件复制到模块目录C:\\ xampp \\ apache \\ modules ,不要忘记将其重命名为mod_wsgi.so
  6. Add the following line to C:\\xampp\\apache\\conf\\httpd.conf 将以下行添加到C:\\ xampp \\ apache \\ conf \\ httpd.conf
    • LoadModule wsgi_module modules/mod_wsgi.so LoadModule wsgi_module modules / mod_wsgi.so
  7. Relaunch apache using C:\\xampp\\xampp-control.exe 使用C:\\ xampp \\ xampp-control.exe重新启动apache

For testing I did the following steps. 为了测试,我做了以下步骤。

  1. Make C:\\xampp\\htdocs\\wsgi\\scripts directory, and copy the test test.wsgi. 创建C:\\ xampp \\ htdocs \\ wsgi \\ scripts目录,并复制测试test.wsgi。

The test.wsgi is as follows. 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. Make C:\\xampp\\apache\\conf\\other\\wsgi.conf that has the following content 创建具有以下内容的C:\\ xampp \\ apache \\ conf \\ other \\ wsgi.conf

This is the code 这是代码

<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. Add this line to the httpd.conf Include "conf/other/wsgi.conf" 将此行添加到httpd.conf 包含“conf / other / wsgi.conf”
  2. Relaunch apache. 重新启动apache。
  3. You'll see wsgi info when you enter 'localhost/test' or 'localost/wsgi/test.wsgi' in your web browser. 当您在Web浏览器中输入“localhost / test”或“localost / wsgi / test.wsgi”时,您将看到wsgi信息。

我也有“服务器错误”,并在Apache error.log文件中看到:它是由于一些空格或换行符与注释行“它的主要目的是......”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM