web.py + lighttpd + matplotlib 不工作
我正在尝试使用lighttpd 部署我的web.py 应用程序。如果导入matplotlib则不起作用。
这有效...
hello.py:
#!/usr/bin/python
import web
# Say hello.
class Index:
def GET(self): return 'hello web.py'
if __name__ == "__main__":
app = web.application(('/*', 'Index'), globals())
app.run()
/etc/init.d/lighttpd restart
我转到我的网站并看到“hello web.py”。
但是,如果我将 import matplotlib
添加到 hello.py 并重新启动服务器,那么当我访问该站点时,我会收到 500 - 内部服务器错误。
这是/var/log/lighttpd/error.log
:--
2010-12-24 00:17:31: (log.c.166) server started
2010-12-24 00:17:42: (mod_fastcgi.c.1734) connect failed: Connection refused on
unix:/tmp/fastcgi.socket-0
2010-12-24 00:17:42: (mod_fastcgi.c.3037) backend died; we'll disable it for 1 s
econds and send the request to another backend instead: reconnects: 0 load: 1
2010-12-24 00:17:43: (mod_fastcgi.c.2582) unexpected end-of-file (perhaps the fa
stcgi process died): pid: 4074 socket: unix:/tmp/fastcgi.socket-0
2010-12-24 00:17:43: (mod_fastcgi.c.3320) child exited, pid: 4074 status: 1
2010-12-24 00:17:43: (mod_fastcgi.c.3367) response not received, request sent: 9
53 on socket: unix:/tmp/fastcgi.socket-0 for /hello.py?, closing connection
2010-12-24 00:20:30: (server.c.1503) server stopped by UID = 0 PID = 4095
2010-12-24 00:20:30: (log.c.166) server started
编辑--
这是我的lighttpd.conf:http://pastebin.com/n6sG5z9K
很确定这只是默认值(除非我设置 server.document-root = "/var/www/hello/"
)
这是我的 fastcgi.conf:
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_rewrite" )
fastcgi.server = ( "/hello.py" =>
(( "socket" => "/tmp/fastcgi.socket",
"bin-path" => "/usr/bin/python /var/www/hello/hello.py",
"max-procs" => 1,
"bin-environment" => (
"REAL_SCRIPT_NAME" => ""
),
"check-local" => "disable"
))
)
url.rewrite-once = (
"^/favicon.ico$" => "/static/favicon.ico",
"^/static/(.*)$" => "/static/$1",
"^/(.*)$" => "/hello.py/$1",
)
有什么建议吗?
I'm trying to deploy my web.py app with lighttpd. It doesn't work if import matplotlib.
This works...
hello.py:
#!/usr/bin/python
import web
# Say hello.
class Index:
def GET(self): return 'hello web.py'
if __name__ == "__main__":
app = web.application(('/*', 'Index'), globals())
app.run()
/etc/init.d/lighttpd restart
I go to my site and see "hello web.py".
But if I add import matplotlib
to hello.py and restart the server, then when I go to the site I get a 500 - Internal Server Error.
Here's /var/log/lighttpd/error.log
:
2010-12-24 00:17:31: (log.c.166) server started
2010-12-24 00:17:42: (mod_fastcgi.c.1734) connect failed: Connection refused on
unix:/tmp/fastcgi.socket-0
2010-12-24 00:17:42: (mod_fastcgi.c.3037) backend died; we'll disable it for 1 s
econds and send the request to another backend instead: reconnects: 0 load: 1
2010-12-24 00:17:43: (mod_fastcgi.c.2582) unexpected end-of-file (perhaps the fa
stcgi process died): pid: 4074 socket: unix:/tmp/fastcgi.socket-0
2010-12-24 00:17:43: (mod_fastcgi.c.3320) child exited, pid: 4074 status: 1
2010-12-24 00:17:43: (mod_fastcgi.c.3367) response not received, request sent: 9
53 on socket: unix:/tmp/fastcgi.socket-0 for /hello.py?, closing connection
2010-12-24 00:20:30: (server.c.1503) server stopped by UID = 0 PID = 4095
2010-12-24 00:20:30: (log.c.166) server started
-- Edit --
Here is my lighttpd.conf: http://pastebin.com/n6sG5z9K
Pretty sure it's just the default (except I set server.document-root = "/var/www/hello/"
)
Here is my fastcgi.conf:
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_rewrite" )
fastcgi.server = ( "/hello.py" =>
(( "socket" => "/tmp/fastcgi.socket",
"bin-path" => "/usr/bin/python /var/www/hello/hello.py",
"max-procs" => 1,
"bin-environment" => (
"REAL_SCRIPT_NAME" => ""
),
"check-local" => "disable"
))
)
url.rewrite-once = (
"^/favicon.ico$" => "/static/favicon.ico",
"^/static/(.*)$" => "/static/$1",
"^/(.*)$" => "/hello.py/$1",
)
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
今天偶然发现了这个问题(使用 Apache,但很可能是完全相同的问题)。我从脚本中重定向了 stdout 和 stderr 以查看发生了什么,问题是 matplotlib 正在尝试创建一个文件:
由于它以用户 httpd (Apache) 身份运行,因此它尝试在 /var/www/ 中创建文件,它由 root 拥有,并且 Apache 用户不可写入。
一个有效的解决方案就像在导入 matplotlib 之前将 MPLCONFIGDIR 设置为临时目录一样简单:
为了跟踪问题,这就是我将 stdout 和 stderr 重定向到某个日志文件以查看发生了什么情况的方法:
我实际上从另一个 StackOverflow 获得了解决方案问题: 设置 Matplotlib MPLCONFIGDIR:考虑将 MPLCONFIGDIR 设置为可写matplotlib 配置数据目录
Stumbled into this today (with Apache, but it's likely to be exactly the same issue). I redirected stdout and stderr from the script to see what was happening, and the issue is that matplotlib is trying to create a file:
Since it's being run as user httpd (Apache), it tries to create the file in /var/www/, which is root-owned, and not writeable by the Apache user.
One valid solution is as simple as setting the MPLCONFIGDIR to a temporary directory before importing matplotlib:
To track the issue, this is how I redirected stdout and stderr to some log file to see what was happening:
I actually got the solution from this other StackOverflow question: Setting Matplotlib MPLCONFIGDIR: consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data
我正在遵循这个食谱: http://webpy.org/cookbook/fastcgi-lighttpd
我忽略了该主题顶部的链接:http://www.mail- archive.com/[email protected]/msg02800.html
该线程有解决方案。我像这样运行 python 进程:
/var/www/hello.py fastcgi 9080
,然后像这样设置我的
fastcgi.conf
:然后它就可以工作了。 (仍然不确定我是否已正确配置所有内容,但事情似乎正在工作。)
I was following this recipe: http://webpy.org/cookbook/fastcgi-lighttpd
I overlooked a link at the top to this thread: http://www.mail-archive.com/[email protected]/msg02800.html
That thread had the solution. I run the python process like so:
/var/www/hello.py fastcgi 9080
and then set my
fastcgi.conf
like so:Then it works. (Still not sure I've got everything configured properly, but things seem to be working.)
我通过以下方式修复问题:
不需要
我的系统是:amazon ec2、ubuntu 10.04
Lighttpd:1.4.26
I fix the issue by:
don't need
my system is: amazon ec2, ubuntu 10.04
lighttpd: 1.4.26
我的第一个猜测是您收到
ImportError
因为matplotlib
未正确安装或不在PYTHONPATH
上或其他一些疯狂的地方事物。唯一确定的方法是查看回溯。它显示您正在运行 fastcgi,这意味着 python 代码正在另一个进程中执行。因此,您无法在lighttpd 日志中找到回溯。你是如何运行 fastcgi 进程的?回溯将被写入其 stderr。您也可以考虑使用 supervisord。它支持将 stderr 重定向到日志文件以及各种其他使创建守护进程变得更容易的功能。
My first guess is that you're getting an
ImportError
becausematplotlib
wasn't installed properly or isn't on thePYTHONPATH
or some other crazy thing. The only way to know for sure is to look at the traceback. It shows you're running fastcgi, which means that the python code is being executed in another process. Therefore, you can't find the traceback in the lighttpd logs.How are you running the fastcgi process? The traceback would have been written to its stderr. You might also consider using supervisord. It has support for redirecting stderr to a log file and various other things that make creating daemon processes easier.