【问题标题】:Python/Flask app using apache/fastcgi producing 500 Error使用 apache/fastcgi 的 Python/Flask 应用程序产生 500 错误
【发布时间】:2015-02-17 21:18:53
【问题描述】:

我一直在做 Miguel Grinberg 的 Flask 微博教程,并且一直在尝试部署到我的 linux VPS。(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi)

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

设置:

我正在使用 Python 2.6.6 和 Apache 2.2.15 运行全新的 CentOS 6.6 安装。我正在使用 sqlite 运行它。如果您有兴趣,我正在使用这些烧瓶模块:http://pastebin.com/bPnH83bs

基本应用结构:(为简洁起见)

位于:/home/apps/portal

我将整个目录改成: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 conf 文件设置:

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>


runp-sqlite.fcgi 内容:

#!flask/bin/python
from flipflop import WSGIServer
from app import app

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


尝试访问页面时 apache 日志出错并收到 500 错误:

[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


我从控制台运行“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


我检查过的内容:

  • 我已禁用 SELinux,因为它导致了另一个问题,但没有解决此问题。
  • 检查文件夹是否已分配给正确的用户/组。
  • 检查 '/etc/httpd/conf/httpd.conf' 和 '/etc/sysconfig/httpd' PIDFILE 位置是否正确
  • 检查 iptables 是否接受到端口 80 和 5000 的流量(用于测试)。如果我删除我添加的 apache 配置,我将成功地从 /var/www/html
  • 提供服务
  • 很多很多的谷歌搜索和玩耍

对不起,文字墙,我只是不知道你需要看到什么。如果有人可以提供帮助,我将非常感激。如果这是愚蠢的事情,我道歉。 :)

更新 1:

将 runp-sqlite.fcgi 改为调用 virtualenv:

#!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()

现在 apache errors_log 有一个新的错误消息:

[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

【问题讨论】:

  • 您好,这个问题解决了吗?如果是,请发布答案! (我现在面临同样的问题......)
  • @SunQingyao 对不起伙计这是太久以前我不记得我是如何解决的或原因是什么。我现在使用 nginx 完成所有工作,它往往比 apache 更顺畅,尤其是对于像这样的小型项目。
  • 感谢您的回复,将尝试 Nginx!

标签: python apache flask mod-wsgi fastcgi


【解决方案1】:

你有 Flask 安装在一个 denderlenv 中吗?如果是这样,问题可能是您的 WSGI 文件没有激活它。当 Apache 尝试为该站点提供服务时,这会导致 ImportError 或类似的东西,从而导致一个不太有用的 500 错误。

修复方法是在导入应用程序之前激活 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 问题:Running Python from a virtualenv with Apache/mod_wsgi, on Windows

【讨论】:

  • 感谢摩西,你是一个传奇人物,我离我更近了一步。我仍然收到 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 -- 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 &lt;br&gt; [Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi
  • 该错误可能意味着在 Apache 的上下文中运行时您的应用程序中仍然存在错误,但调试起来可能会非常棘手。作为第一步,我建议用简单的“Hello,World!”替换所有应用程序代码。应用程序优先,并确保通过 Apache/FastCGI 正确提供服务。
  • 好心的摩西,接下来我会这么做。感谢朋友的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 2015-07-20
相关资源
最近更新 更多