apache mod_wsgi error with django in virtualenv
<p>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...</p>

<p><a href="http://stackoverflow.com/questions/38284814/django-apache-configuration-with-wsgidaemonprocess-not-working">django apache configuration with WSGIDaemonProcess not working</a></p>

<p>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?  </p>

<p>The root problem:</p>

<pre><code> Call to 'site.addsitedir()' failed for '(null)'
</code></pre>

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

<p>My virtual environment path is <code>/usr/local/virtualenvs/servicesite</code></p>

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

<p><strong>wsgi.py:</strong></p>

<pre><code>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)
</code></pre>

<p><strong>apache2.conf</strong></p>

<p>[...]</p>

<pre><code>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/
&lt;Directory /home/addohm/projects/rtservice/servicesite/static/&gt;
        Require all granted
&lt;/Directory&gt;

&lt;Directory /home/addohm/projects/rtservice/servicesite/servicesite&gt;
        &lt;Files wsgy.py&gt;
                Require all granted
        &lt;/Files&gt;
&lt;/Directory&gt;
</code></pre>

<p>[...]</p>

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

<pre><code>import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
application = get_wsgi_application()
</code></pre>

<p>Your Apache configuration should then preferably be:</p>

<pre><code>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/
&lt;Directory /home/addohm/projects/rtservice/servicesite/static/&gt;
        Require all granted
&lt;/Directory&gt;

&lt;Directory /home/addohm/projects/rtservice/servicesite/servicesite&gt;
        &lt;Files wsgy.py&gt;
                Require all granted
        &lt;/Files&gt;
&lt;/Directory&gt;
</code></pre>

<p>That is, use <code>python-home</code> for the location of the directory specified by <code>sys.prefix</code> for the virtual environment. Avoid using <code>python-path</code> and referring to the <code>site-packages</code> directory. Using <code>python-home</code> 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.</p>

<p>A few very important things.</p>

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

<p>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 <code>/usr/local</code>.</p>

<p>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.</p>

<p>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 <code>wsgi.py</code> file and all the directories down to that point.</p>

<p>Further reading on virtual environments which is more up to date:</p>

<ul>
<li><a href="http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html" rel="nofollow">http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html</a></li>
</ul>

python django apache ubuntu mod-wsgi