当前位置: 首页 > 面试题库 >

使用Apache和Mod_wsgi的Flask Hello World仅在Webroot中显示文件

袁增
2023-03-14
问题内容

我正在尝试使用wsgi从Flask站点通过apache2运行基本的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

from hello import app as application

import sys
sys.stdout = sys.stderr

/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

127.0.0.1    example.com

在运行sudo a2ensite flask_dev和之后sudo service apache2 reload (or restart)www.example.com仅提供webroot中的文件。 我已经检查了错误日志,看起来好像是mod_wsgi和mod_python正在启动。有人知道我还想念什么吗?

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

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

错误日志

[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

编辑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/

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

有人以前在WSGI应用程序中遇到过这种错误,并且知道解决方法吗?

提前致谢。

编辑3 所有源代码现在都可以使用


问题答案:

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

尝试使用

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

检查此页面。



 类似资料:
  • 问题内容: 我已经安装了Apache服务器,并且正在通过mod_wsgi处理Flask响应。我已经通过别名注册了WSGI脚本: [httpd.conf] 我在上面的路径中添加了相应的WSGI文件: [/mnt/www/wsgi-scripts/service.wsgi] 我有一个简单的Flask Python测试脚本,提供了服务模块: [/mnt/www/wsgi-scripts/service.

  • 问题内容: 中大型Python WSGI应用程序,Apache + mod_wsgi或Nginx + mod_wsgi使用什么? 哪种组合需要更多的内存和CPU时间? 哪一个更快? 哪一个比另一个稳定? 我也在考虑使用CherryPy的WSGI服务器,但听说它不太适合负载非常大的应用程序,您对此有什么了解? 注意 :我没有使用任何Python Web Framework,只是从头开始编写了整本书。

  • 我正在尝试部署我构建的用于部署在Apache服务器上的Flask Web应用程序。我在Raspberry Pi 3上使用Raspbian(Jessie)OS。该应用程序在flask内置的开发Web服务器上运行完美,但我无法在Apache服务器上部署它。 这就是我所做的: conf文件为:etc/apach2/sites available/arduinoweb。形态: 中的WSGI脚本文件: Ap

  • 我使用dropzone作为CSV/XLS文件上载器。我使用此选项筛选和限制CSV/XLS文件: 您有什么方法可以同时查看这两个XLS/CSV文件吗?

  • 如果你使用 Apache web 服务器,请考虑使用 mod_wsgi 。 注意 请确保在任何 app.run() 调用之前,你应该把应用文件放在一个 if __name__ == `__main__`: 块中或移动到独立的文件。只确保它没被调用是 因为这总是会启动一个本地的 WSGI 服务器,而当我们使用 mod_wsgi 部署应用 时并不想让它出现。 安装 mod_wsgi 如果你还没有安装过

  • If you are using the Apache webserver you should consider using mod_wsgi. Installing mod_wsgi If you don’t have mod_wsgi installed yet you have to either install it using a package manager or compile