Flask和mod_wsgi导入错误

时间:2017-02-01 02:07:15

标签: python apache flask mod-wsgi

我正在尝试使用mod_wsgi在Ubuntu 16.04上通过apache设置一个烧瓶应用程序。发生的事情是wsgi脚本无法导入任何python模块。

我已经验证了wsgi脚本是可执行的,并且已经使用python独立运行它。我还验证了我的烧瓶应用程序可以自己运行而没有错误。我能够导入我运行的任何其他python程序中的所有模块。我还安装了pip和来源的烧瓶。似乎没有什么工作。

这是我的错误日志:

[Wed Feb 01 02:00:39.939582 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] mod_wsgi (pid=6286): Target WSGI script '/var/www/html/flaskapp/flaskapp.wsgi' cannot be loaded as Python module.
[Wed Feb 01 02:00:39.939616 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] mod_wsgi (pid=6286): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'.
[Wed Feb 01 02:00:39.939633 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] Traceback (most recent call last):
[Wed Feb 01 02:00:39.939651 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]   File "/var/www/html/flaskapp/flaskapp.wsgi", line 4, in <module>
[Wed Feb 01 02:00:39.939677 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]     from flaskapp import app as application
[Wed Feb 01 02:00:39.939684 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]   File "/var/www/html/flaskapp/flaskapp.py", line 1, in <module>
[Wed Feb 01 02:00:39.939693 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]     from flask import Flask, json, request, jsonify
[Wed Feb 01 02:00:39.939707 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]   File "/usr/local/lib/python2.7/dist-packages/Flask-0.13.dev0-py2.7.egg/flask/__init__.py", line 21, in <module>
[Wed Feb 01 02:00:39.939716 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]     from .app import Flask, Request, Response
[Wed Feb 01 02:00:39.939720 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]   File "/usr/local/lib/python2.7/dist-packages/Flask-0.13.dev0-py2.7.egg/flask/app.py", line 26, in <module>
[Wed Feb 01 02:00:39.939728 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]     from . import json, cli
[Wed Feb 01 02:00:39.939732 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]   File "/usr/local/lib/python2.7/dist-packages/Flask-0.13.dev0-py2.7.egg/flask/cli.py", line 17, in <module>
[Wed Feb 01 02:00:39.939739 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850]     import click
[Wed Feb 01 02:00:39.939755 2017] [wsgi:error] [pid 6286:tid 140540005824256] [client 73.241.170.36:45850] ImportError: No module named click

这是我的flaskapp.wsgi脚本

import sys
sys.path.insert(0,'/var/www/html/flaskapp')

from flaskapp import app as application

这是我的烧瓶计划:

from flask import Flask, json, request, jsonify
import numpy as np

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

if __name__ == '__main__':
    app.run()

/var/www/html/flaskapp包含flaskapp.py and flaskapp.wsgi

我缺少什么?谢谢!

修改:这是我的/etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    WSGIDaemonProcess flaskapp threads=5
    WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

    <Directory flaskapp>
            WSGIProcessGroup flaskapp
            WSGIApplicationGroup %{GLOBAL}
            Order deny,allow
            Allow from all
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

1 个答案:

答案 0 :(得分:0)

我今天正在处理这个。如果您使用的是Apache 2.4,则是一个简单的更改:

<Directory flaskapp>
        WSGIProcessGroup flaskapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
</Directory>

收件人:

<Directory flaskapp>
        WSGIProcessGroup flaskapp
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
</Directory>

更多信息在这里: http://flask.pocoo.org/docs/1.0/deploying/mod_wsgi/#configuring-apache

另外,不确定是否有所作为,但是我的目录在apache配置中是完整路径:

    Alias /static /var/www/appname/app/static/
    <Directory /var/www/appname/app/>
            Require all granted
    </Directory>
    <Directory /var/www/appname/app/static/>
            Require all granted
    </Directory>