如何使用lighttpd fast-cgi和pyenv运行python脚本?

时间:2014-09-15 16:50:00

标签: fastcgi lighttpd pythonbrew

我正在用自己的用户运行脚本:

# from /etc/lighttpd/lighttpd.conf

server.document-root        = "/opt/app/current/bin/"
server.username             = "klenwell"

fastcgi.server  = (
    ".py" => (
        "localhost" => (
            "socket" => "/tmp/klenwell-fastcgi.socket",
            "bin-path" => "/opt/app/current/bin/app.py",
            "max-procs" => 5
        )
    )
)

在我的python脚本中,我有这样的shebang设置:

#!/usr/bin/env python

但是当我运行脚本时,我收到一条错误,指出它无法加载我用pip安装的模块。这表明它使用的是系统python而不是pyenv版本。

/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf start
(traceback omitted)
ImportError: No module named requests

如果我改变shebang以明确使用pyenv python安装,就像这样:

#!/home/klenwell/.pyenv/versions/2.7.8/bin/python

我能够成功启动lighttpd。

目前,相同的基本配置是使用pythonbrew而不是pyenv成功运行Web应用程序。但由于pythonbrew已被弃用,我正在尝试迁移到pyenv。有关如何配置lighttpd以便成功为我的用户加载pyenv的任何想法?

1 个答案:

答案 0 :(得分:1)

一位同事发现了这一点。它需要将pyenv路径添加到PATH下的bin-environment变量。例如:

fastcgi.server  = (
    ".py" => (
        "localhost" => (
            "socket" => "/tmp/klenwell-fastcgi.socket",
            "bin-path" => "/opt/app/current/bin/app.py",
            "bin-copy-environment" => (""),
            "bin-environment" => (
              "PATH" => "/home/klenwell/.pyenv/shims:/home/klenwell/.pyenv/bin:/bin:/sbin:/usr/bin:/usr/local/bin"
            ),
            "max-procs" => 5
        )
    )
)