简体   繁体   中英

Flask and mod_wsgi import error

I'm trying to setup a flask app through apache on Ubuntu 16.04 using mod_wsgi. What's going on is that the wsgi script is unable to import any python modules.

I've verified the wsgi script is executable and have run it standalone with python. I've also verified my flask app can be run on it's own without errors. I'm able to import all modules in any other python program I run. I've also installed flask with pip and from source. Nothing seems to be working.

Here is my error log:

[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

Here is my flaskapp.wsgi script

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

from flaskapp import app as application

Here is my flask program:

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 contains flaskapp.py and flaskapp.wsgi

Anything I'm missing? Thanks!

Edit: Here is my /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

I was dealing with this today. If you're on Apache 2.4, it's a simple change from this:

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

To:

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

More info here: http://flask.pocoo.org/docs/1.0/deploying/mod_wsgi/#configuring-apache

Also, not sure if it makes a difference but my directories are full paths in the apache config:

    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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM