The problem

Apache gives an error 500 (internal server error) when trying to access website running django 1.9 using wsgi based on the tutorial given at https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04. Running the server using the developmental server (manage.py runserver) works fine.

Looking in the apache error log reveals:

[stuff] mod_wsgi (pid=20304): Target WSGI script '/django/XY_django/XY/XY/wsgi.py' cannot be loaded as Python module. 
[stuff] mod_wsgi (pid=20304): Exception occurred processing WSGI script '/django/XY_django/XY/XY/wsgi.py'.

[python traceback]

[stuff] ImportError: No module named ‘XY’

[this error is repeated twice]

Various resources mention the same or similar problems:

  1. http://stackoverflow.com/questions/6454564/target-wsgi-script-cannot-be-loaded-as-python-module
  2. http://stackoverflow.com/questions/9462212/import-error-no-module-named-django
  3. lots of others

Things I tried

  1. Wrong ownership of django dir and files.
    • Was root, changed to www-data.
    • No change.
  1. Wrong version of wsgi.
    • Ran:
    • sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
      sudo apt-get install libapache2-mod-wsgi-py3
    • No, used the correct version.
  1. Wrong code with the static block in apache config.
    • No, error persists after commenting it out.
  1. WSGIDaemonProcess python-path is wrong.
    • I noticed that the path given in the tutorial has two folders separated by :, not just one:
    • WSGIDaemonProcess django python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-package
      WSGIDaemonProcess django python-path=/django/env/lib/python3.4/site-packages
    • Replaced with:
    • WSGIDaemonProcess django python-path=/django/XY_django/XY:/django/env/lib/python3.4/site-packages
    • Worked!