php doc_root in php.ini and DocumentRoot in httpd.conf - php

I wanted to learn more about PHP and Apache so I decided to install them manually. I don't exactly understand how the two files work together (or if they even go hand in hand in this situation). Whenever I load the localhost webpage, the location of the php files are directed from what I specify in the httpd.conf file. I've made two root folders just for the sake of testing, C:/Users/Alex/test and C:\Users\Alex\My Websites. Apache does not actually use the location that I specified in php.ini (doc_root = "C:\Users\Alex\My Websites"), but instead uses the location that I specified in httpd.conf (DocumentRoot "C:/Users/Alex/test" ). Can anyone please explain when is the root useful in php.ini?

Most likely you're running mod_php inside of Apache (this is the most common way to run PHP under Apache). That means that the PHP environment is controlled entirely by Apache (in Unix environments Apache has its own user as well). You can reconfigure it to use Fast CGI (which is the only way to run PHP under other web servers like nginx) and the setting will matter under that type of setup.
Here's the manual entry for the setting
PHP's "root directory" on the server. Only used if non-empty. If PHP
is configured with safe mode, no files outside this directory are
served. If PHP was not compiled with FORCE_REDIRECT, you should set
doc_root if you are running PHP as a CGI under any web server (other
than IIS). The alternative is to use the cgi.force_redirect
configuration below.

Related

How to configure htaccess-like files for configuring PHP when on shared hosting situations or FCGI? [duplicate]

My development environment is shared with other developers of my startup and is setup on Rackspace. The php.ini file is located in /etc/ folder, and I believe this is a centralized location from where every other developer's dev environment setting is being configured from. I want to customize this php.ini file specifically for myself rather than having to do it in the /etc/ location.
Specifically I am setting up XDEBUG in my environment, some other developers don't want it, so I don't want to bug em :)
To do so, I scanned the Internet on how to override the php.ini file specifically for a directory, and found this page on stackoverflow
And following that, I simply copy pasted the php.ini file within my htdocs folder and then simply echoed out phpinfo() (I echoed this in one of my Controllers, (using Zend)). The index.php file is within the htdocs folder.
When I look # "Loaded Configuration File", it still reads
/etc/ instead of ../htdocs/
Anybody know what's up?
In general, it isn't possible to load php.ini on a per directory basis, but in some special cases (CGI/FastCGI), it is: see documentation for Per-user configuration
Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.
If you are hosting several independent sites on one server, you should consider FastCGI anyway, to keep them separated. With php5-fpm it's very easy to setup many pools of workers.
Note that only set a limited subset of the ini-options in the user-ini-file.
As you said you don't have control on the server, the possible work-arounds would be to:
Use ini_set() to override the changes inside your script. Not all of the configuration directives can be changed using ini_set() though.
Use an .htaccess file in your directory to override the configurations in php.ini file.
(certain parts adapted from #1438393)
Hope this helps!
I'm not sure you understood the post. The post means if you run the server and want a per domain php.ini you can run the module as a per domain so each user controls there domain php.ini however it looks like your server does not offer this so you will need to us htaccess file to overwrite the php.ini settings.
By over write this doesn't mean you can change the directory this means maybe add a module or add error reporting ect...
You can do it by using this post: How can I use xdebug to debug only one virtual host?

Overriding PHP.ini in a shared development environment

My development environment is shared with other developers of my startup and is setup on Rackspace. The php.ini file is located in /etc/ folder, and I believe this is a centralized location from where every other developer's dev environment setting is being configured from. I want to customize this php.ini file specifically for myself rather than having to do it in the /etc/ location.
Specifically I am setting up XDEBUG in my environment, some other developers don't want it, so I don't want to bug em :)
To do so, I scanned the Internet on how to override the php.ini file specifically for a directory, and found this page on stackoverflow
And following that, I simply copy pasted the php.ini file within my htdocs folder and then simply echoed out phpinfo() (I echoed this in one of my Controllers, (using Zend)). The index.php file is within the htdocs folder.
When I look # "Loaded Configuration File", it still reads
/etc/ instead of ../htdocs/
Anybody know what's up?
In general, it isn't possible to load php.ini on a per directory basis, but in some special cases (CGI/FastCGI), it is: see documentation for Per-user configuration
Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.
In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). In case the PHP file is outside the document root, only its directory is scanned.
If you are hosting several independent sites on one server, you should consider FastCGI anyway, to keep them separated. With php5-fpm it's very easy to setup many pools of workers.
Note that only set a limited subset of the ini-options in the user-ini-file.
As you said you don't have control on the server, the possible work-arounds would be to:
Use ini_set() to override the changes inside your script. Not all of the configuration directives can be changed using ini_set() though.
Use an .htaccess file in your directory to override the configurations in php.ini file.
(certain parts adapted from #1438393)
Hope this helps!
I'm not sure you understood the post. The post means if you run the server and want a per domain php.ini you can run the module as a per domain so each user controls there domain php.ini however it looks like your server does not offer this so you will need to us htaccess file to overwrite the php.ini settings.
By over write this doesn't mean you can change the directory this means maybe add a module or add error reporting ect...
You can do it by using this post: How can I use xdebug to debug only one virtual host?

sys_get_temp_dir in shared hosting environment

Note: This could also fit in superuser.
I am setting up PHP 5.3.10 on a shared host with apache2 mpm itk and open_basedir in a way, that each user may not see or change the files of another user. In the apache2 vhost settings, I add the appropriate entries to restrict the user:
AssignUserId userA userA
php_admin_value open_basedir /home/userA/www/
php_admin_value upload_tmp_dir /home/userA/www/tmp/
php_admin_value session.save_path /home/userA/www/tmp/
SetEnv TMPDIR /home/userA/www/tmp/
Now, the first line sets the linux user to use for apache2, the next three lines define the basedir, upload directory and session savepath to be in the user directory. I'll get back to the last line in a sec.
Now for the problem: sys_get_temp_dir() should give back the temporary directory for php, which is /tmp be default on a linux system. For security reasons, this directory should reside in the open_basedir of userA. According to the php-source of 5.3.10, the sys_get_temp_dir()-function uses the environment variable TMPDIR to get this directory:
// php-src/main/php_open_temporary_file.c:217-219
/* On Unix use the (usual) TMPDIR environment variable. */
{
char* s = getenv("TMPDIR");
This is what the fifth line in the configuration above should do. However, sys_get_temp_dir() simply returns the global system directory, ignoring the environmental variable (which is perfectly set in $_SERVER, also viewable via phpinfo()).
This results in some nasty bugs with various software relying on sys_get_temp_dir(), as that directory is outside of the open_basedir setting. I've tried to set the variable directly into $_ENV and $_SERVER without a change in behaviour. I've tried a putenv('TMPDIR=/home/userA/www/tmp') without change.
However, I am able to change the output by defining the variable into /etc/apache2/envvars - which is useless for me, as I want each VHOST to have its own temporary folder.
The only solution I have found so far is overwriting the internal sys_get_temp_dir() through an extension like runkit and enforcing its inclusion via auto_prepend_file. But that solution is so dirty, I simply can't believe, that there is no better solution around.
So, my question: Is there any way to change the result of sys_get_temp_dir() to be set in an apache2 vhost setting, without reimplementing the function with runkit?
Edit: The apache version is 2.2.22, and I currently use mod_php. As I will have to add all users manually, an fcgi or similar setup would also be possible.
Running a putenv('TMPDIR=/foo/bar') inside PHP seems to be able to affect the result of sys_get_temp_dir(). You could have an auto_prepend_file directive arranged to run a piece of PHP to set up the TMPDIR and avoid messing with a redefinition of sys_get_temp_dir().
Edit: Also, you could easily use putenv('TMPDIR='.ini_get('open_basedir').'/tmp') to set the temporary directory to the directory structure you laid out in the question.
Funny enough, this turns out to also work (given that you keep the SetEnv TMPDIR /foo/bar in your Apache configuration):
putenv('TMPDIR='.getenv('TMPDIR'));
Seems like a no-op, but actually does have effect on sys_get_temp_dir(). I'm starting to suspect this has to be some environment-handling bug in PHP.
You have tagged your question cgi, however you are making use of
php_admin_value open_basedir /home/userA/www/
^^^^^^^^^^^^^^^
which is a setting for the apache module version of PHP, Mod_PHP. In that case PHP is loaded once the webserver starts.
Then you make use of SetEnv:
SetEnv TMPDIR /home/userA/www/tmp/
this is setting an internal environment variable. It is passed to other apache modules, however I think you need it with the request, not with the virtual server. I don't know it specifically, but I would assume according to your description that this environment variable is getting reset before the PHP script is invoked.
So more a comment than a real answer, but hopefully it helps you clarify some things.
I normally use FCGI for multi-user environments so that I can better separate the users. I never had problems with setting environment variables per each user. But that's just another comment, I don't want to say you have to use it, too. Just to highlight that you need to find out the right place within apache to set the environment variable so it is (still) set when the script is executed.
Also you might not be setting the right environment variable. According to Apache Documentation about environment variables:
Although these variables are referred to as environment variables, they are not the same as the environment variables controlled by the underlying operating system. Instead, these variables are stored and manipulated in an internal Apache structure. They only become actual operating system environment variables when they are provided to CGI scripts and Server Side Include scripts. If you wish to manipulate the operating system environment under which the server itself runs, you must use the standard environment manipulation mechanisms provided by your operating system shell.
You want to set the operating system environment variable for PHP. But you are setting the internal environment variable only.
Mod_PHP might import them to the script, so if you use getenv('TMPDIR') the PHP SAPI specific implementation is used - which does allow you to see those internal environment variables - however the php_get_temporary_directory function is not using it - it looks like.
Please add your Apache and PHP version to your question.
According to this - 4 year old - bug, sys_get_temp_dir() won't work with virtual-hosts; so
you can try to use only libraries that fixed this issue (& open a bug for those who didn't)
or append /tmp (or whatever your OS uses) in your open_basedir, as it can hold multiple directories (like include_path - separate it with ; on Windows, : otherwise)
Looking at the PHP source, sys_get_temp_dir() works with the following priority:
If its value has been calculated before, the cached value is used.
sys_temp_dir is checked in the ini configuration.
On Windows, the GetTempPathW Win32 API method is used, and according to its documentation the following are used (in this order):
The path specified by the TMP environment variable.
The path specified by the TEMP environment variable.
The path specified by the USERPROFILE environment variable.
The Windows directory.
In *nix, the following are used (in this order):
The TMPDIR environment variable.
The P_tmpdir macro
/tmp (according to the source, this is a last-ditch effort that should never happen).
That should give you enough options for controlling the result of sys_get_temp_dir (e.g. ini_set('sys_temp_dir', $tmpPath) or putenv('TMPDIR=/foo/bar') as others mentioned) Unless it was previously calculated, in which case you're SOL as far as I know and the cached value will be used (but I have zero knowledge in PHP so would love to hear otherwise).
This is a bug in php 5.2 - specify temp dir by php.ini
It's fixed in 5.5
Use this as a temporary solution:
<?php
putenv('TMPDIR=/path/to/your/tmp');
...your code here ...
In case people end up here whos Problem is not solved with putenv...
... for me, it worked to set the sys_temp_dir using php's ini_set like this:
$tmpPath = realpath(__DIR__.'/../app/tmp');
ini_set('sys_temp_dir', $tmpPath);
I am running PHP 5.5.9 (cli) on a windows8 machine.
It looks like you can change the value returned by sys_get_temp_dir(), I have just tried on apache 2.4 and php 5.6.27.
Add a sys_temp_dir in the virtualhost:
php_admin_value sys_temp_dir "/var/www/alternc/f/fser/tmp"
Restart apache, and print the value in a web page using sys_get_temp_dir():
<?php
echo sys_get_temp_dir () ;
Produces the expected output: /var/www/alternc/f/fser/tmp.

php.ini: which one?

I moved from my old apache to nginx and php 5.3.10. And when I tried to modify php.ini to suit my needs, I found there are 3 of them:
$ locate php.ini
/etc/php5/cgi/php.ini
/etc/php5/cli/php.ini
/etc/php5/fpm/php.ini
Which one should I edit?
Generally speaking, the cli/php.ini file is used when the PHP binary is called from the command-line.
You can check that running php --ini from the command-line.
fpm/php.ini will be used when PHP is run as FPM -- which is the case with an nginx installation.
And you can check that calling phpinfo() from a php page served by your webserver.
cgi/php.ini, in your situation, will most likely not be used.
Using two distinct php.ini files (one for CLI, and the other one to serve pages from your webserver) is done quite often, and has one main advantages : it allows you to have different configuration values in each case.
Typically, in the php.ini file that's used by the web-server, you'll specify a rather short max_execution_time : web pages should be served fast, and if a page needs more than a few dozen seconds (30 seconds, by default), it's probably because of a bug -- and the page's generation should be stopped.
On the other hand, you can have pretty long scripts launched from your crontab (or by hand), which means the php.ini file that will be used is the one in cli/. For those scripts, you'll specify a much longer max_execution_time in cli/php.ini than you did in fpm/php.ini.
max_execution_time is a common example ; you could do the same with several other configuration directives, of course.
Although Pascal's answer was detailed and informative it failed to mention some key information in the assumption that everyone knows how to use phpinfo()
For those that don't:
Navigate to your webservers root folder such as /var/www/
Within this folder create a text file called info.php
Edit the file and type phpinfo()
Navigate to the file such as: http://www.example.com/info.php
Here you will see the php.ini path under Loaded Configuration File:
Make sure you delete info.php when you are done.
It really depends on the situation, for me its in fpm as I'm using PHP5-FPM. A solution to your problem could be a universal php.ini and then using a symbolic link created like:
ln -s /etc/php5/php.ini php.ini
Then any modifications you make will be in one general .ini file.
This is probably not really the best solution though, you might want to look into modifying some configuration so that you literally use one file, on one location. Not multiple locations hacked together.
You can find what is the php.ini file used:
By add phpinfo() in a php page and display the page (like the picture under)
From the shell, enter: php -i
Next, you can find the information in the Loaded Configuration file (so here it's /user/local/etc/php/php.ini)
Sometimes, you have indicated (none), in this case you just have to put your custom php.ini that you can find here: http://git.php.net/?p=php-src.git;a=blob;f=php.ini-production;hb=HEAD
I hope this answer will help.

Zend Server and auto_prepend_file (or php_value)

On our development PC we need to have a prepend file (loaded via auto_prepend_file in php.ini) to set up severals paths. This prepend.php file is used on all of our server and help us to easily configure all applications.
But the Zend Server 5 GUI (http://localhost:10081/ZendServer/) does not work when auto_prepend_file is set, and Apache does not accept php_value directive, either in htaccess or httpd.conf files (because of FastCGI).
Do you have any idea to have a prepend file for our code, but not for ZS GUI?
Best regards,
Cédric
http://php.net/manual/en/configuration.file.php says:
php.ini is searched in these locations (in order):
. . .
You can review this list and see if one of the techniques helps in your case. For example, you can set the environment variable PHPRC, or you can put a different php.ini file in each current working directory, assuming each virtual host has a distinct cwd.
Note that when using Apache and mod_php, or other module embedding PHP in the web server (e.g. FastCGI), the php.ini file is read once, at web server startup. When you use PHP in a CGI manner, the php.ini file is read during every web request, so you have more opportunity to use a different php.ini

Categories