How to change configuration settings

Running PHP as an Apache module

When using PHP as an Apache module, you can also change the
configuration settings using directives in Apache configuration
files (e.g. httpd.conf) and .htaccess files. You will need
"AllowOverride Options" or "AllowOverride All" privileges to do so.

There are several Apache directives that allow you
to change the PHP configuration from within the Apache configuration
files. For a listing of which directives are
PHP_INI_ALL, PHP_INI_PERDIR,
or PHP_INI_SYSTEM, have a look at the
List of php.ini directives appendix.

php_valuenamevalue

Sets the value of the specified directive.
Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.
To clear a previously set value use none as the value.

Note:
Don't use php_value to set boolean values.
php_flag (see below) should be used instead.

php_flagnameon|off

Used to set a boolean configuration directive.
Can be used only with PHP_INI_ALL and
PHP_INI_PERDIR type directives.

php_admin_valuenamevalue

Sets the value of the specified directive.
This can not be used in .htaccess files.
Any directive type set with php_admin_value
can not be overridden by .htaccess or ini_set().
To clear a previously set value use none as the value.

php_admin_flagnameon|off

Used to set a boolean configuration directive.
This can not be used in .htaccess files.
Any directive type set with php_admin_flag
can not be overridden by .htaccess or ini_set().

PHP constants do not exist outside of PHP. For example, in
httpd.conf you can not use PHP constants
such as E_ALL or E_NOTICE
to set the error_reporting
directive as they will have no meaning and will evaluate to
0. Use the associated bitmask values instead.
These constants can be used in php.ini

Changing PHP configuration via the Windows registry

When running PHP on Windows, the configuration values can be
modified on a per-directory basis using the Windows registry. The
configuration values are stored in the registry key
HKLM\SOFTWARE\PHP\Per Directory Values,
in the sub-keys corresponding to the path names. For example, configuration
values for the directory c:\inetpub\wwwroot would
be stored in the key HKLM\SOFTWARE\PHP\Per Directory
Values\c\inetpub\wwwroot. The settings for the
directory would be active for any script running from this
directory or any subdirectory of it. The values under the key
should have the name of the PHP configuration directive and the
string value. PHP constants in the values are not parsed.
However, only configuration values changeable in
PHP_INI_USER can be set
this way, PHP_INI_PERDIR values can not.

Other interfaces to PHP

Regardless of how you run PHP, you can change certain values at runtime
of your scripts through ini_set(). See the documentation
on the ini_set() page for more information.

If you are interested in a complete list of configuration settings
on your system with their current values, you can execute the
phpinfo() function, and review the resulting
page. You can also access the values of individual configuration
directives at runtime using ini_get() or
get_cfg_var().

User Contributed Notes 13 notes

Being able to put php directives in httpd.conf and have them work on a per-directory or per-vitual host basis is just great. Now there's another aspect which might be worth being aware of:

A php.ini directive put into your apache conf file applies to php when it runs as an apache module (i.e. in a web page), but NOT when it runs as CLI (command-line interface).

Such feature that might be unwanted by an unhappy few, but I guess most will find it useful. As far as I'm concerned, I'm really happy that I can use open_basedir in my httpd.conf file, and it restricts the access of web users and sub-admins of my domain, but it does NOT restrict my own command-line php scripts...

On Windows, as the documentation above states, you cannot set max_upload_filesize and post_max_size in the registry, because they are PHP_INI_PERDIR.

Confusingly, however, is the fact that phpinfo() will show your changes if you do add those to the regsitry, as if they were taking effect. However, the upload size is *not* changed, regardless of what phpinfo() reports.

I believe the reading of registry values and override happen 'too late' in the request processing, ie, after the file has already been uploaded and rejected.

In a perfect world, phpinfo() would recognize this and not report the overridden values, which is very confusing.

One of the most serious problems here is that it is hard to determine programmatically which of three or more possible configuration methods (php.ini, .user.ini, .htaccess) should be used on any given hosting company's server.

The worst outcome is if an install.php routine attempts to set .htaccess directives on a cgi-mode server, in which case the outcome is usually a '500' crash. Once in that situation the php installation routine cannot be rerun to correct the problem, so you effectively have a hosting lockout situation which can only be corrected by manual intervention.

If you installed Apache in the default location, the path shouldbe "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs". If you installedit elsewhere, find the htdocs folder and type its path:

Just a little further down in the file, look for the line that begins with remove the semicolon from the start of the extension_dir, and set it so that it points to the ext subfolder of your PHP folder:extension_dir = "C:\PHP\ext"

These are optional extensions to PHP, disabled by default. Wewant to enable the MySQL extension so that PHP can communicate with MySQL.To do this, remove the semicolon from the start of the php_mysqli.dll line:extension=php_mysqli.dll

note - php_mysqli, not php_mysql

Keep scrolling even further down in the file, and look for a line that starts with;session.save_path. Once again, remove the semicolon to enable this line,and set it to your Windows Temp folder:session.save_path = "C:\Windows\Temp"

Browse to the conf subfolder in your Apacheinstallation folder (by default, C:\Program Files\Apache SoftwareFoundation\Apache2.2\conf), and select the httpd.conf file located there. In orderto make this file visible for selection, you’ll need to select All Files (*.*) from thefile type drop-down menu at the bottom of the Open window.Look for the existing line in this file that begins with DirectoryIndex, shownhere:<IfModule dir_module>DirectoryIndex index.html</IfModule>

This line tells Apache which filenames to use when it looks for the default pagefor a given directory. Add index.php to the end of this line:<IfModule dir_module>DirectoryIndex index.html index.php</IfModule>

All of the remaining options in this long and intimidating configuration fileshould have been set up correctly by the Apache install program. All you needto do is add the following lines to the very end of the file:

With IIS it can become confusing that changes to php.ini are ineffective. It seems it is necessary to restart the application pool for the changes to be seen. It would be great if this was not necessary - I am sure I have worked with systems where php.ini changes were immediately effectvie.

Running PHP under Apache poses a major problem when it comes to per-directory configuration settings for PHP. In Apache virtual hosting, only a master PHP configuration file (i.e. php.ini) is parsed at run-time per PHP script. Under IIS 6.0 or greater, you can include per-directory PHP configuration files to override or overwrite the master configuration settings. The issue here, however, is having Apache virtual hosts override/overwrite master settings; not what IIS can do.

So, there are two possible solutions. The first solution is described in this section and uses the Apache configuration settings php_value, php_flag, php_admin_value, and php_admin_flag. In that, each virtual host which you'd like to have certain configuration settings changed must have these directives set (and that is for each PHP configuration setting). This, to me, is the more viable solution, although it is time-consuming and mentally taxing.

The other possible solution is to set the PHPRC environment variable. To my knowledge, all implementations of Apache HTTPD allow for the SetEnv directive to set the PHPRC variable per-virtual-host. What that does is tell PHP to look in the specified location for that virtual host's configuration settings file (i.e. "C:/path/to/custom/php.ini"). The only downside to this tactic is that EVERY virtual host's custom php.ini file must contain all set parameters. In other words, every single PHP configuration directive you have set in the master php.ini file must ALSO be set in per-virtual-host configuration settings. Doesn't that suck? It seems rather redundant to me (and completely defeats the purpose) that you have to include all configuration settings OVER AND OVER AGAIN.

The great thing about per-directory configuration settings (when they're implemented correctly) is that PHP already has the master settings pre-loaded and the per-directory settings (which may only ammount to one directive in difference) can be loaded per request and, thus, there is less over-head.

There really is more to this topic than that, which is why I blogged a rather lengthy and detailed article here: http://hyponiq.blogspot.com/2009/02/apache-php-multiple-phpini.html This information is meant to help users and administrators. I highly suggest it be read if anyone has any questions on Apache and PHP configurations. I have included some examples that illustrate the two possible solutions, as well. I did my best to research everything before I wrote the article.

I hope this helps!

==== 10-FEB-09: ====
I must add a little more information:

I've done some thorough testing on my PC as to the PHPRC environment variable set by the Apache directive SetEnv. It seems to me that this variable is completely disregarded using that directive. I tried everything and can only come to the conclusion that either A) I did something very wrong, or B) that it simply doesn't work as expected.

The former solution, however, does work magically! So, to expand on my previous post, the only real and viable solution to this problem is to use the php_value, php_flag, php_admin_value and php_admin_flag directives in your virtual hosts configurations.

For Mac users (Leopard/Tiger/Lion) it's worth knowing exactly where your httpd.conf and php.ini ares picked up, but, out of the box, these are defined only in the compiler settings as defaults.

The httpd binary was compiled with default settings of SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"meaning starting apache using apachectl picks up this httpd.conf without it being specified on the command line via the org.apache.httpd.plist file is /System/Library/LaunchDaemons.

Again, the default httpd.conf does not contain a PHPIniDir, and locates php.ini via libphp5.so in /etc - you would need to create one here (using one of the php.ini.default templates if you want).

Simply desire to say your article is as astonishing. The clearness in your post is simply spectacular and i can assume you’re an expert on this subject. Well with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please carry on the rewarding work.http://ztoko.net/sandal/