reinitialize system wide environment variable in linux - php

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'

Related

Shifting from App Services to a Docker container due to file persistence?

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

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

What is the purpose of the php-fpm system environment variables?

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.

PhP Cake app in Apache2 in Ubunutu not working but working find in window's wamp

I have a web application that is writen in php using cake framework. In windows environment i use wamp and it works find if i enbale apache read_write. Then I shift to use Linux environment Ubuntu 15. I have installed apache2 already and php too. it is all work. But when I put my app in /var/www in ubuntu and open the web page it said that the require_once function cannot file the location of the file.
I already change the owner of the www/* to www-data already, but it is still not work. mod_readwrite of apache is also enabled. So I want to know how could I enable read_write in Lamp like in wamp that I did on window? I met the same error in window if I don't enable it. So think it is the problem. Can anyone help me to solve this problem?
One thing you will want to check for is that you're using the proper directory separator in any paths you're using for your require statements (this can be done by using the DIRECTORY_SEPARATOR constant in place of hard coded slashes in your paths)
Another thing you will want to do is look at the apache configuration for each environment and ensure that they are the same. In particular check to ensure that your virtual hosts are defined correctly.

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

Categories