Python烧瓶& apache上的fast-cgi - 500内部服务器错误(脚本头的过早结束)

时间:2017-09-10 15:39:20

标签: python apache flask webserver fastcgi

我想在apache prodcution服务器上设置我的烧瓶应用程序。我创建了以下.htaccess文件:

<IfModule mod_fcgid.c>
   AddHandler fcgid-script .fcgi
   <Files ~ (\.fcgi)>
       SetHandler fcgid-script
       Options +SymLinksIfOwnerMatch +ExecCGI
   </Files>
</IfModule>

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ /fcgi-bin/runFlaskApp.fcgi/$1 [QSA,L]
</IfModule>

哪个应重定向到以下fcgi文件(设置为chmod 755):

#!/usr/bin/env python2.7
# -*- coding: UTF-8 -*-

RELATIVE_WEB_URL_PATH = '/app_dict'

import os
# This points to the application on the local filesystem.
LOCAL_APPLICATION_PATH = os.path.expanduser('~') + '/html/app_dict'
import sys
sys.path.insert(0, LOCAL_APPLICATION_PATH)

from flup.server.fcgi import WSGIServer
from tmain import app


class ScriptNamePatch(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = RELATIVE_WEB_URL_PATH
        return self.app(environ, start_response)

app = ScriptNamePatch(app)

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

作为回报,应该开始以下烧瓶应用程序:

#!/usr/bin/env python2.7
# -*- coding: utf8 -*-

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

但是当我尝试访问该网站时,会显示内部服务器错误500。以下行显示在apache错误日志中:

[warn] [client 0.0.0.0] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[error] [client 0.0.0.0] Premature end of script headers: runFlaskApp.fcgi

如果我使用“python2.7 runFlaskApp.fcgi”运行fcgi文件,则返回以下内容:

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 12

Hello World!

我相信标题错误是因为它不是在WSGIServer的上下文中运行。

我已经尝试通过添加错误的格式化代码行来激发烧瓶应用中的异常。但是在apache错误日志中没有出现任何异常。这让我相信烧瓶应用程序甚至不会被fcgi脚本加载。

有没有办法进一步调试,或者有人知道这个问题的解决方案吗?

1 个答案:

答案 0 :(得分:0)

问题在于fcgi文件的权限或行结尾。将它从Windows上传到服务器并设置chmod 755不起作用。 只在服务器上创建文件然后运行chmod 755对我有效。