How to set an environment variable for a process spawned by the webserver?

Solution 1:

Since you are using lighttpd fast-cgi, just set it using bin-environment within the lighttpd settings.

## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server    = ( ".php" => 
    ((
            "bin-path" => "/usr/bin/php-cgi",
            "socket" => "/tmp/php.socket",
            "max-procs" => 2,
            "idle-timeout" => 20,

            "bin-environment" => ( 
                    "PHP_FCGI_CHILDREN" => "4",
                    "PHP_FCGI_MAX_REQUESTS" => "10000",

            ),

            "bin-copy-environment" => (
                    "PATH", "SHELL", "USER"
            ),
            "broken-scriptfilename" => "enable"
    ))
)

Solution 2:

You could build a wrapper script for the program, add your environment settings to it, and run the wrapper script instead of the original program.

#!/bin/sh
ENV=... /path/to/program