Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Getting system enviornmentals when running CGI

by westrock2000 (Beadle)
on Sep 20, 2015 at 23:49 UTC ( #1142589=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1142589]
Approved by stevieb
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-05-27 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (94 votes). Check out past polls.

    Notices?