logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: How do I access Linux environment variables using Apache2.2
Author
cf_00



Joined: 07 Mar 2014
Posts: 1

PostPosted: Fri 07 Mar '14 19:36    Post subject: How do I access Linux environment variables using Apache2.2 Reply with quote

I am developing a php web application using Apache on CentOS6. I have set a custom environment variable in CentOS on command line by using: export test_var=3
I cant figure out why I am unable to print this Linux shell variable in my php script on my web-application. Apache seems to pull up its own environment variables only and ignores Linux environment variables.
Things I have tried so far:
1. I have set variable 'variables_order='EGPCS'' in /etc/php.ini. This has allowed me to access Linux variables using PHP CLI but not in my web-application.
2. I tried adding line PassEnv test_var in my Apache configuration file (httpd.conf) but Apache returns a blank value in my application. Passenv works when I add line export test_var=1 in /etc/init.d/httpd but I don't want to use this because I already have this variable declared and set in Linux and so I don't want to set it again.
3. I have written a shell script and have tried using exec to execute the shell script but when I try to print it using print_var, I get a blank array.

Any suggestions? I don't want to set the variable in any file because I have already set it in in Linux. I'm want to pull up the variable from the OS itself.

The Apache documentation says PassEnv should do the trick:
(https://httpd.apache.org/docs/2.2/mod/mod_env.html#passenv)
Quote:
Specifies one or more native system environment variables to make available as internal environment variables, which are available to Apache HTTP Server modules as well as propagated to CGI scripts and SSI pages. Values come from the native OS environment of the shell which invoked the httpd process.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Mon 10 Mar '14 16:24    Post subject: Reply with quote

Why so complex? Just use getenv(); or better $_ENV

Code:


<?php
echo 'My username is ' .$_ENV["USER"] . '!';
?>




Code:

export APP_ENV="development"


Code:

<?php
print $_ENV["APP_ENV"];
print getenv("APP_ENV");
?>
Back to top


Reply to topic   Topic: How do I access Linux environment variables using Apache2.2 View previous topic :: View next topic
Post new topic   Forum Index -> Apache