Apache + Flask无法识别wsgi文件

时间:2017-01-19 03:32:15

标签: python apache flask centos7

运行Anaconda python不是CentOS 7附带的全局python,我已经在本地安装并测试了Flask,它运行正常。

[idf@node3 FlaskApp]$ python __init__.py
[idf@node3 FlaskApp]$ curl localhost:5000
Hello, I love Flask!

当我尝试在apache下运行它时,如下所示,我在最后得到错误。我认为问题是apache试图运行全局python而不是Anaconda python,这是我安装Flask的地方。如果是的话,我不想弄乱系统用于全局python的路径,因为它可能会导致问题。在这种情况下,不知道如何告诉系统使用Anaconda python:

[idf@node3 FlaskApp]$ which python
~/anaconda2/bin/python
[idf@node3 FlaskApp]$

我的申请如下:

档案:hello.py

[idf@node3 FlaskApp]$ more hello.py
import sys
import logging
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
[idf@node3 FlaskApp]$ 

档案:__init__.py

[idf@node3 FlaskApp]$ more __init__.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, I love Flask!"
if __name__ == "__main__":
    app.run()
[idf@node3 FlaskApp]$ 

我的目录结构如下。

[idf@node3 FlaskApp]$ pwd
/var/www/FlaskApp
[idf@node3 FlaskApp]$ ls -la
total 24
drwxr-xr-x 3 apache apache 4096 Jan 19 03:21 .
drwxr-xr-x 6 root   root   4096 Jan 19 02:00 ..
drwxr-xr-x 4 apache apache 4096 Jan 19 02:36 FlaskApp
-rwxr-xr-x 1 apache apache  164 Jan 19 02:32 hello.conf
-rwxr-xr-x 1 apache apache  107 Jan 19 03:21 hello.py
-rwxr-xr-x 1 apache apache  164 Jan 19 02:25 hello.wsgi
[idf@node3 FlaskApp]$ cd FlaskApp/
[idf@node3 FlaskApp]$ ls -la
total 24
drwxr-xr-x 4 apache apache 4096 Jan 19 02:36 .
drwxr-xr-x 3 apache apache 4096 Jan 19 03:21 ..
-rw-r--r-- 1 apache apache  150 Jan 19 02:35 __init__.py
-rw-r--r-- 1 apache apache  449 Jan 19 02:36 __init__.pyc
drwxr-xr-x 2 apache apache 4096 Jan 19 02:01 static
drwxr-xr-x 2 apache apache 4096 Jan 19 02:01 templates
[idf@node3 FlaskApp]$ 

我的apache .conf文件

<VirtualHost *:80>
    ServerName www.mywebsite.com
    ServerAlias mywebsite-ed.com
    ServerAdmin me@mywebsite.com
    WSGIScriptAlias / /var/www/FlaskApp/hello.py
    <Directory /var/www/FlaskApp/FlaskApp/>
        Require all granted
    </Directory>
    Alias /static /var/www/FlaskApp/FlaskApp/static
    <Directory /var/www/FlaskApp/FlaskApp/static/>
        Require all granted
    </Directory>
    ErrorLog /var/log/httpd/error_log
    LogLevel warn
    CustomLog /var/log/httpd/access.log combined
</VirtualHost>

我收到此错误:

[idf@node3 FlaskApp]$ sudo tail -f /var/log/httpd/error_log
[sudo] password for idf: 
[Thu Jan 19 03:23:46.918092 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062] mod_wsgi (pid=4305): Target WSGI script '/var/www/FlaskApp/hello.py' cannot be loaded as Python module.
[Thu Jan 19 03:23:46.918161 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062] mod_wsgi (pid=4305): Exception occurred processing WSGI script '/var/www/FlaskApp/hello.py'.
[Thu Jan 19 03:23:46.918225 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062] Traceback (most recent call last):
[Thu Jan 19 03:23:46.918291 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062]   File "/var/www/FlaskApp/hello.py", line 5, in <module>
[Thu Jan 19 03:23:46.918546 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062]     from FlaskApp import app as application
[Thu Jan 19 03:23:46.918595 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Thu Jan 19 03:23:46.918671 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062]     from flask import Flask
[Thu Jan 19 03:23:46.918734 2017] [:error] [pid 4305] [client xx.xxx.xx.xxx:54062] ImportError: No module named flask

1 个答案:

答案 0 :(得分:0)

卸载系统mod_wsgi包,然后针对Anaconda Python从源代码编译mod_wsgi。建议您使用以下所示的方法:

通过它a?a?a?a?aaaa进入Anaconda Python环境并使用:

aaaa

输出应添加到Apache配置的行以加载mod_wsgi模块并确保正确找到Anaconda Python发行版。