nomercysir
nomercysir

Reputation: 21

passing env variables to fcgi (perl)

I have an apache 2 webserver running with mod_fcgid. CGI scripts are executed by passing some environment variables and then exec'ing a perl program, eg:

index.cgi:
#!/bin/sh
export TEST_VAR=test
exec test.pl

test.pl:
#!/usr/bin/perl
use CGI::Fast; 
while ($q = new CGI::Fast) {
  print "Content-type: text/html\r\n\r\n";
  print "$ENV{TEST_VAR}";
}

This works fine when running without fcgi, but when enabling it (eg, via "SetHandler fcgid-script" in .htaccess), the TEST_VAR isn't passed (not even on the first run). Outside of that, fcgi does appear to be working.

Any ideas, or suggested approaches to this? I'd prefer to set the environment externally as outlined above for various configuration reasons, but it's not mandatory.

Thanks!

Upvotes: 2

Views: 2210

Answers (1)

Chris H
Chris H

Reputation: 46

I guess you have to check / update your apache config.

1) First I'd check the environment apache has set. From the FastCGI website:

To pass per-request environment variables to FastCGI applications, have a look at: mod_env (SetEnv, PassEnv, UnSetEnv) ...

From the FCGI manual:

Use FcgidInitialEnv to define environment variables to pass to the FastCGI application ...

Maybe it's some weird security feature of the OS / sandbox / virtual machine / chroot that modifies your environment vars before they end up in %ENV?

2) File extension for FastCGI scripts is normally .fcgi, not .cgi or .pl. Does apache recognize any of your scripts as a FastCGI/FCGI? And what did you set in the Apache config for AddType/AddHandler/SetHandler/FastCgiServer?

Also: I'm not sure your perl script even stays in memory. The exec in the shell script spawns a new "standard" perl subprocess wich should terminate after the script has run, or not?

Upvotes: 1

Related Questions