My scenario
I have set up in 1 VM (centos7):
Nginx
php-fpm
Nextcloud
For this task, I have followed this guide (of course, I had to change some settings to make it work in my environment)
My question
In some point of the guide, I had to uncomment these lines in the config file for php-fpm /etc/php-fpm.d/www.conf
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
In the guide, It was said that these were php-fpm system environment variables. I had to uncomment them to active them.
I know nothing about these variables, but I know that they are taking as values some info of my system.
Also, I have to say that my nextcloud is working without errors with these variables uncommented. But just for testing, I commented the lines back again, and nextcloud continued working as usual.
So what I want to learn and currently I can't understand is:
Why the guide says that these variables are needed? What is the function of these variables, if activated, in the communication process between nginx and php-fpm?
Why the guide says that these variables are needed?
Because it is a somewhat junky, extraneous addition to the article.
What is the function of these variables
The primary use would be, I guess, having an environment variable pass with a different value for specific PHP-FPM pool. E.g. you have installed some CLI tool (converting between image formats for example) under /usr/local/bin/foo). Then you want to have your website to be able to launch that. It can't unless it can find it in PATH environment variable (which, by default does not include /usr/local/bin.
So you have two options there:
Change the PATH for the PHP-FPM user (defined in pool settings), e.g. in ~/.bashrc
env[PATH] = /usr/local/bin:/usr/bin:/bin
So this is just one of the ways to achieve the same.
if activated, in the communication process between nginx and php-fpm?
No, this is nothing about NGINX->PHP-FPM. It is just how PHP-FPM will run its worker processes (with which environment), so it affects how/which environment PHP scripts will see.
Related
While connected to my App Service (vanilla PHP 7.4 App Service) through SSH, I can see:
Note: Any data outside '/home' is not persisted
If my php.ini and apache2.conf reside outside of /home, I'll never be able to have changes to these files persisted correct? IIRC, I can't modify apache2.conf to set another location for php.ini within /home, the Apache configuration change will never persist the Apache restart.
This only means that I need to build a docker container to have full control of my settings? Am I understanding this correctly?
Thank you #CSharpRocks posting your suggestion as an answer to help other community members for similar issue.
Based on the MS DOC:
To Customize PHP_INI_SYSTEM directives we can't use the _.htaccess_ approach. App Service provides a separate mechanism using the PHP_INI_SCAN_DIR app setting. First, run the
following command in the Cloud Shell to add an app setting called
PHP_INI_SCAN_DIR .
az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PHP_INI_SCAN_DIR="/usr/local/etc/php/conf.d:/home/site/ini"
For example if want to change the value of expose_php run
the following commands:
cd /home/site
mkdir ini
echo "expose_php = Off" >> ini/setting.ini
For more information please refer this SO THREAD
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()
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
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).
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'