【问题标题】:Error 404 when trying to set up a Bottle-powered web app on Apache/mod_wsgi尝试在 Apache/mod_wsgi 上设置 Bottle-powered Web 应用程序时出现错误 404
【发布时间】:2012-05-28 03:29:08
【问题描述】:

我是一名初级程序员。我开始使用 Python 和 Bottle 来制作一个小型 Web 应用程序来打印表单,到目前为止一切都很好。真正的问题是配置 Apache 和 mod_wsgi,因为我几乎一无所知。

我的问题:我不断收到此错误:

错误 404:未找到

抱歉,请求的 URL /factura/ 导致错误:未找到

在工作中,他们给了我重定向到 IP:port 的地址;经过几天阅读 Apache 文档并通过网络查看示例后,我设法设置了配置,因此我的 VirtualHost 不会破坏其他已经运行的虚拟主机。配置如下所示(基于瓶子教程部署部分):

Listen port
NameVirtualHost IP:port

<VirtualHost IP:port>
    ServerName IP:port

    WSGIDaemonProcess factura processes=1 threads=5
    WSGIScriptAlias / /var/www/factura/app.wsgi

    <Directory /var/www/factura>
        WSGIProcessGroup factura
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

我的app.wsgi 与Bottle 教程-部署部分中的几乎相同。我只添加了sys.stdout = sys.stderr这一行:

import sys, os, bottle

# Change working directory so relative paths (and template lookup) work again
sys.path = ['/var/www/factura'] + sys.path
os.chdir(os.path.dirname(__file__))

# Error output redirect
# Exception KeyError in 'threading' module
sys.stdout =  sys.stderr

import factura
application = bottle.default_app()

这里是一些与 Bottle 相关的 python 代码:

from lib import bottle

app = bottle.Bottle()

#serves files in folder 'static'
@app.route('/static/:path#.+#', name='static')
def ...

@app.route("/factura")
@bottle.view("factura")
def ...

@app.route("/print_factura", method="POST")
def ...

我已经阅读了与此类似的其他一些问题,但我无法看到我错过了什么。我想问题出在app.wsgi

更新

文件结构

/var/www/factura/       ## .py files
                /views  ## here is the web template
                /static ## .css and .js of template
                /lib    ## package with bottle and peewee source files
                /data   ## inkscape file to play with
                /bin    ## backup stuff in repo, not used in code

Apache 错误日志只显示

Exception KeyError: KeyError(-1211426160,) in &lt;module 'threading' from '/usr/lib/python2.6/threading.pyc'&gt; ignored 这是来自 wsgi/python 问题的警告,wsgi issue 197 无害

更新 2 工作
添加了@app.route("/factura/") 注意斜线,随着应用程序导入from factura import app as application 的更改,这两者一起使其工作

【问题讨论】:

    标签: python apache mod-wsgi bottle


    【解决方案1】:

    如果您明确创建应用程序:

    app = bottle.Bottle()
    

    那么你应该把它导入你的app.wsgi而不是application = bottle.default_app()

    from factura import app as application
    

    但重要的是这一点。在您的 WSGI 文件中,您执行 import bottle,但在应用程序代码文件中,您执行 from lib import bottle。正如您所解释的,您有两个 Bottle 副本:一个安装在服务器范围内,另一个安装在 lib 目录下。

    这就是您收到404 Not Found 的原因。您实际上是在使用该库的一个实例(创建 app),然后给 Apache 一个不同的 (default_app)来自该库的另一个实例!

    当您开始返回正确的app 时,它开始正常工作。

    【讨论】:

    • 刚刚注意到但我要求 /factura 在 apache 错误中添加了试用斜线。在静态文件夹中有一个 .css 并且我得到相同的错误(斜线似乎具有相同的行为)
    • 在本地主机的瓶子中没有看到该问题测试。当我重新开始工作时会尝试您链接的文档所说的内容,谢谢您的快速回答
    • @mraistlin:我已经更新了我的答案,并提供了更多内容供您检查。 :)
    • 已根据您的要求进行了更新,之后工作:D(现在我只在我的程序中遇到访问被拒绝的问题,但这应该可以通过目录访问轻松修复,并且与此问题无关)。我应该担心添加斜线吗?关于import bottle你问我第一天动手,这位同事说要在服务器上安装带有aptitue的瓶子,那为什么可以在那里调用它
    • @mraistlin:我已经用更详细的行为解释更新了我的答案。如果您的问题得到解决,请考虑接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2018-09-02
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多