我一直在做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产生的500内部服务器错误,而不是烧瓶,我无法弄明白。它在使用解释器运行时有效,但我无法使用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
用户应用'是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()
尝试访问页面并获得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
运行错误&#34; runp-sqlite.fcgi&#34;来自控制台:
[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
我检查过的事情:
对于文字墙感到抱歉,我只是不知道你需要看到什么。如果有人能帮忙解决这个问题,我会非常感激。如果它是愚蠢的话,我道歉。 :)
更改了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
答案 0 :(得分:2)
你是否在virtlenv中安装了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