Python web.py+;lighttpd&x2B;matplotlib不工作

Python web.py+;lighttpd&x2B;matplotlib不工作,python,matplotlib,lighttpd,web.py,Python,Matplotlib,Lighttpd,Web.py,我正在尝试使用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()) a

我正在尝试使用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重新启动

我去我的网站看“helloweb.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:

非常确定这只是默认设置(除了我设置了
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",
 )

有什么建议吗?

我的第一个猜测是,你得到了一个
ImportError
,因为
matplotlib
没有正确安装,或者没有安装在
PYTHONPATH
上,或者其他一些疯狂的东西。唯一确定的方法是查看回溯。它显示您正在运行fastcgi,这意味着python代码正在另一个进程中执行。因此,在lighttpd日志中找不到回溯


您是如何运行fastcgi流程的?回溯将被写入其stderr。你也可以考虑使用。它支持将stderr重定向到日志文件和各种其他功能,使创建守护进程变得更容易。

我遵循以下方法:

我忽略了该线程顶部的链接:

那条线有解决办法。我像这样运行python过程:

/var/www/hello.py fastcgi 9080

然后设置我的
fastcgi.conf
,如下所示:

 fastcgi.server = ( "/hello.py" =>
     ((
        "host" => "127.0.0.1",
        "port" => 9080,
        "check-local" => "disable"
    ))
 )
然后它就起作用了。(仍然不确定是否已正确配置了所有内容,但似乎一切正常。)

我通过以下方式解决了此问题:

pip install flup
不需要

/var/www/hello.py fastcgi 9080
我的系统是:亚马逊ec2,ubuntu 10.04
lighttpd:1.4.26

今天偶然发现了这个问题(使用Apache,但可能是完全相同的问题)。我从脚本中重定向了stdout和stderr以查看发生了什么,问题是matplotlib正在尝试创建一个文件:

Traceback (most recent call last):
  File "/home/ec2-user/dlea/src/dla.py", line 24, in <module>
    import dbm
  File "/home/ec2-user/dlea/src/dbm.py", line 7, in <module>
    import matplotlib
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 709, in <module>
    rcParams = rc_params()
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 627, in rc_params
    fname = matplotlib_fname()
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 565, in matplotlib_fname
    fname = os.path.join(get_configdir(), 'matplotlibrc')
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 240, in wrapper
    ret = func(*args, **kwargs)
  File "/usr/lib64/python2.6/site-packages/matplotlib/__init__.py", line 439, in _get_configdir
    raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h)
RuntimeError: Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data
为了跟踪问题,我将stdout和stderr重定向到某个日志文件以查看发生了什么:

sys.stdout = open("/var/log/dla_stdout.txt", 'a')
sys.stderr = open("/var/log/dla_stderr.txt", 'a')

实际上,我从另一个StackOverflow问题中得到了解决方案:

我只是在启用fastcgi的情况下运行lighttpd。我猜lighttpd在幕后处理衍生过程?我编辑了我的问题以显示我的fast-cgi.conf。这有帮助吗?另外,我还不太明白supervisord是如何融入这一切的。。。对不起,如果我是个哑巴,我对这个东西还是很陌生的。这是很久以前的事了,我真的无法测试这个答案,所以我只想假设它是对的:)
sys.stdout = open("/var/log/dla_stdout.txt", 'a')
sys.stderr = open("/var/log/dla_stderr.txt", 'a')