mod_wsgi: Unable to stat Python home and ImportError: No module named 'encodings'
<p>&nbsp;I am trying to construct a web site on Apache 2.4 using Django and mod_wsgi on Python 3.5.2. But when apache daemon httpd is started, following error message is output on /var/log/httpd/error_log.</p>

<blockquote>
  <p>[Fri Sep 16 17:44:57.145900 2016] [wsgi:warn] [pid 20593] (13)Permission denied: mod_wsgi (pid=20593): Unable to stat Python home /home/ec2-user/.pyenv/versions/3.5.2. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
  <br />Fatal Python error: Py_Initialize: Unable to get the locale encoding
  <br />ImportError: No module named 'encodings'</p>
</blockquote>

<p>&nbsp;So, I explored some articles about similar problems, e.g.</p>

<ul>
<li><p><a href="http://stackoverflow.com/questions/9386611/django-apache-mod-wsgi-permission-denied">Django + Apache + mod_wsgi permission denied</a></p></li>
<li><p><a href="http://stackoverflow.com/questions/24495348/mod-wsgi-importerror-no-module-named-encodings">mod_wsgi: ImportError: No module named &#39;encodings&#39;</a></p></li>
</ul>

<p>but the causes of error message above are not yet resolved.
<br />&nbsp;Please indicate some points to be checked or documents to read and so on.</p>

<p>My development environments are as follows.</p>

<p><strong>(1) Host and OS</strong><br /></p>

<ul>
<li>Hosting Service: Amazon AWS EC2 </li>
<li>OS: Amazon Linux AMI release 2016.03</li>
</ul>

<p><strong>(2) Django Project</strong> <br /></p>

<p>&nbsp;I made Django project named <strong>testprj</strong> in the directory <strong>/home/ec2-user/django-sites</strong>  with user account of <strong>ec2-user</strong>.</p>

<pre><code>[ec2-user@MyEC2 django-sites]$ pwd
/home/ec2-user/django-sites
[ec2-user@MyEC2 django-sites]$ ls -l
total 4
drwxrwxr-x 3 ec2-user ec2-user 4096 Sep 16 14:50 testprj
</code></pre>

<p>Database which the testprj uses is already set up. So, development server provided in Django is successfully started with no error as bellow.</p>

<pre><code>[ec2-user@MyEC2 testprj]$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
September 16, 2016 - 14:23:47
Django version 1.10.1, using settings 'testprj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
</code></pre>

<p><strong>(3) Environments of Python</strong></p>

<p>&nbsp;I installed Python 3.5.2 by <code>pyenv install 3.5.2</code>. And then I set 
<code>pyenv virtualenv 3.5.2 django-sites</code>.
And I set local of /home/ec2-user/django-sites to env <strong>django-sites</strong> as bellow.</p>

<pre><code>[ec2-user@MyEC2 django-sites]$ pwd
/home/ec2-user/django-sites
[ec2-user@MyEC2 django-sites]$ pyenv versions
  system
  3.5.2
  3.5.2/envs/django-sites
* django-sites (set by /home/ec2-user/django-sites/.python-version)
[ec2-user@MyEC2 django-sites]$ python -V
Python 3.5.2
</code></pre>

<p>&nbsp;And I installed following modules through pip command.</p>

<pre><code>[ec2-user@MyEC2 django-sites]$ pwd
 /home/ec2-user/django-sites
[ec2-user@MyEC2 django-sites]$ pip list
configparser (3.5.0)
Django (1.10.1)
mod-wsgi (4.5.7)
pip (8.1.2)
PyMySQL (0.7.9)
setuptools (27.2.0)
</code></pre>

<p><strong>(4) Web server</strong></p>

<p>&nbsp;Web server which I use is Apache 2.4 as bellow.</p>

<pre><code>[ec2-user@MyEC2 ~]$ httpd -v
Server version: Apache/2.4.23 (Amazon)
Server built:   Jul 29 2016 21:42:17
</code></pre>

<p>&nbsp;User and Group of executing Apache are both <strong>apache</strong> as next lines in /etc/httpd/conf/httpd.conf.</p>

<pre><code>User apache
Group apache 
</code></pre>

<p><strong>(5) Additinal conf file for the Django project testprj</strong></p>

<p>&nbsp;I intend to configure such that the URL</p>

<pre><code>http://[MyEC2 domain]/testprj/
</code></pre>

<p>can access a top page of Django project located at /home/ec2-user/django-sites/testprj.<br />
&nbsp;[MyEC2 domain] above is like </p>

<pre><code>ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
</code></pre>

<p>So, I wrote <strong>testprj.conf</strong> in  <strong>/etc/httpd/conf.d</strong> as bellow.</p>

<pre><code>[ec2-user@MyEC2 conf.d]$ pwd
/etc/httpd/conf.d
[ec2-user@MyEC2 conf.d]$ cat -n testprj.conf 
</code></pre>

<pre class="lang-bsh prettyprint-override"><code> 1  LoadModule wsgi_module /home/ec2-user/.pyenv/versions/django-sites/lib/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so
 2  
 3  WSGIPythonHome /home/ec2-user/.pyenv/versions/3.5.2
 4  WSGIScriptReloading On
 5  WSGIScriptAlias  /testprj/ /home/ec2-user/django-sites/testprj/testprj/wsgi.py
 6  WSGIPythonPath /home/ec2-user/django-sites/testprj:/home/ec2-user/.pyenv/versions/django-sites/lib/python3.5/site-packages
 7  
 8  &lt;VirtualHost *:80&gt;
 9  
10    ServerName http://ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com
11    SuexecUserGroup apache apache
12  
13    &lt;Directory /home/ec2-user/django-sites/testprj/testprj&gt;
14      &lt;Files wsgi.py&gt;
15      Require all granted
16      &lt;/Files&gt;
17    &lt;/Directory&gt;
18  
19    Alias /static/ "/home/ec2-user/django-sites/testprj/static/"
20  
21    &lt;Directory /home/ec2-user/django-sites/testprj/static&gt;
22      &lt;Files *&gt;
23      Require all granted
24      &lt;/Files&gt;
25    &lt;/Directory&gt;
26  
27  &lt;/VirtualHost&gt;
28  
</code></pre>

<pre class="lang-none prettyprint-override"><code>[ec2-user@MyEC2 conf.d]$
</code></pre>

<p>Best regards.</p>

<p>A couple of things to check.</p>

<ul>
<li><p>The pyenv tool doesn't install Python with a shared library by default. That could result in problems as mod_wsgi wants a shared library. You need to explicitly tell pyenv to build Python with a shared library.</p></li>
<li><p>A home directory on many Linux systems is not readable to other users. When mod_wsgi is being initialised, it is running as the Apache user and will not be able to see inside of the home directory. There isn't an issue when the mod_wsgi module is loaded by Apache as Apache is running as root at that point.</p></li>
</ul>

<p>There are other issues with your configuration where don't follow best practices, but the above, especially the second item is likely the cause of your problem.</p>

python django apache mod-wsgi