Python 使用 apache 和 mod_wsgi 的 Flask hello world 仅显示 webroot 中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21084791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:01:40  来源:igfitidea点击:

Flask hello world using apache and mod_wsgi shows files in webroot only

pythonapacheapache2flaskwsgi

提问by David

I'm attempting to run the basic hello.py from the flask site over apache2 using wsgi. Here is what my code looks like:

我正在尝试使用 wsgi 在 apache2 上从烧瓶站点运行基本的 hello.py。这是我的代码的样子:

/var/www/flask_dev/hello.py

/var/www/flask_dev/hello.py

from flask import Flask
app = Flask(__name__)

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

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

/var/www/flask_dev/start.wsgi

/var/www/flask_dev/start.wsgi

from hello import app as application

import sys
sys.stdout = sys.stderr

/etc/apache2/sites-available/flask_dev.conf

/etc/apache2/sites-available/flask_dev.conf

#Listen 80
ServerName example.com

<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
    #ServerName example.com

    WSGIDaemonProcess hello user=<myuser> group=<myusersgroup> threads=5 python-path=/var/www/flask_dev

    WSGIScriptAlias / /var/www/flask_dev/start.wsgi
    <Directory /var/www/flask_dev>
           WSGIProcessGroup hello
           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

/etc/hosts

/etc/hosts

127.0.0.1    example.com

After I run sudo a2ensite flask_devand sudo service apache2 reload (or restart), going to www.example.comjust gives the files in the webroot. ~~I've checked the error log and it looks like mod_wsgi and mod_python are starting up. Does anyone know what else I'm missing?~~

在我运行sudo a2ensite flask_devand 之后sudo service apache2 reload (or restart),将www.example.com只提供 webroot 中的文件。~~我检查了错误日志,看起来 mod_wsgi 和 mod_python 正在启动。有谁知道我还缺少什么?~~

Edit 1I'm now able to see messages from the error log, and it looks like I am affected by https://bugs.launchpad.net/ubuntu/+source/libapache2-mod-python/+bug/1073147on Ubuntu 12.04.

编辑 1我现在可以从错误日志中看到消息,看起来我受到了Ubuntu 上的https://bugs.launchpad.net/ubuntu/+source/libapache2-mod-python/+bug/1073147 的影响12.04。

I will try recompiling later to the correct Python version to see if it fixes the error. For now, while this error occurs, I'm still seeing the files listed when I visit example.com.

稍后我将尝试重新编译为正确的 Python 版本,以查看它是否修复了错误。目前,虽然发生此错误,但我在访问 example.com 时仍会看到列出的文件。

error.log

错误日志

[Mon Jan 13 11:28:06 2014] [notice] caught SIGTERM, shutting down
[Mon Jan 13 11:28:07 2014] [error] python_init: Python version mismatch, expected '2.7.2+', found '2.7.3'.
[Mon Jan 13 11:28:07 2014] [error] python_init: Python executable found '/usr/bin/python'.
[Mon Jan 13 11:28:07 2014] [error] python_init: Python path being used '/usr/lib/python2.7/:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload'.
[Mon Jan 13 11:28:07 2014] [notice] mod_python: Creating 8 session mutexes based on 6 max processes and 25 max threads.
[Mon Jan 13 11:28:07 2014] [notice] mod_python: using mutex_directory /tmp 
[Mon Jan 13 11:28:07 2014] [warn] mod_wsgi: Compiled for Python/2.7.2+.
[Mon Jan 13 11:28:07 2014] [warn] mod_wsgi: Runtime using Python/2.7.3.
[Mon Jan 13 11:28:07 2014] [notice] Apache/2.2.22 (Ubuntu) mod_python/3.3.1 Python/2.7.3 mod_wsgi/3.3 configured -- resuming normal operations

Edit 2Updating to 13.04 has solved the version info. Now when I go to example.com I get a 403 forbidden error. When I tail the error.log I see:

编辑 2更新到 13.04 已经解决了版本信息。现在,当我访问 example.com 时,出现 403 禁止错误。当我跟踪 error.log 时,我看到:

[Mon Jan 13 21:03:41.464815 2014] [:error] [pid 10999:tid 3014634304] [client 127.0.0.1:35067] Attempt to invoke directory as WSGI application: /var/www/flask_dev/

I've tried adding AddHandler cgi-script pyto my flask_dev.conf, but that doesn't seem to work either.

我试过添加AddHandler cgi-script py到我的flask_dev.conf,但这似乎也不起作用。

Has anyone gotten this kind of error with a WSGI application before and know of a workaround for this?

有没有人在使用 WSGI 应用程序之前遇到过这种错误并且知道解决方法?

Thanks in advance.

提前致谢。

Edit 3All source code works now

编辑 3所有源代码现在都有效

采纳答案by Akilesh

in /var/www/flask_dev/hello.wsgi you should be importing app not ass. Second you should not be using DocumentRoot to store your scripts. DocumentRoot is for storing static files and so it will certainly list them as files and never run them as script.

在 /var/www/flask_dev/hello.wsgi 你应该导入应用程序而不是屁股。其次,您不应该使用 DocumentRoot 来存储您的脚本。DocumentRoot 用于存储静态文件,因此它肯定会将它们列为文件,而不会将它们作为脚本运行。

Try using

尝试使用

    WSGIScriptAlias / /var/www/flask_dev/hello.wsgi
    <Directory "/var/www/flask_dev">
       WSGIProcessGroup hello
       WSGIApplicationGroup %{GLOBAL}
       Order deny,allow
       Allow from all
    </Directory>

Check thispage.

检查页面。