Apache can't read my environment variables - php

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');

Related

How to set environment variable in php lik .env file

How to include .env file in php. what are the configuration is required. Am hosted in aws ec2 instance and how to access env file in server. can anyone help me.
Assuming you are using Linux EC2, if you can type 'env' command, it should list all the environment variables for you. Now if you want to include any specific environment variable before running your php script and if that variable is not part of your global variables listed in as part of 'env' command, you can always set that using export command. This can be set before running your php program or inside php startup script.
Sample command:
export env_variable_name="value"
For persistent environment variable, these are the common files where we need to specify:
/etc/environment
/etc/profile
example:
export PHP_HOME="/path/to/php/home"
export PATH=$PATH:$PHP_HOME/bin
if you are using Bash, you can declare the variables in the ~/.bashrc:
$ export PATH="$HOME/bin:$PATH"
To load the new environment variables into the current shell session use the source command:
$ source ~/.bashrc

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 can I setup an environment variable to hold a DSN for PHP?

I am trying to set up a permanent "environment" variable per this SO Post.
I put the variable in my .bash_profile and am able to access it from the command line using.
echo $VAR
However, this is a DSN ( DB Credentials ) needed by PHP ( same thing Heroku does ) and I need to access it using
getenv() or similar.
If I run env from the command line I do not see it listed.
It appears there are different types of environment variables and I'm using the wrong one.
How should I do this correctly?
Your webserver does not run your shell so it doesn't the .bash_profile. There are various tricks to get environment variables into Apache but they all rely on having administration access to your webserver, and you don't get that on low-end godaddy hosting.
If you were to explain why you are trying to get environment variables into Apache, what type of hosting you have and what access you have to the server we might be able to better advise. There are modules which expose access to manipulate environment variables for Apache (mod_env for example) but these may not be available/appropriate. It's also possible to get data (and code) into PHP from a .htaccess file, but again its impossible to say whether this meets your requirements.

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.

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