【问题标题】:Running CherryPy under mod_wsgi在 mod_wsgi 下运行 CherryPy
【发布时间】:2012-12-03 05:02:58
【问题描述】:

我正在尝试让 CherryPy 应用程序在 ubuntu 上的 apache2/mod_wsgi 下运行。我正在遵循here 概述的教程,我的配置几乎相同。访问站点的根目录时,我收到 500 Internal Server Error。日志中唯一的错误是:

[Mon Dec 03 04:43:06 2012] [error] [client 64.189.251.239] Premature end of script headers: index.py

我尝试了本教程的多种变体,但没有收到任何重大错误。有什么想法吗?

我的阿帕奇VirtualHost:

...
    WSGIScriptAlias / /var/www/example.com/uba/index.py
DocumentRoot /var/www/example.com/uba/
<Directory />
    Options +ExecCGI Indexes FollowSymLinks
    AllowOverride All
</Directory>

<Directory /var/www/example.com/uba/>
    Options +ExecCGI -Indexes -FollowSymLinks -MultiViews
    WSGIApplicationGroup %{GLOBAL}
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
...

我的 index.py 脚本:

#!/usr/bin/python

import sys
sys.stdout = sys.stderr

import atexit
import threading
import cherrypy

cherrypy.config.update({'environment': 'embedded'})

if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
    cherrypy.engine.start(blocking=False)
    atexit.register(cherrypy.engine.stop)

class Root(object):
    def index(self):
        return 'Hello World!'
    index.exposed = True

application = cherrypy.Application(Root(), script_name=None, config=None)

更新 #1:

运行这个非常基本的 wsgi 应用程序会产生完全相同的错误:

#!/usr/bin/python

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

【问题讨论】:

    标签: python ubuntu apache2 mod-wsgi cherrypy


    【解决方案1】:

    我建议你用一个超级简单的 wsgi 应用程序替换你的cherrypy index.py 脚本来测试你的 apache 配置。

    def application(environ, start_response):
        status = '200 OK'
        output = 'Hello World!'
    
        response_headers = [('Content-type', 'text/plain'),
                            ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
    
        return [output]
    

    在尝试使用cherrypy 脚本之前确保它可以正常工作。

    【讨论】:

    • 即使这样也会产生同样的错误。我没想过要尝试这个,因为我在 wsgi 下运行了几个 django 应用程序也没有问题。 apache 配置一定有问题。
    • 哦,我觉得自己很愚蠢。显然在配置中需要AddHandler wsgi-script .py - 我假设WSGIScriptAlias 会绕过它
    • 好吧,我现在只是在猜测,但是您的 index.py 脚本是可执行的吗?也许在应用程序函数中放置一个打印语句,看看它是否被调用。
    • 你不应该需要 AddHandler,除非你可能已经在 Apache 配置的其他地方有一个 AddHandler 映射 .py 到 cgi 脚本。这是 mod_wsgi 提倡使用 .wsgi 扩展名的部分原因,因为它不会与现有的 CGI 定义发生冲突。
    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 2010-09-23
    • 2012-07-11
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多