apache mod_wsgi error with django in virtualenv

Flibertyjibbet

I can't seem to find a good answer to this. Who needs to own the virtualenv when running it as a WSGIDaemon? I assume on my OS (Ubuntu 16) www-data, but I want to be sure. Trying some new things to get this thing working based off of the answer from this post...

django apache configuration with WSGIDaemonProcess not working

Does the django project, the virtualenv folder, or both need to be owned by the apache group? What ownerships need to be in place to serve a django project without specifying a port? Why do I get the following?

The root problem:

 Call to 'site.addsitedir()' failed for '(null)'

When I start apache, I get this error. I've followed several different guides, including: http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html and https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/ but have had zero success.

My virtual environment path is /usr/local/virtualenvs/servicesite

My django project path is /home/addohm/projects/rtservice/servicesite this is where manage.py resides, which leaves /home/addohm/projects/rtservice/servicesite/servicesite as the location of my wsgi.py.

wsgi.py:

SERVICESITE = ['/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages']

import os
import sys
import site

prev_sys_path = list(sys.path)

for directory in SERVICESITE
        site.addsitedir(directory)

new_sys_path = []
for item in list(sys.path):
        if item not in prev_sys_path:
                new_sys_path.append(item)
                sys.path.remove(item)
sys.path[:0] = new_sys_path

"""  **Doesn't seem to work, throwing error in apache logs**
site.addsitedir('/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages')
"""

from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
DJANGO_PATH =  os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)

apache2.conf

[...]

WSGIDaemonProcess servicesite python-path=/home/addohm/projects/rtservice/servicesite:/usr/local/virtualenvs/servicesite/lib/python3.5/site-packages
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py

Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
        Require all granted
</Directory>

<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
        <Files wsgy.py>
                Require all granted
        </Files>
</Directory>

[...]

Graham Dumpleton

You should not have need to change anything in the original wsgi.py generated by Django for you. It is generally sufficient to have:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()

Your Apache configuration should then preferably be:

WSGIDaemonProcess service site python-home=/usr/local/virtualenvs/servicesite \
    python-path=/home/addohm/projects/rtservice/servicesite
WSGIProcessGroup servicesite
WSGIScriptAlias / /home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py

Alias /static/ /home/addohm/projects/rtservice/servicesite/static/
<Directory /home/addohm/projects/rtservice/servicesite/static/>
        Require all granted
</Directory>

<Directory /home/addohm/projects/rtservice/servicesite/servicesite>
        <Files wsgy.py>
                Require all granted
        </Files>
</Directory>

That is, use python-home for the location of the directory specified by sys.prefix for the virtual environment. Avoid using python-path and referring to the site-packages directory. Using python-home has been preferred way for a very long time and using it ensures that things fail in a more obvious way when you don't do things the correct way.

A few very important things.

The first is that mod_wsgi must be compiled for the specific Python major/minor version you want to use.

Second, the Python virtual environment must be created from the same Python installation as mod_wsgi was compiled for. You can't have mod_wsgi compiled against a system Python installation, but have your virtual environment based off a separate Python installation for same major/minor version in /usr/local.

Third, the user that Apache runs your code as must have read access to any directories/files for the main Python installation, the virtual environment and your application code. When you stick stuff under a home directory, it will not generally have access as the home directory prohibits others from reading anything in the home directory.

Fourth, if the mod_wsgi daemon process group is set to run as a different user than the Apache user, the Apache user still must at least have ability to read the wsgi.py file and all the directories down to that point.

Further reading on virtual environments which is more up to date:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Apache with virtualenv and mod_wsgi : ImportError : No module named 'django'

My virtualenv is not taking into account ? [apache, mod_wsgi and django]

Django Custom Error Handlers in Apache and mod_wsgi

Django, apache and mod_wsgi

Apache, Django and mod_wsgi

multiple django sites with apache & mod_wsgi

Setup Django with apache using mod_wsgi

Django apache configuration with mod_wsgi module

Deploying Django on Apache with mod_wsgi

500 internal server error mod_wsgi apache "importerror: No Module named 'django'

500 Internal Server Error django + mod_wsgi + apache2 on ubuntu 16

Getting random 500 error with Apache2 + mod_wsgi + django 'Remote end closed connection without response'

Asking for advice on Django deployment settings with Apache and mod_wsgi

Gateway Timeout Using Django on Apache with mod_wsgi

Django + mod_wsgi + apache2: server hangs

Cannot deploy Official Django tutorial on Apache (mod_wsgi)

Django on Apache with mod_wsgi CSRF Verification Failed

Django on apache wtih mod_wsgi (Linux) - 403 Forbidden

Django - Apache with mod_wsgi not serving static-files

running django python 3.4 on mod_wsgi with apache2

Django + pipenv + Apache + mod_wsgi deployment on ubuntu not loading modules

Apache + mod_wsgi + django: send a default response for a backlogged request

Can not find CSS file path in Django on Apache with mod_wsgi

server internal error with mod_wsgi and apache2

Error occurred when installing mod_wsgi on apache2.4

Apache server mod_wsgi in Redhat gives 500 error

Django https ssl configuration error related to mod_wsgi

Internal 500 error with Django: trying to set up mod_wsgi

Django MOD_WSGI Settings ? MOD_WSGI Apache2 403 You don't have permission to access this resource