fastcgilighttpdpythonbrew

How can I run a python script using lighttpd fast-cgi with pyenv?


I am running the script with my own user:

# 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
        )
    )
)

In my python script, I have the shebang set like so:

#!/usr/bin/env python

But when I run the script, I get an error stating it can't load a module I installed with pip. This indicates that it is using system python rather than the pyenv version.

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

If I change the shebang to use the pyenv python install explicitly, like so:

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

I am able start lighttpd successfully.

Currently, the same basic configuration is successfully running the web application using pythonbrew rather than pyenv. But since pythonbrew has been deprecated, I am trying to migrate to pyenv. Any ideas on how to configure lighttpd so that it successfully loads pyenv for my user?


Solution

  • A colleague figured this out. It required adding the pyenv paths to the PATH variable under bin-environment. For example:

    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
            )
        )
    )