SunshinePHP 2020


Apache モジュールとして PHP を実行している場合

PHP を Apache モジュールとして使用している場合、Apache 設定ファイル (例、httpd.conf) もしくは .htaccess ファイルにディレクティブを記述することで、PHP の設定の変更を行うことが 可能です。このようにして設定変更を行うには、"AllowOverride Options" もしくは "AllowOverride All" 権限が必要です。

Apache 設定ファイルから PHP の設定を変更するには、 以下に示す Apache ディレクティブを使用します。 各設定オプションの変更の可否 (PHP_INI_ALL, PHP_INI_PERDIR, または PHP_INI_SYSTEM) については、付録 php.ini ディレクティブのリスト を参照してください。

php_value name value

指定した設定オプションに値を設定します。変更の可否が、 PHP_INI_ALL もしくは PHP_INI_PERDIR である設定オプションに対し利用できます。 セット済みの値をクリアしたい場合は、none を 値として使用してください。

注意: 論理値を設定する場合には php_value を使用しないでください。代わりに、php_flag (下記参照)を使用する必要があります。

php_flag name on|off

設定オプションに論理値を設定するために使用します。変更の可否が、 PHP_INI_ALL もしくは PHP_INI_PERDIR である設定オプションで利用できます。

php_admin_value name value

指定した設定オプションに値を設定します。このディレクティブは、.htaccess ファイルでは利用できません。また、 php_admin_value で設定された設定オプションの値は、.htaccessini_set() では上書きできません。 セット済みの値をクリアしたい場合は、none を値として使用してください。

php_admin_flag name on|off

設定オプションに論理値を設定するために使用します。 このディレクティブは、.htaccess ファイルでは利用できません。 php_admin_value で設定された設定オプションの値は、.htaccessini_set() では上書きできません。

例1 Apache 設定の例

<IfModule mod_php5.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on


PHP 定数は PHP 以外では使用できません。たとえば、 httpd.conf の中で error_reporting オプションを設定しようとして E_ALLE_NOTICE のような PHP 定数を使用することは できません。これらは意味を有さないため、 0 と評価されてしまいます。 代わりに、対応するビットマスク値を使用してください。 php.ini の中では、これらの PHP 定数を使用することができます。

Windows レジストリによる PHP の設定の変更

Windows 上で PHP を実行している場合、Windows レジストリを使用して設定値を ディレクトリ毎に変更することができます。 設定値は、レジストリキー 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 である設定値は設定できません。


どのように PHP を実行しているかに係わらず、ini_set() 関数を 用いて、スクリプトの実行時に一部のオプションの設定値を変更することができます。詳細は、 ini_set() 関数のリファレンスを参照してください。

使用しているシステムにおける現在のオプション設定値の完全なリストを得たい場合は、 phpinfo() 関数を実行し、出力された結果を参照してください。 また、ini_get() 関数または get_cfg_var() 関数を用いて、個々のオプションの設定値にアクセスすることも可能です。

add a note add a note

User Contributed Notes 13 notes

contrees.du.reve at gmail dot com
11 years ago
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...
nick at vistaworks dot net
7 years ago
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.
5 years ago
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.
12 years ago
@ pgl: As the documentation says:

"To clear a previously set value use none as the value."

Works fine for me.
ohcc at 163 dot com
6 years ago
You will fail if you try to change configuration settings of PHP with   the php_value directive in Apache 2.4.
user at NOSPAM dot example dot com
8 years ago
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
rohitkumar at cftechno dot com
8 years ago
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
<IfModule dir_module>
DirectoryIndex index.html

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:
<IfModule dir_module>
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
PHPIniDir "C:/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
the paths.

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.
andy at ddtechnologies dot com
5 years ago
Registry Key Handling Through PHP:

This is an ready to use code. You just need to copy this code into a PHP file.

//php Code starts from here......

//Reading the Registry

$Wshshell= new COM('WScript.Shell');
$data= $Wshshell->regRead('HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\path');
echo "Data is::".$data;

//Function for writing to the registry.

function registry_write($folder, $key, $value, $type="REG_SZ")
  $WshShell = new COM("WScript.Shell");

  $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
    $result = $WshShell->RegWrite($registry, $value, $type);
    echo "Entry is Successfully written at:".$registry;
  catch(Exception $e){
    echo "Some Exception in Registry writing".$e;

  return false;

// Function to deleting from the Register Entry.

function registry_delete($folder, $key, $value, $type="REG_SZ")
    $WshShell = new COM("");
    $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
    $result = $WshShell->RegDelete($registry);
    echo $key." is Successfully deleted from HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder ;
    catch(Exception $e){
        echo "Some Exception with the code::".$e;
    return false;

//Here 7-ZIP is taken as a example.

$folder = "7-ZIP";
$key = "Key_name";
$value = "Key_Value";


hyponiq at gmail dot com
10 years ago
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:  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.
cawoodm at gmail dot com
10 years ago
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.
8 years ago
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 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.
abu dot ztoko1 at gmail dot com
2 years ago
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.
self at pabloviquez dot com
10 years ago
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.
To Top