How to set the environment variable for PHP in Linux? - php

I'm implementing MediaWiki with the Oracle Database and therefore I need to add TNS_ADMIN variable to my PHP startup environment.
Q1: How do i add TNS_ADMIN variable to PHP Startup environment?
Q2: Is there a specifc file to edit in order to add the env varibale? If so where can i find it on Linux?

If you are using Apache and the apache2handler SAPI, you can use SetEnv; you can also export the variable from the init script that starts the PHP process (works for all SAPIs). For example, on most (older) Linux-based distros, you'll probably find a file named /etc/init.d/apache2 or /etc/init.d/httpd that is a shell script where you can export variables for that service. Check your OS' equivalent of "man 5 init" for specifics.

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

Is it required to set/define/declare/specify/assign an environment variable PHP_INI_SCAN_DIR in order to scan directory/directories by PHP?

I'm using PHP 7.2.11 on my laptop that runs on Windows 10 Home Single Language 64-bit Operating System.
I've installed PHP 7.2.11 and Apache/2.4.35 (Win32) using the latest version of XAMPP for Windows.
I come across following paragraph from PHP Manual :
It is possible to configure PHP to scan for .ini files in a directory
after reading php.ini. This can be done at compile time by setting the
--with-config-file-scan-dir option. In PHP 5.2.0 and later, the scan directory can then be overridden at run time by setting the
PHP_INI_SCAN_DIR environment variable.
I executed below code to check whether the environment variable PHP_INI_SCAN_DIR exist on my machine or not by executing below code in my web browser :
<?php
var_dump(getenv('PHP_INI_SCAN_DIR'));
?>
Output of above code is :
bool(false)
The output clearly says that no such environment variable titled PHP_INI_SCAN_DIR does exist on my machine.
Then, my question is do I required to set/define/declare/specify/assign an environment variable titled PHP_INI_SCAN_DIR so that PHP can scan the additional directory/directories? If yes, then please let me know how should I define it and in which file it should be defined? If no, then how should PHP scan additional directory/directories?
Is this the case with Windows only that the environment variable PHP_INI_SCAN_DIR doesn't exist or is this the case with every other operating system on which PHP is running?
Is there really any need to make use of the environment variable PHP_INI_SCAN_DIR and scan additional directory/directories in my situation i.e. in Windows and XAMPP setup environment?
I'm asking this question because I've heard that mostly Linux operating systems do required to do all this stuff.
Thanks.

How to read Windows's Environment variables from php using built-in webserver?

I use Azure to deploy my code, so I'm using server's variables to save important settings (like client_id and client_secrets).
$_SERVER['APPSETTING_Imgur_Client_Id'];
Set Azure Application Environment variables
Azure $_SERVER
I would like to use the same mecanism in my development environnement (Windows), by setting Environnement variables (directly to windows) and use them in my app.
So I have defined 2 Windows Variables (system properties->Environment Variables->User Variables):
Set Windows's Evironment variables
But thoses variables aren't available through $_SERVER['APPSETTING_Imgur_Client_Id']; (with php -S [::]:80 index.php cmd).
I got only default $_SERVER variables.
So I'm askying:
How to read thoses Windows's evironment variables with php?
If we can't how to add custom $_SERVER variables (without using the php application code).
I'm using:
PHP 7.1.10
&
Windows Server 2016
I have changed the locations of variables (now under System variables) and restarted my computer.
But after that, even with getenv('APPSETTING_Imgur_Client_Id'') or $_SERVER I can't access to my custom variables.
But with command line php -r 'var_export($_SERVER);' I can see my variables
If I want to edit my code, I clone the repo and launch the webserver from the local directory with php -S [::]:80 index.php.
It seam that the php -S command doesn't pass windows environment.
Thanks you.
Edit: Solution:
use getenv see picture below:
Postman sample, with both user env and system env
Use the getenv function.
Documentation at http://php.net/manual/en/function.getenv.php
Note that the variables set under system properties->Environment Variables->User Variables can not be seem by a web server when the web server runs under a different user account. You need to set the variables via system properties->Environment Variables-> System Variables (and then restart the web server to have them available)

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