PHP and Apache environment variables - php

I want to pass an environment variable in linux to apache on start up.
export MYVAR=5
--I define my environment variable on the command line
PassEnv MYVAR
--set apache to import the variable in apache config file
apachectl restart
--when I restart apache I don't get an error message. However I have noticed
that if I do not create the environment variable in my first step, I get a warning message, so must be working here
echo $_SERVER['MYVAR']
--i try to access the environment variable within PHP but it is not defined
I've observed that if I try to PassEnv an environment variable that already exits (one that I havn't created myself) it works fine. The SetEnv directive also works fine.
I'd really like to pass an environment variable to apache on the fly without writing it in a file. Help much appreciated.
I'm using CentOS, PHP5 and Apache2.
Thanks.
update
it seems the environment variable gets passed if i invoke the apache startup directly with httpd and not use apachectl which is a shell script. I would have thought that the "export" would have exported the variable to the shell script no? I am not a linux guru so excuse my lack of knowledge.

If you want the environment variable to be passed to apache, you should make sure that said environment variable is defined for the environment that apache is running in. To do that, the easiest option is to add an export MYVAR=value line to envvars (should be located in /etc/apache2) or the script that starts apache (in /etc/init.d), and add your PassEnv MYVAR where it's wanted in your apache configuration.
Restarting apache should make sure the MYVAR environment variable is passed. AFAIK you won't be able to change the value of this var while apache is running though...
Some relevant info for CentOS : http://php.dzone.com/news/inserting-variable-headers-apa&default=false&zid=159&browser=16&mid=0&refresh=0

You should access environment variables using the superglobal $_ENV:
$_ENV['MYVAR']
http://www.php.net/manual/en/reserved.variables.environment.php
Update
Your variable may actually be under $_SERVER afterall, as per the link below. Worth checking $_ENV though.
As per http://www.php.net/manual/en/reserved.variables.environment.php#97105

To expand on wimvds' answer above; you can change environment variables while Apache is running with the SetEnvIf module. Specifically, the SetEnvIf directive of said module.
e.g.
SetEnvIf Request_URI "^.*\/foobar.*$" FOOBAR="something"
The above example will set the $FOOBAR environment variable to "something" if the regex matches the request URI (in this case, any URI containing the word "foobar"). You can define this in your host's configuration file (usually in /etc/apache2/sites-available/mywebsite.conf or something similar).

Related

What cache stores the windows PATH and how do I clear it?

I am using PHP in an HTML5 document in Windows 10, calling shell_exec() which I want to use to call Rscript, ultimately. The problem I have run into is that the environmental %PATH% variable in Windows that shell_exec() is getting from some cache somewhere is yesterday's version, which doesn't include the path to Rscript, so the call fails.
I carefully set both the personal and the system environmental PATH variables in Windows using the Windows tool to include a path to Rscript, and then later to remove another link to PERL to make sure the total path was below 260 characters, then I moved the Rscript path up to the front. No joy.
The following command pulls up a %PATH% from somewhere but I have no idea where If I simply use CMD to go to that directory and issue the commands via that shell, they find the right path, but if I call it via shell_exec() it's the old path. I've tried rebooting and clearing the browser caches so I'm pretty sure this is a PHP cache thing, but I see many references to cahces here to figure out which one I'm facing.
So any help would be appreciated.
This code shows the OLD path, not the current one:
shell_exec("echo %PATH% > outputFilexx.Rout 2> errorFilexx.Rout");
Environment variables are not really cached but simply part of the run-time environment of a process. A new process inherits all environment variables and their values - at that time when it is started - from its parent process.
After that changing the value of an environment variable in one process does not affect other (already running) processes.
How to change an environment variable of a running process depends on the program and may not always be possible. To "reload" environment variables otherwise you need to restart the process - and possibly also their parent processes since otherwise they would inherit the "old" values from them again.
I.e. you not only should restart PHP/Apache but also the WAMP console so %PATH% is inherited from the system environment. To be completely sure you can just reboot the whole machine.
But environment variables may also be change via application depend configurations. So you could change the PATH only for
Apache via SetEnv
PHP via putenv()

Is it safe to source system-wide environment variables in apache envvars?

I am trying to set an environment variable on Linux which should be available in shell as well as Apache.
I added the variable to /etc/environment, so the file looks something like this:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
MY_VAR="foo"
To have MY_VAR available in apache, this is what I added in /etc/apache2/envvars:
. /etc/environment
export MY_VAR=$MY_VAR
After doing a lot of research, this is the only way I was able to get the same environment variable work system-wide and in Apache without having to declare them twice.
Are there any security concerns with this approach?
In general, you don't need to use the keyword export when sourcing scripts.
Therefore the extra script (i.e. my_apacheenv.sh) should have this format:
VARIABLE_A="some hardcoded value"
VARIABLE_B="${DYNAMIC_ONE}"
VARIABLE_C="${APACHE_HOSTNAME}"
and be sure it is marked as executable (chmod +x /path/to/my_apacheenv.sh).
The file /etc/apache2/envvars should contain:
. /path/to/my_apacheenv.sh
Then you can define the value of DYNAMIC_ONE or APACHE_HOSTNAME in any usual way, either in the session or as environment variable.
For me, it works pretty well when used in combination of a docker-compose file:
version: "3"
services:
single-apache-php:
build: ./../cicd/apache
environment:
APACHE_HOSTNAME: "my.dynamic.hostname.com"
I was able to solve my own problem. I removed MY_VAR from /etc/environment and created a separate file /etc/profile.d/apache.sh which looks like this:
export MY_VAR="foo"
Then I sourced this file in /etc/apache2/envvars like so:
. /etc/profile.d/apache.sh
Any variable set in apache.sh is available system-wide as well as in Apache
Because apache.sh is a script, unlike /etc/environment, the variables do not have to be "export"ed again in envvars
There is no security risk because no unwanted variables are passed magically to Apache
Whatever variables you want to be available system-wide as well as Apache, you add it to apache.sh and if you want it only in Apache then you add it in envvars

how to set environment variables in apache xampp ?

I would like to add some environment variables in apache xampp in windows.
I already tried the following :
adding this in httpd.conf
SetEnv ENVIRONMENT "setting"
or, run this script
putenv("ENVIRONMENT='setting'");
but that doesn't seem to work when I echo phpinfo(), there isn't ENVIRONMENT variable in the environment section
I know this is an old post and the OP already may has solved this by himself but for others:
SetEnv ENVIRONMENT setting
This would be the correct Syntax inside of the httpd.conf file. Also, this code should be put at the end of the file.
After saving httpd.conf the apache server needs to be restarted. Otherwise the environment variable will not be set in "this" apache session!
Sincerely,
Kami Yang
No server restart required if you add your environment variables to a .htaccess file in your code directory. For example if you have a PHP app under the directory P:\xampp7.3\htdocs\MyTestApp, add a P:\xampp7.3\htdocs\MyTestApp\env-test.php script and copy the line of code below into it:
<?php echo getenv('APPLICATION_NAME'); // print APPLICATION_NAME environment variable ?>
Next create a P:\xampp7.3\htdocs\MyTestApp\.htaccess file and add:
SetEnv APPLICATION_NAME "Testing my application environment variable"
When you run the env-test.php script, it should print
Testing my application environment variable

How do I print Linux shell environment variable in a PHP web application running on Apache

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

reinitialize system wide environment variable in linux

I just want my apache to register some of my predefined environment so that i can retrieve it using getenv function in php. How can i do this? I tried adding /etc/profile.d/foo.sh with export FOO=/bar/baz using root and restarted apache.
Environment variables are inherited by processes in Unix. The files in /etc/profile.d are only executed (in the current shell, not in a subshell) when you log in. Just changing the value there and then restarting a process will not update the environment.
Possible Fixes:
log out/log in, then start apache
source the file: # . /etc/profile.d/foo.sh, then restart apache
source the file in the apache init script
You also need to make sure that /etc/profile.d/ is sourced when Apache is started by init rather than yourself.
The best fix might also depend on the distribution you are using, because they use different schemes for configuration.
You can use SetEnv in your config files (/etc/httpd/conf.d/*.conf, .htaccess ...). Additionally you should be able to define them in /etc/sysconfig/httpd (on RPM-based distribs) and export them (note: not tested).
Note: it wouldn't surprise me if some distributions tried quite hard to hide as much as possible, as far as system config is concerned, from a publically accessible service such as Apache. And if they don't, they might start doing this in a future version. Hence I advise you to do this explicitly. If you need to share such a setting between Apache and your shells, you could try sourcing /etc/profile.d/yourprofile.sh from /etc/sysconfig/httpd
Apache config files allow you to set environment variables on a per site basis.
So if your web server is serving pages from two logical sites you can have the same environment variable set differently for each site and thus get your PHP to react differently.
See the Apache mod_env for details:
If you need env vars for Apache only, what worked for me was editing the /etc/apache2/envvars and restart of Apache. I added these settings:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

Categories