Deploying Python Flask App on Apache with Python v

2019-07-22 14:27发布

问题:

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.

I installed mod_wsgi using yum.

I configured the virtualhost as shown below:

<VirtualHost *:80>
    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
    <Directory /var/www/myapp.myserver.com/html>
            WSGIProcessGroup smex
            WSGIApplicationGroup %{GLOBAL}
            Order deny,allow
            Allow from all
    </Directory>
</VirtualHost>

My wsgi file is configured as shown below:

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

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

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

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:

  1. Do I need to have Python 3.5.1 installed in /usr/local or can I just have it in the virtual environment.
  2. 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?
  3. What else am I missing to get this to work?

Thanks in advance for your help.

回答1:

Check out the Software Collections Library (SCL) at https://www.softwarecollections.org 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.