简体   繁体   中英

Apache with mod_wsgi emitting error concerning database in django

First off, I have searched and tried numerous answers on this topic but none seems to help. For some reason, I am having a weird error with Apache and django. Here's my setup: I am running Mac OS X El Capitan, I am using the OS Apache 2.4 and I have installed mod_wsgi extension. I can run django through the terminal using the "runserver" command without any issues. I am also running my django project in a virtual environment. I installed python3 so that I don't have to use the OS python 2. The virtual environment is therefore based on python 3. My django project connects to mysql using the mysqlclient driver as recommended in the django docs. My django version is 1.9. I have configured my apache vhost file to serve my django project using this vhost configuration:

<VirtualHost *:80>

ServerName simplesurvey.proj
ServerAdmin admin@simplesurvey.proj

Alias /static /Users/UncharteD/Sites/project/survey/static/
<Directory /Users/UncharteD/Sites/project/survey/static/>
    Require all granted
</Directory>

<Directory /Users/UncharteD/Sites/project/surveyproject/>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess simplesurvey python-path=/Users/UncharteD/Sites/project/:/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages
WSGIProcessGroup simplesurvey
WSGIScriptAlias / /Users/UncharteD/Sites/project/surveyproject/wsgi.py

I am getting a 500 error status in my web browser. I have added simplesurvey.proj as an entry to my host file. Here is the browser message precisely:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

I checked my Apache error log and it consists of this:

Full Apache error log

The most important bit (I think) is the stack trace. Why is it failing to load a module when the command line version can perfectly do everything? Here is a bit from the log:

   [Thu May 05 18:31:52.828568 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0] mod_wsgi (pid=64376): Target WSGI script '/Users/UncharteD/Sites/project/surveyproject/wsgi.py' cannot be loaded as Python module.
[Thu May 05 18:31:52.828660 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0] mod_wsgi (pid=64376): Exception occurred processing WSGI script '/Users/UncharteD/Sites/project/surveyproject/wsgi.py'.
[Thu May 05 18:31:52.828681 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0] Traceback (most recent call last):
[Thu May 05 18:31:52.828701 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/Sites/project/surveyproject/wsgi.py", line 16, in <module>
[Thu May 05 18:31:52.828779 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     application = get_wsgi_application()
[Thu May 05 18:31:52.828792 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Thu May 05 18:31:52.828875 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     django.setup()
[Thu May 05 18:31:52.828898 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
[Thu May 05 18:31:52.828973 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     apps.populate(settings.INSTALLED_APPS)
[Thu May 05 18:31:52.828991 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
[Thu May 05 18:31:52.829136 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     app_config.import_models(all_models)
[Thu May 05 18:31:52.829150 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
[Thu May 05 18:31:52.829224 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     self.models_module = import_module(models_module_name)
[Thu May 05 18:31:52.829234 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu May 05 18:31:52.829309 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     __import__(name)
[Thu May 05 18:31:52.829320 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/Sites/project/survey/models.py", line 22, in <module>
[Thu May 05 18:31:52.829432 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     class Genre(models.Model):
[Thu May 05 18:31:52.829441 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/models/base.py", line 108, in __new__
[Thu May 05 18:31:52.829807 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     new_class.add_to_class('_meta', Options(meta, app_label))
[Thu May 05 18:31:52.829817 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/models/base.py", line 307, in add_to_class
[Thu May 05 18:31:52.829844 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     value.contribute_to_class(cls, name)
[Thu May 05 18:31:52.829852 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/models/options.py", line 263, in contribute_to_class
[Thu May 05 18:31:52.830098 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
[Thu May 05 18:31:52.830109 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/__init__.py", line 36, in __getattr__
[Thu May 05 18:31:52.830150 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     return getattr(connections[DEFAULT_DB_ALIAS], item)
[Thu May 05 18:31:52.830158 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/utils.py", line 212, in __getitem__
[Thu May 05 18:31:52.830249 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     backend = load_backend(db['ENGINE'])
[Thu May 05 18:31:52.830258 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/utils.py", line 116, in load_backend
[Thu May 05 18:31:52.830271 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     return import_module('%s.base' % backend_name)
[Thu May 05 18:31:52.830279 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu May 05 18:31:52.830291 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     __import__(name)
[Thu May 05 18:31:52.830297 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]   File "/Users/UncharteD/.virtualenvs/survey/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 28, in <module>
[Thu May 05 18:31:52.830400 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0]     raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
[Thu May 05 18:31:52.830420 2016] [wsgi:error] [pid 64376] [remote 127.0.0.1:0] ImproperlyConfigured: Error loading MySQLdb module: No module named _mysql

Any help on how to fix this would be appreciated. Thanks

Maybe a permission issue?

Apache has it's own user, so maybe that user don't have access to your virtualenv path, specially since it's inside a user folder. That's usually fixed by adding the apache user to your usergroup.

At least I know that's how it works for Linux, I'm guessing it kinda the same for Macs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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