使用apache/fastcgi的Python/Flask应用程序产生500错误

使用apache/fastcgi的Python/Flask应用程序产生500错误,python,apache,flask,mod-wsgi,fastcgi,Python,Apache,Flask,Mod Wsgi,Fastcgi,我一直在做Miguel Grinberg的Flask微博教程,一直在尝试部署到我的linux VPS。() 我得到的是一个由apache而不是flask产生的500内部服务器错误,我无法理解。当使用解释器运行它时,它可以工作,但是我不能用apache启动它。我读了这么多谷歌搜索和问题,我迷路了。我对linux/python/flask比较陌生,但我愿意了解是否有人能为我指明正确的方向。 设置: FcgidIPCDir /tmp AddHandler fcgid-script .fcgi <

我一直在做Miguel Grinberg的Flask微博教程,一直在尝试部署到我的linux VPS。()

我得到的是一个由apache而不是flask产生的500内部服务器错误,我无法理解。当使用解释器运行它时,它可以工作,但是我不能用apache启动它。我读了这么多谷歌搜索和问题,我迷路了。我对linux/python/flask比较陌生,但我愿意了解是否有人能为我指明正确的方向。

设置:

FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
    DocumentRoot /home/apps/portal/app/static
    Alias /static /home/apps/portal/app/static
    ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>
#!flask/bin/python
from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
[root@**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
    File "./runp-sqlite.fcgi", line 6, in <module>
        WSGIServer(app).run()
    File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
        sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
#!flask/bin/python

activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi
我正在使用Python 2.6.6和Apache 2.2.15重新安装CentOS 6.6。我正在用sqlite运行它。如果您感兴趣,我将使用这些烧瓶模块:

应用程序的基本结构:(为简洁起见省略了一些内容)

位于:/home/apps/portal

我有整个目录chown'd to:chown-R apps:apache

用户“apps”是apache组的成员

flask\ (virtualenv)
app\
  static\
  templates\
  __init__.py
  models.py
  views.py
  forms.py
  decorators.py
db_repository\
search.db\
tmp\
app.db
config.py
run.py
runp.py
runp-sqlite.fcgi

Apache配置文件设置:

FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
    DocumentRoot /home/apps/portal/app/static
    Alias /static /home/apps/portal/app/static
    ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>
#!flask/bin/python
from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
[root@**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
    File "./runp-sqlite.fcgi", line 6, in <module>
        WSGIServer(app).run()
    File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
        sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
#!flask/bin/python

activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi

尝试访问页面时,apache日志中出现错误,出现500个错误:

FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
    DocumentRoot /home/apps/portal/app/static
    Alias /static /home/apps/portal/app/static
    ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>
#!flask/bin/python
from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
[root@**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
    File "./runp-sqlite.fcgi", line 6, in <module>
        WSGIServer(app).run()
    File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
        sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
#!flask/bin/python

activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi

从控制台运行“runp sqlite.fcgi”时出错:

FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
    DocumentRoot /home/apps/portal/app/static
    Alias /static /home/apps/portal/app/static
    ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>
#!flask/bin/python
from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
[root@**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
    File "./runp-sqlite.fcgi", line 6, in <module>
        WSGIServer(app).run()
    File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
        sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
#!flask/bin/python

activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi
现在apache错误日志有一条新的错误消息:

FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
    DocumentRoot /home/apps/portal/app/static
    Alias /static /home/apps/portal/app/static
    ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>
#!flask/bin/python
from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi
[root@**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
    File "./runp-sqlite.fcgi", line 6, in <module>
        WSGIServer(app).run()
    File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
        sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket
#!flask/bin/python

activate_this = '/home/apps/portal/flask/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()
[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi

您是否在virtuelenv中安装了烧瓶?如果是这样,问题可能是您的WSGI文件没有激活它。当Apache试图为站点提供服务时,这会导致ImportError或类似事件,这会导致一个没有多大帮助的错误

修复方法是在导入应用程序之前激活WSGI文件中的virtualenv,如下所示:

#!/bin/python

VENV_DIR = 'your_app/your_venv'
activate_this = os.path.join(VENV_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

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

另请参见前一个SO问题:

谢谢摩西,你是一个传奇,我离你更近了一步。我仍然收到500个错误,但现在有一条新消息:
[Fri Dec 19 13:43:03 2014][notice]Apache/2.2.15(Unix)DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured--恢复正常操作[Fri Dec 19 13:43:05 2014][warn][client 110.143.38.80](104)对等方重置连接:mod_fcgid:从FastCGI服务器读取数据时出错
[Fri Dec 19 13:43:05 2014][error][client 110.143.38.80]脚本头过早结束:runp sqlite.fcgi
该错误可能意味着在Apache上下文中运行时应用程序中仍然存在错误,但调试起来可能会非常棘手。作为第一步,我建议首先用一个简单的“Hello,World!”应用程序替换所有的应用程序代码,并确保通过Apache/FastCGI正确地提供服务。思维良好的Moses,我接下来会这样做。谢谢你的帮助,伙计。你好,你解决这个问题了吗?如果是,请发布答案!(我现在也面临同样的问题…@SunQingyao对不起,伙计,这是很久以前的事了,我不记得我是怎么解决的,也不记得原因是什么。我现在用nginx做所有的工作,它比apache更流畅,尤其是像这样的小项目。谢谢你的回复,我会试试nginx!