Définition du chemin Python dans Windows XAMPP à l’aide de WSGI

Je mets en place une version de développement d’un serveur live sur Webfaction, exécutant des applications Django dans un environnement de serveur virtuel Apache (exécuté sans erreur) sur ma machine locale – XP, exécutant XAMPP Lite avec Python 2.6 – que je peux valider via Git.

XAMPP fonctionne correctement avec Python et le serveur démarre parfaitement avec le module WSGI chargé. Le problème est que lorsque je configure mes chemins Python, ils sont définis à moitié dans le format nix (avec /) et à moitié dans Windows (avec des barres obliques inverses).

Voici l’erreur Apache de la machine locale, montrant les chemins Python corrompus:

[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" 

Et le contenu du fichier .wsgi incriminé:

 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 est la valeur par défaut pour XAMPP (et non une instance virtuelle), avec les éléments suivants ajoutés pour charger le module wsgi

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

& pour pointer vers le fichier wsgi:

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

Je sais que le serveur XAMPP utilise Python2.6 (je suis obligé d’utiliser TortoiseGIT) et que la production est sur la version 2.5 (avec l’hôte Web), mais cela ne semble pas être le coupable. être capable de définir le bon chemin au moins!

Toutes les suggestions pour que le chemin Python joue à la balle sont les bienvenues!

Mon PC a Python 2.6, donc j’utiliserai toute la configuration en supposant que Python 2.6 est la version cible.

  1. Télécharger le dernier xampp ( http://www.apachefriends.org/en/xampp-windows.html ), à compter du 29 novembre 2010, ver 1.7.3 est le plus récent.
  2. Installer xampp pour windows, je l’ai installé c: \ xampp
  3. Téléchargez et installez Python 2.6 ( http://www.python.org/download/releases/2.6/ )
  4. Téléchargez wsgi pour Windows – http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2 Reportez-vous à la documentation si nécessaire – http://code.google.com/p/modwsgi/wiki/InstallationOnWindows
  5. Copiez le fichier so dans le répertoire C: \ xampp \ apache \ modules , n’oubliez pas de le renommer mod_wsgi.so
  6. Ajoutez la ligne suivante à C: \ xampp \ apache \ conf \ httpd.conf
    • LoadModule wsgi_module modules / mod_wsgi.so
  7. Relancez Apache en utilisant C: \ xampp \ xampp-control.exe

Pour les tests, j’ai effectué les étapes suivantes.

  1. Créez le répertoire C: \ xampp \ htdocs \ wsgi \ scripts et copiez le test test.wsgi.

Le test.wsgi est le suivant.

 #!/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 = """\   WSGI Test Script   

WSGI test script is working!

%(wsgi_env)s
1. System Information
Python%(python_version)s
Python Path%(python_path)s
Platform%(platform)s
Absolute path of this script%(abs_path)s
Filename%(filename)s
2. WSGI Environment
""" row_template = " %s%r" 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. Créez C: \ xampp \ apache \ conf \ other \ wsgi.conf qui a le contenu suivant

C’est le code

  Options ExecCGI Indexes AddHandler cgi-script .cgi AddHandler wsgi-script .wsgi Order allow,deny Allow from all  Alias /wsgi/ "C:/xampp/htdocs/wsgi/scripts/"  WSGIScriptAlias /test "C:/xampp/htdocs/wsgi/scripts/test.wsgi"  
  1. Ajoutez cette ligne au fichier httpd.conf Include “conf / other / wsgi.conf”
  2. Relancez Apache.
  3. Vous verrez les informations wsgi lorsque vous entrez «localhost / test» ou «localost / wsgi / test.wsgi» dans votre navigateur Web.

J’ai aussi eu “l’erreur de serveur” et je suis allé voir dans le fichier error.log d’Apache: c’était dû à un espace blanc ou à un saut de ligne avec la ligne de commentaire “Son but principal est …”