No environment variables are available via PHP-fpm+nginx - php

I tried to modify php.ini in the following way:
variables_order = "GPCSE"
register_globals = On
But the required PATH variable is neither in $_ENV nor accessible via getenv('PATH').
I'm running Nginx + PHP-FPM on Ubuntu 10.04.
Note:
executing the following command in console gives a correct result:
php -r "echo getenv('PATH');"
I guess that PATH is environment variable of bash, but as long as php-fpm not starting via bash it doesn't have required variables. Any way to include them?
Thanks.
Update#1:
As temporary solution I found out that PATH variable stored in '/etc/environment' file. So I just going to read it from there. If someone needs a code:
preg_match('/^(PATH)="?([^"]+)"?$/i', file_get_contents('/etc/environment'), $match);
putenv($match[1]."=".$match[2]);

By default, PHP-FPM clears the environment variables (from the www.conf):
; Clear environment in FPM workers
; Prevents arbitrary environment variables from reaching FPM worker processes
; by clearing the environment in workers before env vars specified in this
; pool configuration are added.
; Setting to "no" will make all environment variables available to PHP code
; via getenv(), $_ENV and $_SERVER.
; Default Value: yes
;clear_env = no
You can uncomment the last line to set clear_env to no, but if you prefer, you can set only the environment variables needed:
env[PATH] = $PATH

I came across this problem when upgrading my OwnCloud installation to version 8.1.
They describe a fix in their documentation
Summary is: Locate your www.conf file in your php5-fpm config folder (for Ubuntu this is /etc/php5/fpm/pool.d/www.conf) and uncomment the needed env[PATH] line.
Optionally update the content of the variable with the output of php -r "echo getenv('PATH');"

You could specify it via an include path: http://www.php.net/manual/en/ini.core.php#ini.include-path
You can specify it your php.ini (though you may want a specific path instead of including everything)
include_path=".:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/fo‌​ta/Development/adt-bundle-linux-x86_64-20130917/sdk/platform-tools "

Related

PHP SNMP Warning: SNMP::get(): No response from 127.0.0.1

I'm trying to get PHP SNMP to work on XAMPP PHP 7.1.9
I'm always getting this result: Warning: SNMP::get(): No response from 127.0.0.1
Here's what I did:
Enabled php_snmp.dll on php.ini
Installed net-snmp-5.5.0-2.x64
Added windows environment variable MIBDIRS with value C:\usr\share\snmp\mibs
Code I'm running to test:
<?php
$session = new SNMP(SNMP::VERSION_1, "127.0.0.1", "public");
$sysdescr = $session->get("sysDescr.0");
echo "$sysdescr\n";
$session->close();
I'm new to this so I'm not sure what I'm missing.
The problem was with the MIBDIRS path.
I followed php docs and it says:
The Windows distribution of Net-SNMP contains support files for SNMP in the mibs directory. This directory should added to Windows' environment variables, as MIBDIRS, with the value being the full path to the mibs directory: e.g. c:\usr\mibs.
Problem was I couldn't find c:\usr\mibs on my net-snmp installation so I used C:\usr\share\snmp\mibs instead.
Solution:
I tried using a device with snmp support instead of 127.0.0.1 and ran snmpget on cli and confirmed net-snmp installation actually works. I figured the problem must be with the path php is using to run snmp.
So I used where snmpget to check the command path then changed MIBDIRS environment value with the result which is C:\usr\bin\snmpget.exe

OCIEnvNlsCreate() failed - please check that ORACLE_HOME and > LD_LIBRARY_PATH

ubuntu 16.10 / nginx / php7.0 / oracle 11 xe
I'm trying to connect to the Oracle databas via PHP. And I get the following errors.
$conn = oci_connect('login', 'pass', 'localhost/xe');
Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something
wrong with your system - please check that ORACLE_HOME and
LD_LIBRARY_PATH are set and point to the right directories in
/var/www/blah.php on line 26
Warning: oci_connect(): Error while trying to retrieve text for error
ORA-01804 in /var/www/blah.php on line 26
Really request variables, they will be empty. This code return empty string.
$test = getenv('LD_LIBRARY_PATH')." ".getenv('ORACLE_HOME');
print ("$test");
But in phpinfo() they are displayed.
I can write environment variables to each file, but this is not an option.
putenv("ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe");
putenv("LD_LIBRARY_PATH=-Wl,-rpath,/u01/app/oracle/product/11.2.0/xe/lib -L/u01/app/oracle/product/11.2.0/xe/lib -lclntsh");
What can I do?
I solved it by removing semicolon from the line ;clean_env=no in the file /etc/php-fpm.d/www.conf in case of php-fpm 5.6 in CentOS 6. Hope it will help in newer PHP versions. Remember to restart php-fpm service after making changes to this file.
We dont have the same infrastructure but I just solved this same issue in :
Apache
CentOS7
PHP 5
Oracle 12cR1
The message says :
please check that ORACLE_HOME and LD_LIBRARY_PATH are set and point to the right directories
In apache, there is a file where you can set the environment variable :
/etc/sysconfig/httpd. There has to be a config file for nginx.
I added the required environment variables.
Assuming that
ORACLE_HOME is /oracle/product/12.1.0/dbhome_1
Oracle client was installed under /oracle/product/12.1.0/client
Add :
ORACLE_HOME=/oracle/product/12.1.0/dbhome_1
LD_LIBRARY_PATH=/oracle/product/12.1.0/dbhome_1/lib:/oracle/product/12.1.0/dbhome_1/network/lib:/oracle/product/12.1.0/client/lib
I also added :
TNS_ADMIN=/oracle/product/12.1.0/dbhome_1/network/admin
ORACLE_SID=<your sid>
After updating /etc/sysconfig/httpd I just restarted Apache's service httpd and that was it:
$ sudo systemctl restart httpd
I hope it helps !
This is related to the incorrect NLS_LANG parameter. It was set to 'NA' in my case. The fix is:
Windows - The NLS_LANG must be unset in the Windows registry (re-named is best). Look for the NLS_LANG subkey in the registry at \HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE, and rename it.
Linux/UNIX - Here you simply issue the Linux command "unset NLS_LANG"

Debug php executed from rabbitmq queue with xdebug

I'm trying to debug PHP code executed by a RabbitMQ consumer.
xdebug is working fine in both CLI and http, but it fails when something is executed from a queue. I have set the following trace:
ini_get("xdebug.idekey")
ini_get("xdebug.remote_host")
ini_get("xdebug.remote_connect_back")
ini_get("xdebug.remote_mode").
ini_get("xdebug.remote_autostart")
php_ini_loaded_file()
getenv("XDEBUG_CONFIG")
It prints:
""
localhost
1
req
0
/etc/php5/cli/php.ini
xdebug.idekey=sublime.xdebug
xdebug.remote_host=10.5.223.108
xdebug.remote_connect_back=0
xdebug.remote_mode=req xdebug.remote_autostart=1
So the problem seems to be that it is ignoring the environment variable XDEBUG_CONFIG and is using the values in the cli ini file. I don't have permissions to modify the ini file, and the XDEBUG_CONFIG variable works fine when executing something via CLI.
Any ideas why XDEBUG_CONFIG is being ignored when the code is executed from a queue?
Xdebug and RabbitMQ consumer commands
To make your IDE aware of the connection you need to prefix the command with the environment variable:
XDEBUG_CONFIG="put-here-your-value" php my-script.php

Cant access environment variables in php

Im having issues accessing OS environment variables in php
I have apache/php installed on a centos 6.3 image
in httpd.conf mod mod_env.so is loaded
in php.ini I have set variables_order = "EGPCS"
restarted httpd (many times)
in shell if I type "env" I get
DB_PORT_28017_TCP_PROTO=tcp
HOSTNAME=c6188a8bd77f
DB_NAME=/rockmongo/db
DB_PORT_27017_TCP=tcp://172.17.0.36:27017
TERM=xterm
DB_PORT_28017_TCP_PORT=28017
DB_PORT=tcp://172.17.0.36:27017
DB_PORT_27017_TCP_PORT=27017
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/etc/php.d
DB_PORT_27017_TCP_PROTO=tcp
DB_PORT_28017_TCP_ADDR=172.17.0.36
DB_PORT_28017_TCP=tcp://172.17.0.36:28017
SHLVL=1
HOME=/
DB_PORT_27017_TCP_ADDR=172.17.0.36
container=lxc
_=/usr/bin/env
OLDPWD=/etc
which has the variables im after, however if I execute print_r($_ENV); in php I get
Array ( [TERM] => xterm [PATH] => /sbin:/usr/sbin:/bin:/usr/bin [PWD] => / [LANG] => C [SHLVL] => 2 [_] => /usr/sbin/httpd )
have also looked in $_SERVER & $GLOBALS.
Interestingly if I execute php -i in shell I see the env variables set correctly in _ENV
I should note im running this in a docker container, however I dont believe it is a issue as variables display correctly in #env & #php -i. I think I have a issue with my httpd/php config
Anyone have advice for this?
thanks
I ended up having a few options
if docker container needs to run multiple services, setting env vars to /etc/environment will make them available for all users. I added the following line to my Dockerfile CMD
CMD ["env | grep _ >> /etc/environment"]
if docker container runs a single service, its best to set the entry point to the desired application, env vars will automatically be passed to application user. this is my Dockerfile CDM & ENTRYPOINT to run apache
ENTRYPOINT ["/usr/sbin/httpd"]
CMD ["-D", "FOREGROUND"]
Dagon is correct.
Unless you logged in as your web server User (apache?) you may not see the same environment variables. You can see them easily with a phpinfo test file though:
<?php
phpinfo();
?>
Or you can set your own with a .htaccess file:
SetEnv HTTP_MY_VARIABLE "my value";
From dwitz: You can also make the environment variables available system wide with this:
env | grep _ >> /etc/environment
Sorry can't comment yet... So had to create an answer.
Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.
The library is available here: https://github.com/jpcercal/environment
As you say, the environment are already loaded. Then, to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:
<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));
Enjoy it.

$PATH environment variable for apache2 on mac

I am trying to get apache/php to recognize the path to my git. I have been researching and looking in all corners of the web and cannot find how to do this. Basically, no matter what I try, when I run echo phpinfo(); the Apache Environment path does not change from /usr/bin:/bin:/usr/sbin:/sbin. And when I run system('echo $PATH'); in PHP, it reads the same.
System Information:
Mac OSX (Lion)
Apache 2 (running as _www)
PHP 5.3.6
Here is what I have tried editing so far:
/etc/profile
~/.bash_profile
~/.profile
/etc/path
/etc/path.d/{NEW_FILE}
Nothing I have tried so far has changed the $PATH variable. Any ideas?
SOLUTION
So here is the final solution. I edited the
/System/Library/LaunchDaemons/org.apache.httpd.plist
and added
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin</string>
</dict>
You can set the PATH environment variable in /System/Library/LaunchDaemons/org.apache.httpd.plist.
More in the docs.
Did you update the PATH environment variable of user '_www'? Apache will read environment variables from the user runs itself. Or, it looks like you didn't restart apache after updating PATH environment variable.
Check out the older discussion :
How do I add paths to the Apache PATH variable?
Setting environment variables in OS X?
And if you want to modify environment variable in PHP, getenv() and putenv() can be a better choice.
getenv : http://php.net/manual/en/function.getenv.php
putenv : http://www.php.net/manual/en/function.putenv.php
$path = getenv('PATH');
putenv( "PATH=$path:/new_path_that_you_want_to_add" );
Important note for El Capitan (Apologies for the new answer - I don't have enough Rep to comment)
On OSX 10.11, the /System/Library folder is protected, so the files can't be edited.
You need to:
Reboot into Recovery Mode (hold CMD + r after the startup sound)
Once in recovery mode, go to Utilities > Terminal
Run:
csrutil disable
Reboot back into OSX - you should now be able to change the files
Once done, go back to recovery mode and run
csrutil enable
Hope that helps
I created this gist that helped me out from the information above:
https://gist.github.com/srayhunter/5208619
My problem was that PHP was not finding a program that we had installed under /usr/local/bin. Once we did the above it all worked and played nice on mac osx.
for ubuntu server, in /etc/apache2/envvars,
for centos server, in /etc/sysconfig/httpd,
to add:
export PATH=<your php cli path>
and restart apache
A similar problem to what I was having installing Derby. The way I solved it was by opening TextEdit. Select File > Open at this point press Shift + Command + . , this will allow you to view all the documents. Head to the user directory and search for a file called ".profile" . Open it and add the export VARIABLE= Value line for example:
export DERBY_HOME=/opt/local/share/java/derby/
Save the document and restart your terminal to see if the changes went into affect.

Categories