Issue
I'm trying to configure mod_wsgi
to run with Apache so I can use Django on Apache on the same virtual host, along with the previous Apache+PHP configuration. I've been trying to run my Django app on the http://localhost/django
address. Following is my configuration:
$ cat /etc/httpd/sites-available/localhost.conf
<VirtualHost *:80>
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
</VirtualHost>
# 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
When I go to http://localhost/
, I see the script /home/httpd/localhost/public_html/index.php
run, as expected. However, when I visit http://localhost/django
, I get a 500 Internal Server Error. I get the following stack trace in /home/httpd/localhost/error.log
(removed timestamps and other [bracketed] stuff for readability):
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 <module>
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 "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'mysite'
Note that the /home/httpd/localhost/mysite/
directory was created by the django-admin.py
script.
Also note that just running ./manage.py runserver
works fine.
Solution
You haven't done anything to put the Django project on the Python path. As this to the top of your wsgi script:
import sys
sys.path.insert(0, '/home/httpd/localhost/mysite')
Answered By - Daniel Roseman