http://qs321.pair.com?node_id=1142589

westrock2000 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use a path from the Bash terminal in my Perl script. When I run the script from the command line as the user, the scripts picks up the variable correctly

$cwd = $ENV{'DYNBLDHOME'};

But when I run the script through Apache (as the same user) $cwd is empty

The environmental DYNBLDHOME is set in ~/.bashrc

I'm not sure where the disconnect is, it is Perl or Apache that is the issue?

I didn't know what to search for, I saw something about mod_env.so in Apache, and that is already enabled

Replies are listed 'Best First'.
Re: Getting system enviornmentals when running CGI
by flexvault (Monsignor) on Sep 21, 2015 at 02:51 UTC

    Hello westrock2000,

    Usually Apache sets the environmental variable 'DOCUMENT ROOT', but to make sure, why not print out all the variables:

    foreach my $env ( sort keys %ENV ) { print "ENV:\t$env\t$ENV{$env}\n"; }
    I usually print to a file so I can check it later and often. Perl gives you the power, so why not use it :-)

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: Getting system enviornmentals when running CGI
by dsheroh (Monsignor) on Sep 21, 2015 at 07:23 UTC
    As already mentioned, .bashrc has no effect on code running under apache because apache is not bash and doesn't read .bashrc.

    Instead, to set an environment variable for use in apache, you need to use the SetEnv command in your apache config file or .htaccess:

    SetEnv DYNBLDHOME /some/path/or/other
      The presence of other environment variables will tell you whether or not you are running under Apache. If so, all output from the script should be HTML formatted. If your program requires a SetEnv-supplied variable but does not see one, it must produce an error message output to that effect and should also make an entry in the Apache log.
Re: Getting system enviornmentals when running CGI
by Anonymous Monk on Sep 20, 2015 at 23:58 UTC

    I'm not sure where the disconnect is, it is Perl or Apache that is the issue?

    Neither.

    For ~/.bashrc to have an effect bash has to read it.

    So the issues are, bash isn't running

    or bash is running but its not reading .bashrc for documented reasons.

    Solution is simple, read it yourself or make sure bash is instructed to read it, see Re: How to execute alias commands (source your profile)

Re: Getting system enviornmentals when running CGI
by Anonymous Monk on Sep 22, 2015 at 00:41 UTC
    Bottom line: when Apache forks your process, it does not fork a shell. It does create an environment that you can identify, but it (by-design) leaves nothing to chance . . .