lighttpd + webpy 启动时出错

作者:Hily 原始链接:http://hily.me/blog/2009/02/lighttpd-webpy-mysqldb-fastcgi-start-fail/
版权声明:可以转载,转载时务必以超链接形式标明文章原始出处作者信息版权声明

调了大半天,火挺大,还好总算解决了。

运行环境:

  • gentoo
  • python 2.5.2
  • lighttpd 1.4.21
  • webpy 0.31

直接跑 /work/www/test/main.py 没问题,交给跑 fastcgi 就有问题,错误提示:

2009-02-22 06:09:47: (log.c.97) server started
2009-02-22 06:09:47: (mod_fastcgi.c.1051) the fastcgi-backend /work/www/test/main.py failed to start:
2009-02-22 06:09:47: (mod_fastcgi.c.1055) child exited with status 1 /work/www/test/main.py
2009-02-22 06:09:47: (mod_fastcgi.c.1058) If you're trying to run PHP as a FastCGI backend, make sure you're using the FastCGI-enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT '(cgi)' NOR '(cli)'.
For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-programIf this is PHP on Gentoo, add 'fastcgi' to the USE flags.
2009-02-22 06:09:47: (mod_fastcgi.c.1365) [ERROR]: spawning fcgi failed.

看了下lighttpd 的代码,child exited with status 1 是程序正常退出,看来不像是 lighttpd 的问题了,问题可能出在 flup 上?

一直在 lighttpd 和 flup 上纠结了很久,还是没找出问题所在。

最后把 main.py 写成最简单的:

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:       
    def GET(self, name):
        if not name:
            name = 'world'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

好家伙,居然可以跑起来!看样子是程序的问题了。

不断删除程序代码后,最终定位问题位于 import MySQLdb 语句上,每当执行这句时就出错,注释掉就正常了。

可是用 python 命令行测试 import MySQLdb 又正常,见鬼了!

难道是 www 用户的 python 环境变量设置有问题?在 webpy 时直接输出后如下,和在 python 命令行里执行的一样:

['', '/usr/lib64/python2.5/site-packages/MySQL_python-1.2.2-py2.5-linux-x86_64.egg', '/usr/lib64/python2.5/site-packages/flup-1.0.1-py2.5.egg', '/usr/lib64/portage/pym', '/usr/lib64/python25.zip', '/usr/lib64/python2.5', '/usr/lib64/python2.5/plat-linux2', '/usr/lib64/python2.5/lib-tk', '/usr/lib64/python2.5/lib-dynload', '/usr/lib64/python2.5/site-packages']

好吧,彻底没折,直接让程序报错好了:

#!/usr/bin/env python

import sys
import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        if not name:
            try:
                import MySQLdb
            except Exception, e:
                return str(e);
        return ''

if __name__ == "__main__":
    app.run()

这下子真相大白:

Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/root/.python-eggs' The Python egg cache directory is currently set to: /root/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.

见鬼了,egg 也有缓存?干啥用滴? 暂且不管干什么用的,为什么默认要把 PYTHON_EGG_CACHE 设置到 /root 目录下呢,因为我是以 root 用户安装的?好傻。 用 baidu 和 google 查找“The following error occurred while trying to extract file(s) to the Python egg”,结果就多了。 可以修改缓存文件夹的位置或权限,也可以把 egg 直接解压。 权限已经让我纠结这么久了,我就不用这种方式了,直接解压: 解压前先从 easy-install.pth 中删除 MySQLdb 的 egg 包路径:

# cd /usr/lib64/python2.5/site-packages/
# mkdir t
# unzip MySQL_python-1.2.2-py2.5-linux-x86_64.egg -d t
# rm MySQL_python-1.2.2-py2.5-linux-x86_64.egg
# mv t MySQL_python-1.2.2-py2.5-linux-x86_64.egg

从 sys.path 中可以看到,我的 flup 也是 egg,顺带也把它解压了:

# mkdir t
# unzip flup-1.0.1-py2.5.egg -d t
# rm flup-1.0.1-py2.5.egg
# mv t flup-1.0.1-py2.5.egg

终于搞定!

-- EOF --

已经有 2 条评论 »

  1. Lighttpd(fastcgi) + web.py + MySQLdb 无法正常运行 - Bory.Chan 说道:

    2009年12月28日 17:13

    [...] 再后来找到 lighttpd + webpy 启动时出错,出现的问题与其一样。 [...]

  2. chenlb 说道:

    2009年12月28日 17:15

    多谢你的文章,我也出现同样的问题,折腾了好久。

    linux 下的权限有时很烦人。

发表一下您的高见

If you have any question, or for the language problem, please fell free to leave a comment or just contact me with email: hilyjiang [At] Gmail.