apache mod_wsgi error with django in virtualenv

瘦欲@ 提交于 2019-12-05 14:00:13

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!