how to set environment variables in apache xampp ? - php

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

Related

Apache can't read my environment variables

I created an environment variable called API_KEY on my linux server, when I run the following command on php, I get false, meaning there is no variable with that name.
$api = getenv("API_KEY");
I created the variable following this steps:
created a keys.sh file in /etc/profile.d/ and saved my variable with this format API_KEY="my variable value"
When I run the "printenv" command as user-ec2 or root I can see the environment variable is there, but what it seems to be the problem is that when apache tries to access it dosen´t show up, I don´t know what to do so that the apache user can find this variable.
I went to etc/httpd/conf, where my httpd.conf file was located and created the environment variables using:
SetEnv API_KEY API_KEY_VALEU
Now the apache user could return the environment variable when I used:
getenv('API_KEY');

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 are env variable stored and used?

How can I set up one and use it in PHP?
I am working on linux, and so far I can do export foo='bar' and use echo $foo which works, but how do I set up an ENV variable in linux and use it in PHP?
I am assuming I could use $_ENV in php to get the contents, but not sure how to store it, and if it will stay there even if I reboot my machine
In apache you could define environment variables like this
<VirtualHost hostname:80>
SetEnv VARIABLE_NAME variable_value
</VirtualHost>
Or you can add it to the file .profile or .bashrc or your current shell profile file (located in your home directory). Then, each time you open your shell it will be loaded.

How to set global environment variables for PHP

I have read the question/answers here but I don't understand how to set variables in /etc/environment. If I edit the file, do I need to restart my machine or simply log out my current user (or log in a new one?).
I want to set a global variable to denote that websites on my machine are in 'development' or 'testing' mode. I don't want to have to set this for every project (whether it uses PHP, Java/Tomcat, NodeJS, etc). I'm aware that (for Apache) I can set the environment variable in the following ways:
directly from php with putenv() (this seems useless since I want to avoid logic that tries to figure out what server the files are on)
using .htaccess SetEnv ENVIRONMENT 'local' (this would require me to duplicate this file/code for every server, not ideal)
using a Virtual Host directive SetEnv ENVIRONMENT 'local' (if I'm using a virtual host, which in nearly all cases I am, but again requires me to copy/paste code over and over again)
in httpd-conf SetEnv ENVIRONMENT 'local' (this will only apply to apache, and I would like it to apply to NodeJS servers as well)
I'm not saying I can't do #4 (and apply #3 selectively to NodeJS servers). But I'm thinking that this is a good reason to use /etc/environment. As I said above, I have edited the file (after first creating it) and tried the following combinations, none of which seemed to work:
ENVIRONMENT='local'
ENVIRONMENT=local
export ENVIRONMENT='local'
export ENVIRONMENT=local
I say that none of them worked because I did not find the variable in output from:
print_r($_SERVER);
print_r($_ENV);
echo(getenv('ENVIRONMENT'));
What you want to do is use an Apache configuration file. You will need access to a configuration folder and the httpd.conf file (or modified version). You can then configure the httpd.conf to dynamically load configuration files using this approach
Include conf.d/*.conf
Inside the conf.d folder you place your specific environment configuration files.
server-environment-dev.conf example:
SetEnv ENVIRONMENT "local"
server-environment-prod.conf example:
SetEnv ENVIRONMENT "production"
These settings will show up in your php code as available environment variables. This approach allows you to keep your vhost files, .htaccess, and your other configuration files agnostic of the environment.
etc/environment, etc/profile.d, .bash_profile, .profile, etc files are not readable by PHP in Apache due to some process/user limitations. You can bash, smash, crash the variables all you want and see them set in your terminal but they will not show up in phpinfo() unless you set it via one of the Apache configuration files.
For NodeJS you can start the app passing in your environment variable or you can set the NODE_ENV in multiple ways include your .bash_profile and possibly etc/environment file if you want to be user agnostic.
A good read for Node:
http://www.hacksparrow.com/running-express-js-in-production-mode.html
So I would assume you have a global config file somewhere. Why not put a constant in that file that you can change? Would be far easier that trying to set something on the server level.
define('ENVIRONMENT', 'testing');
if(ENVIRONMENT == 'testing') {
echo 'We\'re just testing';
}
If you still can't get your environment variables:
you may need to edit your real httpd.conf in
~/Library/Application Support/appsolute/MAMP PRO/
instead of
/Applications/MAMP/conf/apache/
Also you may need to use getenv() instead of $_ENV

PHP and Apache environment variables

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

Categories