PHP 日本研討會 2024

設定檔

設定檔(php.ini)會在 PHP 啟動時讀取。對於 PHP 的伺服器模組版本,這只會在網頁伺服器啟動時發生一次。對於 CGICLI 版本,則會在每次調用時發生。

會在以下位置搜尋 php.ini(依序):

  • SAPI 模組特定位置(Apache 2 中的 PHPIniDir 指令,CGI 和 CLI 中的 -c 命令列選項)
  • PHPRC 環境變數。
  • 可以針對不同版本的 PHP 設定 php.ini 檔案的位置。登錄機碼的根目錄取決於已安裝的作業系統和 PHP 的 32 位元或 64 位元。對於 32 位元作業系統上的 32 位元 PHP 或 64 位元作業系統上的 64 位元 PHP,請使用 [(HKEY_LOCAL_MACHINE\SOFTWARE\PHP];對於 64 位元作業系統上的 32 位元 PHP 版本,請改用 [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP]。對於相同位元的安裝,會依序檢查以下登錄機碼:[HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z][HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y][HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x],其中 x、y 和 z 表示 PHP 的主要、次要和發行版本。對於 64 位元作業系統上的 32 位元 PHP 版本,會依序檢查以下登錄機碼:[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y.z][HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x.y][HKEY_LOCAL_MACHINE\SOFTWARE\WOW6421Node\PHP\x],其中 x、y 和 z 表示 PHP 的主要、次要和發行版本。如果這些機碼中有任何一個具有 IniFilePath 的值,則找到的第一個值將用作 php.ini 的位置(僅限 Windows)。
  • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP][HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\PHP]IniFilePath 的值(僅限 Windows)。
  • 目前的工作目錄(CLI 除外)。
  • 網頁伺服器的目錄(適用於 SAPI 模組),或 PHP 的目錄(在 Windows 中則為其他情況)。
  • Windows 目錄(C:\windowsC:\winnt)(適用於 Windows),或 --with-config-file-path 編譯時間選項。

如果 php-SAPI.ini 存在(其中 SAPI 是正在使用的 SAPI,例如,php-cli.iniphp-apache.ini),則會使用它來取代 php.ini。可以使用 php_sapi_name() 來判斷 SAPI 名稱。

注意:

Apache 網頁伺服器會在啟動時將目錄變更為根目錄,導致 PHP 嘗試從根檔案系統讀取 php.ini(如果存在)。

環境變數可以在 php.ini 中的設定值內參照,如下所示。從 PHP 8.3.0 開始,可以指定備用值,當參照的變數未定義時將會使用該值。

範例 #1 php.ini 環境變數

; PHP_MEMORY_LIMIT is taken from environment
memory_limit = ${PHP_MEMORY_LIMIT}
; If PHP_MAX_EXECUTION_TIME is not defined, it will fall back to 30
max_execution_time = ${PHP_MAX_EXECUTION_TIME:-30}

擴充功能處理的 php.ini 指令記錄在擴充功能本身的個別頁面上。附錄中提供了核心指令的清單。並非所有 PHP 指令都記錄在本手冊中:如需在您的 PHP 版本中可用的完整指令清單,請閱讀您已清楚註解的 php.ini 檔案。或者,您也可以找到來自 Git 的 » 最新的 php.ini 也很有幫助。

範例 #2 php.ini 範例

; 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"

可以從 .ini 檔案內參照現有的 .ini 變數。範例:open_basedir = ${open_basedir} ":/new/dir"

掃描目錄

可以在讀取 php.ini 之後,設定 PHP 掃描目錄中的 .ini 檔案。這可以在編譯時透過設定 --with-config-file-scan-dir 選項來完成。然後可以在執行階段透過設定 PHP_INI_SCAN_DIR 環境變數來覆寫掃描目錄。

可以透過以平台特定的路徑分隔符號(Windows、NetWare 和 RISC OS 上為 ;;所有其他平台上為 :;PHP 使用的值可以作為 PATH_SEPARATOR 常數使用)分隔來掃描多個目錄。如果在 PHP_INI_SCAN_DIR 中指定空白目錄,PHP 也會掃描編譯時透過 --with-config-file-scan-dir 指定的目錄。

在每個目錄中,PHP 將會依字母順序掃描所有以 .ini 結尾的檔案。已載入的檔案清單及其順序,可以透過呼叫 php_ini_scanned_files() 或使用 --ini 選項執行 PHP 來取得。

Assuming PHP is configured with --with-config-file-scan-dir=/etc/php.d,
and that the path separator is :...

$ php
  PHP will load all files in /etc/php.d/*.ini as configuration files.

$ PHP_INI_SCAN_DIR=/usr/local/etc/php.d php
  PHP will load all files in /usr/local/etc/php.d/*.ini as
  configuration files.

$ PHP_INI_SCAN_DIR=:/usr/local/etc/php.d php
  PHP will load all files in /etc/php.d/*.ini, then
  /usr/local/etc/php.d/*.ini as configuration files.

$ PHP_INI_SCAN_DIR=/usr/local/etc/php.d: php
  PHP will load all files in /usr/local/etc/php.d/*.ini, then
  /etc/php.d/*.ini as configuration files.
新增註解

使用者貢獻的註解 2 個註解

weili
3 年前
給那些也感到疑惑的人。

即使未載入任何設定檔 (php.ini),PHP 也能正常運作,
它只會將預設值套用至指令。
Pictor13
1 年前
請注意,`error_reporting` 無法使用環境變數插值(例如 `error_reporting = ${PHP_ERROR_REPORTING}`)。

`error_reporting` 的處理方式與其他指令不同
如果指派了環境變數,則會靜默忽略此變數,並以值 `0`(亦即不報告)取代。

我找不到有關它的文件。
是否應該在 https://github.com/php/php-src/blob/8f5156fcba9906664ecd97e4c279ee980e522121/php.ini-production#L451-L500 中加入這項資訊?

我不確定此特定行為是否只影響 `error_reporting` 或其他指令。
To Top