Deploying Python Flask App on Apache with Python version installed in Virtual Environment Only
<p>I am working on a CentOS7 development environment. The machine came with Python 2.7.5 pre-installed. I have developed a web application using Python 3.5.1 which along with it's dependencies was installed in the virtual environment only. Python 3 is not installed machine-wide. I am now trying to deploy the application on an Apache server but have run into trouble. Here is what I have done.</p>

<p>I installed mod_wsgi using yum.</p>

<p>I configured the virtualhost as shown below:</p>

<pre><code>&lt;VirtualHost *:80&gt;
    ServerName myapp.myserver.com

    WSGIDaemonProcess myapp user=myuser group=mygroup threads=5 python-path=/var/www/myapp.myserver.com/html:/var/www/myapp.myserver.com/venv/lib:/var/www/myapp.myserver.com/venv/lib/python3.5/site-packages python-home=/var/www/myapp.myserver.com/html/venv
    WSGIScriptAlias / /var/www/myapp.myserver.com/html/myapp.wsgi
    &lt;Directory /var/www/myapp.myserver.com/html&gt;
            WSGIProcessGroup smex
            WSGIApplicationGroup %{GLOBAL}
            Order deny,allow
            Allow from all
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</code></pre>

<p>My wsgi file is configured as shown below:</p>

<pre><code>import sys
sys.path.insert(0, '/var/www/myapp.myserver.com/html')

activate_this = '/var/www/myapp.myserver.com/html/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

from myapp import app as application
</code></pre>

<p>However, I am getting an internal server error when I try to open the site. The error log reveals the following:</p>

<pre><code>Tue Oct 18 14:24:50.174740 2016] [mpm_prefork:notice] [pid 27810] AH00163: Apache/2.4.6 (CentOS) PHP/5.4.16 mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Tue Oct 18 14:24:50.174784 2016] [core:notice] [pid 27810] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
</code></pre>

<p>The last error keeps repeating for most of the log file. The first thing that catches my eye is the Python version which seems to be 2.7.5. This brings me to my questions:</p>

<ol>
<li>Do I need to have Python 3.5.1 installed in /usr/local or can I just have it in the virtual environment.</li>
<li>Do I need to install a specific mod_wsgi version for this version of Python? If so should I install it via pip instead of yum?</li>
<li>What else am I missing to get this to work?</li>
</ol>

<p>Thanks in advance for your help.</p>

<p>Check out the Software Collections Library (SCL) at <a href="https://www.softwarecollections.org" rel="nofollow">https://www.softwarecollections.org</a> for CentOS/RHEL, don't use any default system Python, Apache or mod_wsgi packages. The SCL provides newer versions of Python and Apache than the default system versions. Then build mod_wsgi from source code against the SCL versions of Python and Apache. You cannot force mod_wsgi to use a Python virtual environment for a different version of Python than what mod_wsgi was compiled for.</p>

python apache flask mod-wsgi python-3.5