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”。
但是如果我在hello.py中添加了import matplotlib
并重启服务器,然后再访问网站,就会出现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",
)
有什么建议吗?
4 个回答
我通过以下方式解决了这个问题:
pip install flup
不需要
/var/www/hello.py fastcgi 9080
我的系统是:亚马逊EC2,Ubuntu 10.04
lighttpd版本:1.4.26
我在按照这个教程操作: http://webpy.org/cookbook/fastcgi-lighttpd
我忽略了顶部的一个链接,指向这个讨论串: http://www.mail-archive.com/webpy@googlegroups.com/msg02800.html
那个讨论串里有解决办法。我是这样运行python程序的:
/var/www/hello.py fastcgi 9080
然后我把我的 fastcgi.conf
设置成这样:
fastcgi.server = ( "/hello.py" =>
((
"host" => "127.0.0.1",
"port" => 9080,
"check-local" => "disable"
))
)
这样就可以了。(虽然我还不确定我的配置是否完全正确,但看起来一切都在正常运行。)
今天我遇到了一个问题(是在使用Apache的时候,但这个问题可能在其他情况下也会出现)。我把脚本的输出和错误信息重定向到文件里,想看看发生了什么,结果发现是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
因为这个脚本是以httpd用户(Apache的用户)身份运行的,所以它试图在/var/www/目录下创建文件,而这个目录是属于root用户的,Apache用户没有权限写入。
一个简单的解决办法就是在导入matplotlib之前,把MPLCONFIGDIR设置为一个临时目录:
import os
import tempfile
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
import matplotlib
为了跟踪这个问题,我是这样把输出和错误信息重定向到一个日志文件里的,以便查看发生了什么:
sys.stdout = open("/var/log/dla_stdout.txt", 'a')
sys.stderr = open("/var/log/dla_stderr.txt", 'a')
其实我从另一个StackOverflow的问题中找到了这个解决方案: 设置Matplotlib的MPLCONFIGDIR:考虑把MPLCONFIGDIR设置为一个可写的目录,用于matplotlib的配置数据