php 7 $_ENV && getenv() empty with EGPCS set - php

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"

Related

Is it possible have more than one version of php on the same VirtualHost instance? [duplicate]

This question already has an answer here:
Multiple versions of PHP-FPM, installing and configuring...?
(1 answer)
Closed 2 years ago.
I've been looking at PHP-FPM recently but I've failed find examples that show more than one version of php being employed to serve different files on the same host instance. Is it possible?
Yes, you can run multiple versions of PHP (PHP-FPM is very possible too) on the same virtual host. But, you have to name the extensions of the files of each of the versions.
You would need to run each of the the PHP-FPM versions on diffrent ports, by adding different ports for the listen directive in each of the www.conf: For example, listen 127.0.0.1:9000 for one of the php versions, and listen 127.0.0.1:1234
For example, you would want to name the file extension of the files which want to run PHP 7.4 as .php74, and for another example, you would want to name the file extension as php80 for the file which want to run PHP 8.0.
How can I acheive this:
Install php-fpm, and then inside www.conf for php 7.4 for example, you should set security_limit_extensions as security.limit_extensions = .php74.
And for php-8.0's php-fpm, you would want to set it to .php80.
And finally, if you are using Apache, you can add this to use the appropriate version for the appropriate files only (I am not a Nginx user, so, good luck finding info about that):
<FilesMatch "\.php74$">
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
For PHP 8.0 now:
<FilesMatch "\.php80$">
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>
See PHP-FPM's manual, and Apache's documentation about mod_proxy_fcgi.

Set PHP limits in Virtual Host FPM/FastCGI

I am trying to create individual setting of PHP (like memory_limit..) for Virtual Hosts But I dont know how could I do that.
I am using Fedora 27 deamon httpd, VHosts works good. When I put SetEnv PHPRC /var/www/pokus.cz/php.ini it doesnt work ( Yes I reloaded deamon).
I try just write php_value memory_limit 64M to VHost.conf file but it prints error
Invalid command "php_value"
I Think that, it is because there is no mod_php in Fedora 27 httpd, but it uses FPM/FastCGI. How could I solve that, Thanks alot for any advise :)
If you want to set individual PHP parameters to your VHosts on server and there is PHP-FPM running on it, you could specify it by different FPM pools.
Just copy the default FPM pool called www (etc/php-fpm.d/www.conf) and edit:
name parameter (specify new socket name) and
listen parameter (specify new socket name, which will VHost run)
(You can edit the php parameters there, like memory_limit...)
Now you have to add this to your VHost conf file:
<FilesMatch \.(php|phar)$>
SetHandler "proxy:unix:/run/php-fpm/yourchoosenname.sock|fcgi://localhost"
</FilesMatch>
This will start your php scripts on new FPM pool for this specific VHost.
Dont forget to reload your web server (I use Apache, so systemctl reload httpd) and PHP-FPM.

SetEnv httpd for use in PHP

I'm attempting to set an environmental variable in Apache for later use in PHP. I'm doing this by including a file in /etc/httpd/conf.d/, which holds these contents:
SetEnv ENVIRONMENT "dev"
I've also added this line to httpd.conf file for testing:
SetEnv BATCHPATH /foo:/bar
Neither of these variables appear in PHP via $_SERVER or $_ENV, and I do not see them using getenv("ENVIRONMENT").
I'm running Centos 7, have the env_module listed as (shared) under httpd -M, but also have mod_suexec listed there as well, which I've read may be overriding any user variables.
Any help is appreciated.

Silently install Apache with php and files

I am writing a C++ installer and want to have it perform the following tasks without user interaction:
Silently install an instance of Apache
Silently add PHP to said apache instance
Determine the DocumentRoot of the apache instance and copy a bunch of php files to that location
Currently I have it installing apache using this command: apache_2.2.6-win32-x86-no_ssl.msi /passive ALLUSERS=1 SERVERADMIN=admin#localhost SERVERNAME=localhost SERVERDOMAIN=localhost SERVERPORT=80. This silently installs apache with the specified settings and leaves the services automatically started. Perfect.
The problem is that after this I don't know how to get the DocumentRoot that Apache uses (it is by default C:\Program Files (x86)\Apache Software Foundation\Apache\htdocs on my test computer), so I don't know where to send the files. So I either need to:
Control the location of htdocs (preferable)
Find a way to dynamically grab the DocumentRoot
Thanks!
You'll need to parse the Apache/conf/httpd.conf file then change the DocumentRoot value to what location you want him to point at.
Find this
DocumentRoot ".*"
Change it to
DocumentRoot "Your Location"
To find the location of the httpd.conf knowing that the default installation folder of apache is Programe Files ...
Using the win 32 API
TCHAR pf[MAX_PATH];
SHGetSpecialFolderPath(
0,
pf,
CSIDL_PROGRAM_FILES,
FALSE );
http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx
Then add \Apache Software Foundation\Apache\conf\httpd.conf to the program files folder to locate the httpd.conf file

PHP $_ENV vs Linux environment (Ubuntu)

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.

Categories