J’essaie de configurer mod_wsgi
pour qu’il fonctionne avec Apache afin que je puisse utiliser Django sur Apache sur le même hôte virtuel, avec la configuration antérieure d’Apache + PHP. J’ai essayé d’exécuter mon application Django sur l’ http://localhost/django
. Voici ma configuration:
$ cat /etc/httpd/sites-available/localhost.conf ServerName localhost ServerAdmin admin@localhost # ServerAlias foo.localhost WSGIScriptAlias /django /home/httpd/localhost/mysite/mysite/wsgi.py DocumentRoot /home/httpd/localhost/public_html ErrorLog /home/httpd/localhost/error.log CustomLog /home/httpd/localhost/requests.log combined # vim: syntax=apache $ ls -l /etc/httpd/sites-enabled/localhost.conf lrwxrwxrwx 1 root root 41 Jan 17 08:21 /etc/httpd/sites-enabled/localhost.conf -> /etc/httpd/sites-available/localhost.conf $ cat /etc/httpd/conf/httpd.conf ... # Include virtual hosts IncludeOptional sites-enabled/*.conf $ cat /home/httpd/localhost/mysite/mysite/wsgi.py """ WSGI config for mysite project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") application = get_wsgi_application() $ ls -AR /home/httpd/localhost/ /home/httpd/localhost/: django.wsgi error.log mysite public_html requests.log /home/httpd/localhost/mysite: db.sqlite3 manage.py mysite /home/httpd/localhost/mysite/mysite: __init__.py __pycache__ settings.py urls.py wsgi.py /home/httpd/localhost/mysite/mysite/__pycache__: __init__.cpython-34.pyc settings.cpython-34.pyc urls.cpython-34.pyc wsgi.cpython-34.pyc /home/httpd/localhost/public_html: index.php
Lorsque je vais sur http://localhost/
, je vois le script /home/httpd/localhost/public_html/index.php
exécuté, comme prévu. Cependant, lorsque je visite http://localhost/django
, je reçois une erreur de serveur interne 500. /home/httpd/localhost/error.log
trace de stack suivante dans /home/httpd/localhost/error.log
(horodatage supprimé et autres trucs [entre parenthèses] pour la lisibilité):
mod_wsgi (pid=16908): Target WSGI script '/home/httpd/localhost/mysite/mysite/wsgi.py' cannot be loaded as Python module. mod_wsgi (pid=16908): Exception occurred processing WSGI script '/home/httpd/localhost/mysite/mysite/wsgi.py'. Traceback (most recent call last): File "/home/httpd/localhost/mysite/mysite/wsgi.py", line 16, in application = get_wsgi_application() File "/usr/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application django.setup() File "/usr/lib/python3.4/site-packages/django/__init__.py", line 17, in setup configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "/usr/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__ self._setup(name) File "/usr/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup self._wrapped = Settings(settings_module) File "/usr/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/usr/lib64/python3.4/importlib/__init__.py", line 109, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 2254, in _gcd_import File "", line 2237, in _find_and_load File "", line 2212, in _find_and_load_unlocked File "", line 321, in _call_with_frames_removed File "", line 2254, in _gcd_import File "", line 2237, in _find_and_load File "", line 2224, in _find_and_load_unlocked ImportError: No module named 'mysite'
Notez que le répertoire /home/httpd/localhost/mysite/
été créé par le script django-admin.py
.
Notez également que l’exécution de ./manage.py runserver
fonctionne ./manage.py runserver
.
Vous n’avez rien fait pour placer le projet Django sur le chemin Python. En haut de votre script wsgi:
import sys sys.path.insert(0, '/home/httpd/localhost/mysite')
essaye ça,
Ajoutez votre chemin de projet à sys.path
import os import sys sys.path.append('/home/httpd/localhost/mysite') from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") application = get_wsgi_application()