2010-10-08 64 views
2

我在Webfaction上設置了一個活動服務器的開發版本,在本地機器XP上運行XAMPP,在虛擬Apache服務器環境中運行Django應用程序(運行時沒有任何錯誤) Lite with Python 2.6 - 我可以通過Git提交更改。使用WSGI在Windows XAMPP中設置Python路徑

XAMPP啓動並運行良好,並且服務器啓動完美,加載WSGI模塊。問題是,當我設置我的Python路徑時,它們以'nix格式(使用/)設置爲一半,而在Windows中設置爲一半(使用反斜槓)。

這裏的本地機器Apache的錯誤,表現出損壞的蟒蛇路徑:

[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(由虛擬主機enfordced)但這似乎並不是罪魁禍首 - 我仍然期望能夠至少設置正確的路徑!

所有關於獲取Python路徑的建議歡迎使用球!

回答

7

我的電腦有Python 2.6,所以我會在假設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. for Windows下載WSGI - http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2 如果必要的話,請參考文檔 - http://code.google.com/p/modwsgi/wiki/InstallationOnWindows
  5. 複製等等文件到模塊目錄C:\ XAMPP的\ apache的\模塊,不要忘了將它更名mod_wsgi.so
  6. 以下行添加到C:\ XAMPP的\ apache的\的conf \ httpd.conf中
    • 的LoadModule wsgi_module模塊/ mod_wsgi.so
  7. 下重新啓動apache的使用C:\ XAMPP \ XAMPP-CONTROL.EXE

爲了測試我做了以下的步驟。

  1. C:\ XAMPP \ htdocs中\ WSGI \腳本目錄,並複製測試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 \阿帕奇\ CONF \其他\ 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 包含 「CONF /其他/ wsgi.conf」
  • 重新啓動apache。
  • 當您在Web瀏覽器中輸入'localhost/test'或'localost/wsgi/test.wsgi'時,您將看到wsgi信息。
  • +0

    +1這幫助我解決了Python在XAMPP問題上的問題。 – 2011-06-03 22:06:35

    +0

    如果遇到問題,就像我一樣,嘗試使用'C:\ xampp \ apache_start.bat'手動啓動apache;確保你的Python版本與mod_wsgi所需的版本完全相同;並避免64位Python。 – dirkjot 2011-08-17 13:54:57

    +0

    感謝您的腳本 - 如果您使用的是Python3,則需要將頭文件打包,需要將第84行的print()和最後一行包含在函數調用中,並且出於某種原因它不喜歡85上的格式化,所以我評論道出來。這些被解析爲語法錯誤,但之後,它的工作。 – 2013-11-30 00:40:26

    1

    我也有「服務器錯誤」,並去Apache的error.log文件中看到:這是由於一些空白或換行符與評論行來「它的主要目的是.. .. 。「