【问题标题】:flask: The requested URL was not found on this serverflask:在此服务器上找不到请求的 URL
【发布时间】:2016-11-18 15:52:41
【问题描述】:

我正在使用一个使用 apache 2.4 的共享主机帐户,尝试使用 http://fgimian.github.io/blog/2014/02/14/serving-a-python-flask-website-on-hostmonster 部署一个烧瓶应用程序。我已经把代码和 fcgi 脚本放在 public_html 文件夹中了,文件夹的内容在上面的截图中:

manage_apache.fcgi 脚本是:

#!/home/username/anaconda2/bin/python
import sys,os
from flup.server.fcgi import WSGIServer
sys.path.insert(0, '/home/username/public_html')
from myflaskapp.settings import Config, SharedConfig
from myflaskapp.app import create_app

if __name__ == '__main__':
    app = create_app(SharedConfig)
    WSGIServer(app).run()

我已经到了最后一步,并在命令行中使用 putty to SSH in 进行测试:

[~/public_html]# ./manage_apache.fcgi

我可以看到正在生成的正确网页,所以我假设我的主机支持快速 cgi。我没有收到任何 python 错误。

文章中的.htaccess文件是:

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ manage_apache.fcgi/$1 [QSA,L]

在浏览器中当我浏览 mysite.org 时,我得到了

Not Found

The requested URL /manage_apache.fcgi/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

根据支持 .htaccess 文件正在重定向到 manage_apache.fcgi/$1

-rwxr-xr-x 1 myusername myusername Nov 22 17:26 manage_apache.fcgi*

我该如何解决这个问题?

【问题讨论】:

    标签: python apache .htaccess flask


    【解决方案1】:

    我怀疑

    sys.path.insert(0, '/home/username/public_html')
    

    是一个绝对路径,但是烧瓶应用程序正在寻找一个相对于烧瓶网关的相对路径,但找不到它。

    您是否尝试将库包装在应用实例中 - 移动应用实例中的绝对路径?

    例如,请参阅http://werkzeug.pocoo.org/: from werkzeug.wrappers 导入请求,响应

    @Request.application
    def application(request):
        return Response('Hello World!')
    
    if __name__ == '__main__':
        from werkzeug.serving import run_simple
        # move absolute path here
        run_simple('localhost', 4000, application)
    

    【讨论】:

    • 感谢您查看此内容。 '# move absolute path here' 是什么意思?这是否意味着 sys.path.insert(0, '/home/username/public_html')
    • 这是一个取自 WSGI 包装器的模型,你应该适应它。尝试在模块应用程序本身中导入路径和 wsgi 模块。另一种方法是使用烧瓶模板,它允许您在“公共”静态文件夹中提供文件 - 相对于您的应用程序的路径是公共的。此链接显示了如何从文件系统加载器提供文件:buxty.com/b/2012/05/custom-template-folders-with-flask 还请查看:flask.pocoo.org/docs/0.11/tutorial/foldersstackoverflow.com/a/28758723/305883 可能会帮助您处理逻辑。抱歉,我的笔记本电脑现在处于维护状态,我不能做更多事情:/
    【解决方案2】:

    我怀疑该主机不支持fcgi。仅仅因为主机允许您在命令行上运行 Python 脚本并不意味着他们已经在 Apache 中配置了 mod_fcgi。

    试试这个:apachectl -t -D DUMP_MODULES | grep cgi。您应该看到fcgi_modulefastcgi_module,或者可能是cgi_module

    如果您只看到cgi_module,那么您应该可以使用AddHandler cgi-script .py 而不是AddHandler fcgid-script .fcgi

    如果您没有看到这些,那么您可以尝试wsgiapachectl -t -D DUMP_MODULES | grep wsgi。如果你看到wsgi_module,那么你知道你可以使用wsgi。那时,you might be able to follow instructions here under .htaccess

    【讨论】:

    • 感谢您的回答。我试过:' apachectl -t -D DUMP_MODULES' 并得到 -bash: apachectl: command not found。我可以请支持人员为我运行此程序,但是否有其他方法可以获取此信息?
    • 您最好让支持人员确认您使用的是 Apache 并告诉您是否启用了 CGI 或 FCGI。
    • 他们说CGI和FCGI都没有启用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2010-10-16
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多