简体   繁体   中英

apache wsgi django path issue frustration

I have read similar questions/answers but I cant resolve the situation. So I have installed WSGI and want to serve my django app from Apache.

What I did initially was to create with virtualenv the folder testing which holds the python related stuff. So I did

  virtualenv environment

My vhost file looks like:

<VirtualHost *:8001>
WSGIDaemonProcess myapp python-path=/var/www/vhosts/myapp.com/:/var/www/vhosts/myapp.com/environment/lib/python2.7/:/var/www/vhosts/myapp.com/environment/lib/python2.7/sites-available
WSGIScriptAlias / /var/www/vhosts/myapp/testing/wsgi.py
...
...

My wsgi.py is:

 import os

 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")

 from django.core.wsgi import get_wsgi_application

 application = get_wsgi_application()

Once visiting the webpage I am getting a Server Internal Error and the error.log says 2 error messages basically:

 [wsgi:error] [pid 31165] [client 192.168.0.3:31337] mod_wsgi (pid 31165): Target WSGI script '/var/www/vhosts/myapp.com/testing/wsgi.py' cannot be loaded as Python Module

and right after several lines that end to:

 [wsgi:error] [pid 31165] [client 192.168.0.3:31337] mod_wsgi (pid 31165): ImportError: Could not import settings 'testing.settings' (is it on sys.path? Is there an import error in the settings file?)

I found regarding the first error msg this Target WSGI script cannot be loaded as Python module however I couldnt resolve the issue. What I get is that this error is not stopping Apache from trying to load my python (django) project since the next error is what I think causes the 500 response. For the second error I found that is a matter of adding the paths (something that I have done to my vhost file! ?)

I tried other solutions such as adding the WSGIPythonPath on my apache.conf but didnt work.

My wsgi.py file is as follows:

 import os
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")

 from django.core.wsgi import get_wsgi_application

 application = get_wsgi_application()

I also tried adding

  import sys
  os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
  sys.path.append('/var/www/vhosts/myapp/testing')
  import mysite.settings

didnt work.

You could try moving wsgi.py and settings.py to /var/www/vhosts/myapp/testing/mysite

WSGIPythonPath /var/www/vhosts/myapp/testing
<VirtualHost *:8000>

    WSGIScriptAlias / /var/www/vhosts/myapp/testing/mysite/wsgi.py
    # WSGIApplicationGroup is needed to prevent some problem?
    WSGIApplicationGroup %{GLOBAL}
</VirtualHost>

and in the wsgi.py file

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
sys.path.append('/var/www/vhosts/myapp/testing')
import mysite.settings

I think I had similar issues when I upgraded django. Things started working after I relocated settings.py and wsgi.py

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