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...
설정을 바꾸는 방법
PHP를 아파치 모듈로 실행
PHP를 아파치 모듈로 사용할 때, 아파치 설정 파일(즉 httpd.conf)과 .htaccess 파일에서 설정을 변경할 수 있습니다. 이를 위해서는 "AllowOverride Options"나 "AllowOverride All" 권한이 필요합니다.
몇몇 아파치 지시어는 PHP 설정을 아파치 설정 파일에서 바꿀 수 있게 합니다. 어떤 지시어가 PHP_INI_ALL, PHP_INI_PERDIR, PHP_INI_SYSTEM인지는, php.ini 지시어 목록 부록을 참고하십시오.
지정한 지시어의 값을 설정합니다. PHP_INI_ALL과 PHP_INI_PERDIR 형식 지시어에만 사용할 수 있습니다. 이전 설정 값을 지우려면 값에 none을 사용합니다.
php_value로 논리 값을 설정하지 마십시오. 대신
php_flag(아래 참고)를 사용해야 합니다.
논리값 지시어를 설정합니다. PHP_INI_ALL과 PHP_INI_PERDIR 형식 지시어에만 사용할 수 있습니다.
지정한 지시어의 값을 설정합니다. .htaccess 파일에서 사용할 수 없습니다.
php_admin_value로 설정하는 지시어 형식은 .htaccess나 ini_set()으로 덮어쓸 수 없습니다. 이전 설정 값을 지우려면 값에 none을 사용합니다.
논리값 지시어를 설정합니다. .htaccess 파일에서 사용할 수 없습니다.
php_admin_flag로 설정하는 지시어 형식은 .htaccess로 덮어쓸 수 없습니다.
Example #1 아파치 설정 예제
<IfModule mod_php5.c> php_value include_path ".:/usr/local/lib/php" php_admin_flag safe_mode on </IfModule> <IfModule mod_php4.c> php_value include_path ".:/usr/local/lib/php" php_admin_flag safe_mode on </IfModule>
PHP 상수는 PHP 밖에 존재하지 않습니다. 예를 들어, httpd.conf에서 E_ALL나 E_NOTICE 등의 PHP 상수를 사용할 수 없습니다. error_reporting 지시어에 이런 설정을 하면, 아무런 의미를 가지지 않고 0으로 평가됩니다. 대신 할당된 비트마스크 값을 사용하십시오. 이런 상수는 php.ini에서는 사용할 수 있습니다.
윈도우 레지스트리에서 PHP 설정 바꾸기
PHP를 윈도우에서 실행할 때, 설정값은 윈도우 레지스트리를 사용하여 디렉토리 기반으로 변경할 수 있습니다. 설정값은 레지스트리 키 HKLM\SOFTWARE\PHP\Per Directory Values에 저장되며, 보조키는 경로명에 해당합니다. 예를 들어, c:\inetpub\wwwroot 디렉토리를 위한 설정값은 HKLM\SOFTWARE\PHP\Per Directory Values\c\inetpub\wwwroot 키에 저장됩니다. 디렉토리 설정은 이 디렉토리와 그 밑의 서브디렉토리에서 실행하는 모든 스크립트에 적용됩니다. 키 아래 값은 PHP 설정 지시어 이름을 가지고 문자열 값이여야 합니다. 값에 사용하는 PHP 상수는 해석되지 않습니다. 그러나, PHP_INI_USER로 바꿀 수 있는 설정값만 이 방법을 사용할 수 있습니다. PHP_INI_PERDIR 값은 사용할 수 없습니다.
It is not possible to unset a config option using php_value. This caused me problems with auto_prepend_file settings where I wanted to have a global file auto included, with an exception for only one site. The solution used to be to use auto_prepend_file /dev/null, but this now causes errors, so I just create and include blank.inc now instead.
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.
@ pgl: As the documentation says:
"To clear a previously set value use none as the value."
Works fine for me.
PHP Constants will work with php_value; for example:
php_value error_reporting 30711 #bitmask is the same as
php_value error_reporting "E_ALL & ~E_STRICT & ~E_NOTICE" #string of constants
Note that, changing the PHP configuration via the windows registry will set the new values using php_admin_value. This makes that you cannot override them on runtime.
So for example, if you set the include_path on the windows registry and then you call the set_include_path function in your application, it will return false and won't change the include_path.
Just a quick note regarding per-directory php settings. A number of flags can be set custom to a particular app's requirement based on one of two methods describe her http://www.cognitivecombine.com/?p=207
I particularly stumbled upon this to turn off register_globals. Drupal, for e.g., required this to be off but I knew of numerous php apps that had code which relied on these globals. The .htaccess method described in the above URL worked for my case.
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
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).
It's worth changing the PHPIniDir setting in httpd.conf setting to eg.:
Configuration File /usr/local/php5/lib/php.ini
so you know exactly which php.ini is being used at all times.
phpinfo() will then advise you that it's using this file.
Open php.ini from c:/php in your text editor
If you installed Apache in the default location, the path should
be "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs". If you installed
it elsewhere, find the htdocs folder and type its path:
doc_root = "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs"
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. We
want 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:
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 Apache
installation folder (by default, C:\Program Files\Apache Software
Foundation\Apache2.2\conf), and select the httpd.conf file located there. In order
to make this file visible for selection, you’ll need to select All Files (*.*) from the
file type drop-down menu at the bottom of the Open window.
Look for the existing line in this file that begins with DirectoryIndex, shown
This line tells Apache which filenames to use when it looks for the default page
for a given directory. Add index.php to the end of this line:
DirectoryIndex index.html index.php
All of the remaining options in this long and intimidating configuration file
should have been set up correctly by the Apache install program. All you need
to do is add the following lines to the very end of the file:
LoadModule php5_module "C:/PHP/php5apache2_2.dll"
AddType application/x-httpd-php .php
Make sure the LoadModule and PHPIniDir lines point to your PHP installation
directory, and note the use of forward slashes (/) instead of backslashes (\) in
Save your changes and Restart Apache using the Apache Service Monitor system tray icon. If all is
well, Apache will start up again without complaint.
x64 based Windows registry path is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\PHP\Per Directory Values
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.
Once again, it can be very boring! But it does work.
I've found this directive useful for setting per-file php.ini options. For example, when I want to have my .css styles processed as php scripts, I put this code into .htaccess to setup correct mimetype.
AddHandler php5-script .css
php_value default_mimetype "text/css"
To change the configuration for php running as cgi those handy module commands won't work.. The work-around is being able to tell php to start with a custom php.ini file.. configured the way you want.
With multiple custom php.ini files
The trick is creating a wrapper script to set the location of the php.ini file that php will use. Then it exec's the php cgi.
shell script /cgi-bin/phpini.cgi
Now all you have to do is setup Apache to run php files through the wrapper script instead of just executing the php cgi.
In your .htaccess or httpd.conf file
AddHandler php-cgi .php
Action php-cgi /cgi-bin/phpini.cgi
So to change the configuration of php you just need to change the PHPRC variable to point to a different directory containing your customized php.ini.. You could also create multiple shell wrapper scripts and create multiple Handler's+Actions in .htaccess..
in your .htaccess
AddHandler php-cgi1 .php1
Action php-cgi1 /cgi-bin/phpini-1.cgi
AddHandler php-cgi2 .php2
Action php-cgi2 /cgi-bin/phpini-2.cgi
AddHandler php-cgi3 .php3
Action php-cgi3 /cgi-bin/phpini-3.cgi
The only caveat here is that it seems like you would have to rename the file extensions, but there are ways around that too ->