zoukankan      html  css  js  c++  java
  • lighttpd, web.py, spawning fcgi failed

    lighttpd, web.py, spawning fcgi failed

    基于web.py的程序开发起来还是非常简单的,但是我没想到在服务器上部署的时候却遇到了不少麻烦。我用的 web server 是 lighttpd,不能正常启动,查看错误日志,发现如下几行:

    2009-12-15 19:48:04: (server.c.1503) server stopped by UID = 0 PID = 25128 2009-12-15 19:48:30: (log.c.166) server started
    2009-12-15 19:48:30: (mod_fastcgi.c.1104) the fastcgi-backend /var/www/code.py failed to start:
    2009-12-15 19:48:30: (mod_fastcgi.c.1108) child exited with status 1 /var/www/code.py
    2009-12-15 19:48:30: (mod_fastcgi.c.1111) If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.If this is PHP on Gentoo, add 'fastcgi' to the USE flags.
    2009-12-15 19:48:30: (mod_fastcgi.c.1399) [ERROR]: spawning fcgi failed. 2009-12-15 19:48:30: (server.c.931) Configuration of plugins failed. Going down.
    

    经历了许多周折之后,问题终于解决掉──实际上不是仅仅一个问题。在这儿把经验分享一下,如果有人遇到同样的问题,至少可以少走一些弯路。请按照一下几点检查错误:

    code.py 可以执行吗?

    当然你可能没有用 code.py 这个名字,我给可执行文件的名字是 root.py

    检查一下文件的权限,code.py 必须是可执行的。如果没有执行权限,那么给它加上:

    # chmod 755 code.py
    

    同时,要保证文件头部有这样的指令:

    #!/usr/bin/env python
    

    hello world 的例子可以正常运行吗?

    把你的 code.py 的内容用 web.py 首页的例子替换(别忘了加上第一行的指令):

    #!/usr/bin/env python
    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()
    

    一般情况下这个例子是可以正常运行的。这说明可能是我们自己的程序比 hello world 多引用的库出了问题。

    检查一下 egg cache 的权限

    我的程序要链接 MySQL 数据库,所以用到了 MySQL-python,启动失败肯定有它的份。现在试试在 hello world 里加上 import MySQLdb,果然不能启动了!为了看到原因,我们用 try-except 来捕获 import MySQLdb 引发的异常,并且把它输出到网页上。把上面 hello 类的 GET 函数改成:

    try:
        import MySQLdb
    except Exception, e:
        return str(e);
    return 'hello'
    

    打开 localhost:8080, 我看到了如下的错误信息:

    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: ‘/sbin/.python-eggs’ The Python egg cache directory is currently set to: /sbin/.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.

    又是权限问题,解决办法有很多种,我就把它的 owner 改成了运行 lighttpd 的用户(我配置文件里写的是 daemon):

    chown -R daemon.daemon /sbin/.python-eggs
    

    lighttpd 使用的是哪个 python?

    lighttpd 执行环境的环境变量可能和你在 shell 里使用的是不一样的!我有两个 python,一个是 CentOS 自带的老旧 python 2.4,另一个是我后来自己编译的 python 2.6,在我的 $PATH 里,python 2.6 所在的目录是优先的,但是后来发现 lighttpd 使用的竟然是旧的 python 2.4!如果是这样,比较简单的办法就是在 code.py 的头部写上 python 2.6 的完整路径,比如我的:

    #!/usr/local/bin/python
    
  • 相关阅读:
    C语言学习之我见-strcpy()字符串复制函数
    豆邮windows客户端(第三方)开发详解
    项目管理的主要控制因素(转)
    daterangepicker -- bootstrap日期时间范围插件使用分享
    Jacob
    Selenium
    基本套接字总结(@function)
    Python字符串处理NoneType的处理
    机器学习相关会议(转载)
    jboss-AS目录结构了解(资料摘取)
  • 原文地址:https://www.cnblogs.com/andyjia/p/4993313.html
Copyright © 2011-2022 走看看