Skip to main content
 首页 » 编程设计

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

2024年10月25日33shangdawei

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

我收到由 apache 而非 flask 产生的 500 内部服务器错误,我无法弄清楚。它在使用解释器运行时有效,但我无法使用 apache 启动它。我已经阅读了很多谷歌搜索和 SO 问题,但我迷路了。我对 linux/python/flask 比较陌生,但如果有人能给我指明正确的方向,我愿意学习。

设置:

我正在使用 Python 2.6.6 和 Apache 2.2.15 运行全新的 CentOS 6.6 安装。我正在用 sqlite 运行它。如果您有兴趣,我正在使用这些 flask 模块: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 配置文件设置:

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


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

[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 

请您参考如下方法:

你在 virtuelenv 中安装了 Flask 吗?如果是这样,问题可能是您的 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