Run mod_wsgi with virtualenv or Python with versio

2020-01-29 07:30发布

I am trying to make my Flask application work on CentOS server. Basically the issue is that I have Python 2.6 installed in /usr/bin which is system default and Python 3.4 installed in /usr/local/bin. I would like to use Python 3.4 virtualenv or at least Python 3.4 interpreter for mod_wsgi to run my application.

I have created virtualenv in ~/virtualenvs/flask.

I have this WSGI script:

import os
import sys
from logging import Formatter, FileHandler

APP_HOME = r"/home/fenikso/Album"


activate_this = os.path.join("/home/fenikso/virtualenvs/flask/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

sys.path.insert(0, APP_HOME)
os.chdir(APP_HOME)

from app import app

handler = FileHandler("app.log")
handler.setFormatter(Formatter("[%(asctime)s | %(levelname)s] %(message)s"))
app.logger.addHandler(handler)
application = app

And following config in Apache:

<VirtualHost *:80>
        ServerName album2.site.cz
        Alias /static "/home/fenikso/Album/static"
        Alias /photos "/home/fenikso/Album/photos"
        Alias /thumbs "/home/fenikso/Album/thumbs"
        WSGIScriptAlias / "/home/fenikso/Album/wsgi.py"
        <Directory "/home/fenikso/Album">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/static">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/photos">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/thumbs">
            AllowOverride None
            Allow from all
        </Directory>
</VirtualHost>

However, when trying to run the application, I get an error:

Apache/2.2.15 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.6 mod_fcgid/2.3.7 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips SVN/1.6.11 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
mod_wsgi (pid=14627): Target WSGI script '/home/fenikso/Album/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=14627): Exception occurred processing WSGI script '/home/fenikso/Album/wsgi.py'.
Traceback (most recent call last):
   File "/home/fenikso/Album/wsgi.py", line 15, in <module>
     from app import app
   File "/home/fenikso/Album/app.py", line 1, in <module>
     from flask import Flask
 ImportError: No module named flask

I have noticed that either Python 2.6 is being ran and my virtualenv is not activated. What would be the proper way to get this working and still have the Python 2.6 as a system default?

2条回答
放我归山
2楼-- · 2020-01-29 08:14

Look into the WSGIPythonHome and WSGIPythonPath directives. It's also possible that you have a python2.6 mod_wsgi installed, mod_wsgi must be compiled for the intended python version and does not support multiple python versions. So check that your mod_wsgi is py3.4 compatible and set the directives above.

Alternatively, you could run the flask app with a python server like gunicorn and proxypass from apache to gunicorn.

查看更多
\"骚年 ilove
3楼-- · 2020-01-29 08:19

You have to add the following line in your apache.conf in order to give the right executable and the path to the virtualenv.

WSGIPythonHome /usr/local/bin
WSGIPythonPath /home/fenikso/virtualenv/lib/python3.4/site-packages

You will find all the options of these two command in the mod_wsgi documentation

Be aware that you must have the version of mod_wsgi compatible with the python executable. In your case, you probably have to install mod_wsgi3.4 and configure apache to use it instead of the standart mod_wsgi module.

The whole configuration file should be :

WSGIPythonHome "/usr/local/bin"
WSGIPythonPath "/home/fenikso/virtualenv/lib/python3.4/site-packages"

<VirtualHost *:80>
        ServerName album2.site.cz
        Alias /static "/home/fenikso/Album/static"
        Alias /photos "/home/fenikso/Album/photos"
        Alias /thumbs "/home/fenikso/Album/thumbs"
        WSGIScriptAlias / "/home/fenikso/Album/wsgi.py"
        <Directory "/home/fenikso/Album">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/static">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/photos">
            AllowOverride None
            Allow from all
        </Directory>
        <Directory "/home/fenikso/Album/thumbs">
            AllowOverride None
            Allow from all
        </Directory>
</VirtualHost>
查看更多
登录 后发表回答