In my PHP code I am trying to access my linux system environment variable that I export in /etc/profile.
When I type "printenv" in my terminal, I can see that the variable (called MEMCACHED_1) set.
But my PHP doesn't see that variable, it's neither in $_ENV array nor can I access it with getenv function.
In my php.ini file I set variables_order = "EGPCS". That added a lot of variables to $_ENV, but MEMCACHED_1 is still not there.
What php.ini exactly do you mean? There are /etc/php5/apache2/php.ini (configuration used by scripts run by Apache module) and /etc/php5/cli/php.ini (configuration used by command line scripts). The latter has variables_order = "GPCS" as default setting on Ubuntu.
If you are running your code from command line, try changing /etc/php5/cli/php.ini and setting variables_order = "EGPCS".
If you are using Apache module, you may want to access needed variables as $_SERVER instead of $_ENV, or call getenv() before as comments here suggest: http://php.net/manual/en/reserved.variables.environment.php
UPDATE
Another thing to remember: PHP processes started by Apache module are run with user www-data (or whatever your User variable in Apache configuration says). So, you should also check printenv for correct user:
sudo su -l www-data -c 'printenv'
-- this should have line USER=www-data or something similar (not root, not your login). Make sure MEMCACHED_1 is set there.
Related
I need to change PHP max_input_vars variable but after change in php.ini and server restart phpinfo shows old value all the time. This is what i do:
in phpinfo() i've got
...
Loaded Configuration File /etc/php.ini
...
max_input_vars 1000
from server CL I've edit /etc/php.ini and uncomment line with max_input_vars it looks like this:
...
max_input_vars = 5000
...
after this changes and restarting web server
systemctl restart httpd
phpinfo still shows max_input_vars as 1000
It looks like php use some other php.ini or values are overwritten because with other settings like max_execution_time is the same.
If i'm doing it wrong way? Is this possible that phpinfo shows different configuration file than the one used?
phpinfo output:
As you're using PHP-FPM, you need to check it's configs for overrides, typically in /etc/php-fpm/*.conf.
php_admin_value[max_input_vars] = XXX
After validating there are no overrides that affect your settings, restart the php-fpm service and as a best-practice also Apache.
systemctl restart php-fpm httpd
Restarting the PHP-FPM service is required because Apache passes requests to the running PHP instance(s) loaded into memory from the PHP-FPM service. As PHP is already loaded into memory by PHP-FPM, the PHP-FPM service needs to be restarted for PHP configuration changes to be applied.
Apache prior to PHP-FPM typically relied on starting the PHP process using the Apache mod_php, thereby requiring the restarting of the Apache service for configuration changes to be applied immediately, or until mod_php reloaded the PHP instance.
Based on your configuration, you should add additional PHP config settings to /etc/php.d/zzz-custom.ini instead of /etc/php.ini. PHP will load the config files in alphabetical order, resulting in the zzz-custom.ini file being loaded last and be used as the final values of the PHP settings.
Using the /etc/php.d/zzz-custom.ini file will also prevent the loss of your custom php.ini settings and the need to make extensive changes to the default /etc/php.ini file when updating PHP versions.
However, /etc/php-fpm/*.conf settings will take precedent over any /etc/php.d/*.ini configs.
first: Method is edit the PHP. ini file
Locate your PHP. ini file. ...
If you find your existing PHP. ini, open the file and locate the following line of code (xx represents a number): max_input_vars = xx ; And set it to your desired limit. ...
If you created your own PHP. ...
Save your changes, and reboot your localhost or your server.
I have the folowing settings for my server,
ec2 -> centos7
httpd 2.4
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
php 7 ->
php.ini
variables_order=EGPCS
and an ENV var set in centos with
export myTestDir=/home/myuser
[myUser#.....]$ echo $myTestDir
/home/myuser
however I cannot get any system ENV var to be available in php.
echo exec('whoami'); // returns -> myUser
$_SESSION['test1']=$_SERVER['myTestDir']; // returns -> null
$_SESSION['test2']=$_ENV['myTestDir']; // returns -> null
$_SESSION['test3']=getenv("myTestDir"); // returns -> false
Am i missing an additional step or doing something wrong with variables_order=EGPCS
PHP's environment is isolated from the user environment you're using in your shell. This is by design, because PHP requires that you explicitly import variables into its environment. It will not do that implicitly since that would likely be a huge security risk.
From the manual
These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables.
So in order to load the environment variables into PHP you must do so at the process level that loads the PHP interpreter. For example, in Apache httpd, using mod_php, this would be done by specifying the SetEnv directives in your apache.conf or virtual host files.
For PHP-FPM this would be done in your pool config files like env[myTestDir] = "/home/myuser"
I have an index.php file that is running some script making mysql requests. For obvious security reason, i'd like to remove the mysql credentials (host, password, user, database) from this script and replace them with some apache environment variables.
I tried to create such variables in /etc/apache2/envvars using following line of code : export MYSQL_USER='my_user' and then I intend to get it back using getEnv php function like this : getenv('MYSQL_USER') but this returns nothing.
Any idea ? Thanks !
Four steps :
in /etc/apache2/envvars : export MYVAR='value'
in /etc/apache2/apache2.conf : PassEnv MYVAR
Restart apache
in the php file located wherever apache2 is running: echo $_SERVER['MYVAR'];
In the virtual host for Apache you could use
SetEnv VARIABLE_NAME value
and the get the value of this variable from your PHP code using
$variable = getenv('VARIABLE_NAME');
You can read more on SetEnv here http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv
If you would like to store passwords in an environment variable, but you don't want other users to be able to read the password or key, you could follow the following steps (tested on Ubuntu 18.04):
Create a file /etc/apache2/conf-available/aws_key.conf
Change the ownership to root sudo chown root:root /etc/apache2/conf-available/aws_key.conf
Change the file permission, so it is only readable for root sudo chmod 600 /etc/apache2/conf-available/aws_key.conf
Put the password or key in the file, e.g. SetEnv AWS_SECRET_ACCESS_KEY abcdefg12345+-987654321
Enable the configuration file sudo a2enconf aws_key
Restart Apache sudo service apache2 restart
This key is accessible in PHP via $_SERVER['AWS_SECRET_ACCESS_KEY'].
On a Ubuntu web server [LAMP], I'm trying to get PHP errors to write into a php_error file, but no matter what I do, they keep going to the apache log.
Here's what I have tried/done:
Edited the php.ini file:
error_reporting = E_ALL | E_STRICT
display_error = Off
log_errors = On
error_log = /var/log/php_errors.log
Restarted Apache
Checked the phpinfo() output to verify that the changes I made to the php.ini file took -- they did.
After verifying that the errors were still going to the Apache log, I physically created the php_errors.log and tried again. Still going to the Apache log!
Rebooted the web server! Still...!
Anybody have a solution?
Check the directory permissions for /var/log. Ensure that the user your Web service is running as has write permissions to that folder. Alternatively, create a subfolder (/var/log/phplogs?) and assign explicit permissions on that for the user in question then change the error_log value to be a file in that folder
It had to do with ownership. One or the other has worked [for reasons that are not clear to me]:
chown www-data:www-data /var/log/php_error.log
chown same-user-as-www-home:same-user-as-web-home /var/log/php_error.log
Also, the following has made a difference:
chmod 664 /var/log/php_error.log
[as opposed to chmod 644...again for reasons that are not clear to me]
For the record, Ubuntu uses AppArmor and it limits what Apache does.
Additionally, to change the permissions to the file and editing the php.ini file, you must do the next steps:
Go to the next file
/etc/apparmor.d/abstractions
Edit this file
apache2-common
Add the next line
/var/log/php_errors.log rw,
where rw means the process could read and write this file
or you could also do:
sudo nano /etc/apparmor.d/abstractions/apache2-common
And finally, reload the configuration of apparmor
systemctl reload apparmor
Note: Centos/Redhat/Oracle Linux uses SELinux and it requires the same step but the configuration is different.
I have been busy setting up my own VPS after being used to cPanel, but I can't seem to find out how to let PHP create an error_log file in the same directory as the script that throws the errors.
I would like this to happen without me having to add a line of code to each .php file. In cPanel this works out of the box somehow.
Example:
Error in: /var/www/webapp1/index.php
Logfile location: /var/www/webapp1/error_log
Error in: /var/www/info/system/test.php
Logfile location: /var/www/info/system/error_log
Basically, I want PHP to store an error_log file in each directory for the scripts in that directory.
Additional information:
Single VPS account
Debian 6.0 (Squeeze) GNU/Linux
Apache 2.2.16
Set the error_log value to the name of the error log you want to appear in the directory, but do not put any slashes. The file will be saved in the directory from which the script is ran, so the same directory.
error_log = "php_error.log"
For this, there is the error_log directive in php.ini like:
error_log string
Where string represents the name of the file where script errors should be logged. The file should be writable by the web server's user. If the special value syslog is used, the errors are sent to the system logger instead. On Unix, this means syslog(3) and on Windows NT it means the event log. The system logger is not supported on Windows 95. See also: syslog(). If this directive is not set, errors are sent to the SAPI error logger. For example, it is an error log in Apache or stderr in CLI.
Edit your php.ini file and uncomment the line with error_log:
error_log = php_errors.log
Save the changes, restart Apache, and be happy.
If you're using Linux, open a terminal and type this to restart:
sudo services apache2 restart
If you have access to WebHost Manager (WHM) you can search for SERVICES and then restart HTTPD or Apache.
If you're using EasyPHP or Vertrigo, you can do that through the program it self.
basically I want php to store an error_log file in each directory for the scripts in that directory
Assuming you're using Apache, you can use Apache's error_log directive in the VirtualHost to accomplish this behaviour. If memory serves me right, PHP itself doesn't decide where it stores its errors.