The configuration file

The configuration file (php.ini) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.

php.ini is searched for in these locations (in order):

  • SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
  • The PHPRC environment variable. Before PHP 5.2.0, this was checked after the registry key mentioned below.
  • As of PHP 5.2.0, the location of the php.ini file can be set for different versions of PHP. The following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z], [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x], where x, y and z mean the PHP major, minor and release versions. If there is a value for IniFilePath in any of these keys, the first one found will be used as the location of the php.ini (Windows only).
  • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP], value of IniFilePath (Windows only).
  • Current working directory (except CLI).
  • The web server's directory (for SAPI modules), or directory of PHP (otherwise in Windows).
  • Windows directory (C:\windows or C:\winnt) (for Windows), or --with-config-file-path compile time option.

If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini), it is used instead of php.ini. The SAPI name can be determined with php_sapi_name().

Note:

The Apache web server changes the directory to root at startup, causing PHP to attempt to read php.ini from the root filesystem if it exists.

Using environment variables can be used in php.ini as shown below.

Example #1 php.ini Environment Variables

; PHP_MEMORY_LIMIT is taken from environment
memory_limit = ${PHP_MEMORY_LIMIT}

The php.ini directives handled by extensions are documented on the respective pages of the extensions themselves. A list of the core directives is available in the appendix. Not all PHP directives are necessarily documented in this manual: for a complete list of directives available in your PHP version, please read your well commented php.ini file. Alternatively, you may find » the latest php.ini from Git helpful too.

Example #2 php.ini example

; any text on a line after an unquoted semicolon (;) is ignored
[php] ; section markers (text within square brackets) are also ignored
; Boolean values can be set to either:
;    true, on, yes
; or false, off, no, none
register_globals = off
track_errors = yes

; you can enclose strings in double-quotes
include_path = ".:/usr/local/lib/php"

; backslashes are treated the same as any other character
include_path = ".;c:\php\lib"

Since PHP 5.1.0, it is possible to refer to existing .ini variables from within .ini files. Example: open_basedir = ${open_basedir} ":/new/dir".

Коментарии

Автор:
For someone who's also wondering.

PHP can work even if there is no configuration file(php.ini) loaded,
it will simply applies the default values to directives.
2021-08-12 07:00:41
http://php5.kiev.ua/manual/ru/configuration.file.html
Автор:
Notice that `error_reporting` CANNOT be interpolated with an environment variable (e.g. `error_reporting = ${PHP_ERROR_REPORTING}`).

`error_reporting` is treated differently than other directives:
if assigned an environment variable, this will be silently ignored and replaced with value `0` (aka no-reporting).

I couldn't find documentation about it.
Is maybe an info that should be added in https://github.com/php/php-src/blob/8f5156fcba9906664ecd97e4c279ee980e522121/php.ini-production#L451-L500 ?

I am not aware if this specific behavior affects only `error_reporting` or also other directive.
2023-11-27 23:12:12
http://php5.kiev.ua/manual/ru/configuration.file.html
For anyone who wants to ignore any directory entirely

On a linux Redhat distro (this was on EL9):

; if using php-fpm, put this file in place"
/etc/systemd/system/php-fpm.service.d/override.conf 
[Service]
Environment="PHP_INI_SCAN_DIR="

; do a systemd config reload:
systemctl daemon-reload

; if you want to make sure php on the cli fails if it's not passing 'PHP_INI_SCAN_DIR= php' (this should also affect both linux normal+system users?) then drop this file in place:
cat /etc/php.d/00-dont-use-phpd.ini
auto_prepend_file=MISSING_ENV__PHP_INI_SCAN_DIR__SEE_ETC_PHPD_00_DONT_USE_PHPD_INI
; don't allow php.d directory to load in files
; you must pass 'PHP_INI_SCAN_DIR= ' before your php command on the cli
; reference:
; https://www.php.net/manual/en/configuration.file.php#configuration.file.scan
2023-12-08 21:32:11
http://php5.kiev.ua/manual/ru/configuration.file.html
I'm not sure why the comment saying you can use constants is downvoted so much.  This is amazing news and just what I was looking for.

Having a configuration like:
php_admin_value[error_log] = "/logfolder/php-fpm-"PHP_VERSION".$pool.log"

will nicely log to /logfolder/php-fpm-8.3.10.mypool.log , which is really useful if you have multiple versions of PHP running and want to keep your log files separate and not have to fiddle with the configuration each time you update.  A list of the pre-defined constants is here ( https://www.php.net/manual/en/reserved.constants.php#reserved.constants.core )  The PHP_*_VERSION, PHP_VERSION, PHP_OS_FAMILY, and maybe PHP_SAPI are useful in this case.
2024-08-15 23:58:05
http://php5.kiev.ua/manual/ru/configuration.file.html

    Поддержать сайт на родительском проекте КГБ