前言
php.ini 是 PHP 執行引擎的總控制台,正確調校能大幅提升效能、強化安全、簡化除錯。本文針對核心指令分類深度解析,附上生產環境建議值、常見誤區與遷移注意事項(PHP 5.6 → 7.x → 8.x)。
配置檔案結構與語法
- 指令格式:
directive = value(區分大小寫)
- 值型別:字串(引號)、數值、常數、INI 常數、運算式(位運算)
- 布林:
On/Off、1/0、true/false
- 註解:
; 開頭
- 區塊:
[Section](僅供組織,不影響解析)
- 可在
httpd.conf/.user.ini 覆蓋(視 PHP_INI_SYSTEM/PHP_INI_PERDIR/PHP_INI_ALL 模式)
核心指令分類詳解
1. 效能與資源限制
| 指令 | 預設 | 建議生產值 | 說明 |
| memory_limit | 128M | 256M~512M | 單腳本最大記憶體,視業務調整,-1 為無限(危險) |
| max_execution_time | 30 | 30~60 | 秒;CLI 為 0(無限),Web 建議保守防止卡死 |
| max_input_time | 60 | 60~120 | 解析 POST/GET/上傳資料時間 |
| post_max_size | 8M | 20M~100M | 需 > upload_max_filesize |
| upload_max_filesize | 2M | 10M~50M | 單檔上傳上限 |
| realpath_cache_size | 4M | 4M~16M | 路徑解析快取,大型專案調大 |
| realpath_cache_ttl | 120 | 300~600 | 快取存活秒數,檔案少變動可調大 |
| output_buffering | 4096 | 4096~8192 | 輸出緩衝區,允許輸出後送 Header,略增記憶體 |
| implicit_flush | Off | Off | 開啟會嚴重拖慢效能,僅除錯用 |
2. 安全防護
| 指令 | 預設 | 建議生產值 | 說明 |
| expose_php | On | Off | 隱藏 HTTP Header X-Powered-By: PHP/x.y.z |
| display_errors | On | Off | 生產環境嚴禁輸出錯誤到瀏覽器 |
| display_startup_errors | Off | Off | 啟動階段錯誤也不顯示 |
| log_errors | Off | On | 錯誤寫入日誌檔 |
| error_log | 空 | /var/log/php/error.log | 指定日誌路徑,需 www-data 可寫 |
| error_reporting | E_ALL & ~E_NOTICE | E_ALL & ~E_DEPRECATED & ~E_STRICT | 生產建議記錄所有錯誤但不顯示 |
| allow_url_fopen | On | Off | 禁止 file_get_contents('https://...') 遠程讀取 |
| allow_url_include | Off | Off | 嚴禁 include 遠程檔案(極高風險) |
| disable_functions | 空 | exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source,pcntl_exec,dl | 禁用危險函式,視業務保留必要項 |
| disable_classes | 空 | 視業務 | 禁用危險類別 |
| open_basedir | 空 | /var/www/:/tmp/:/usr/share/php/ | 限制檔案存取路徑,防止目錄遍歷 |
| session.cookie_httponly | Off | On | 防 XSS 竊取 Session ID |
| session.cookie_secure | Off | On (僅 HTTPS) | 僅 HTTPS 傳送 Cookie |
| session.use_strict_mode | Off | On | 拒絕未初始化的 Session ID,防固定攻擊 |
3. 除錯與開發
| 指令 | 開發環境 | 生產環境 | 說明 |
| display_errors | On | Off | |
| error_reporting | E_ALL | E_ALL & ~E_DEPRECATED & ~E_STRICT | |
| html_errors | On | Off | 錯誤輸出 HTML 格式 |
| xdebug.mode | develop,debug,profile | Off | 需安裝 Xdebug 擴展 |
| xdebug.client_port | 9003 | - | PHP 8.x + Xdebug 3 |
| zend.exception_ignore_args | Off | On | 例外堆疊不包含參數值,減少記憶體/日誌體積 |
4. 擴展管理
; 動態載入擴展(路徑由 extension_dir 決定)
extension=pdo_mysql
extension=redis
extension=opcache
extension=gd
extension=intl
extension=zip
extension=bcmath
extension=soap
extension=xmlrpc
; Zend 擴展(須指定完整路徑或 zend_extension_ts)
zend_extension=opcache.so
; Windows
; zend_extension=php_opcache.dll
5. OpCache 生產環境黃金配置
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256 ; MB,視專案大小調整
opcache.interned_strings_buffer=16 ; MB
opcache.max_accelerated_files=20000 ; 足夠覆蓋所有 PHP 檔案
opcache.revalidate_freq=0 ; 0=每次檢查(開發),生產設 60~300 或 0 搭配部署腳本重啟
opcache.validate_timestamps=1 ; 生產可設 0,部署後需重啟 PHP-FPM
opcache.save_comments=1 ; 保留註解(Doctrine/Annotation 需要)
opcache.fast_shutdown=1 ; PHP 7.0+ 加速關閉
opcache.jit=1255 ; PHP 8.0+ JIT 編譯(tracing+function),視 CPU 架構測試調整
opcache.jit_buffer_size=128M
6. 日期時區與多國語系
date.timezone = "Asia/Taipei" ; 必須設定,避免警告
mbstring.language = "Traditional Chinese"
mbstring.internal_encoding = "UTF-8"
mbstring.http_input = "pass"
mbstring.http_output = "pass"
mbstring.encoding_translation = Off
mbstring.detect_order = "UTF-8, BIG5, ASCII"
mbstring.func_overload = 0 ; 0=不重載,避免相容性問題
iconv.input_encoding = "UTF-8"
iconv.internal_encoding = "UTF-8"
iconv.output_encoding = "UTF-8"
7. Session 安全與效能
session.save_handler = redis ; 分散式部署必改
session.save_path = "tcp://127.0.0.1:6379?auth=yourpass"
session.gc_probability = 1
session.gc_divisor = 1000 ; 0.1% 機率觸發 GC
session.gc_maxlifetime = 1440 ; 24 分鐘
session.sid_length = 32 ; PHP 7.1+ Session ID 長度
session.sid_bits_per_character = 5 ; 字元熵值
session.cookie_samesite = Lax ; PHP 7.3+ CSRF 防護
環境差異部署策略
| 指令 | 開發 | 測試/Staging | 生產 |
| display_errors | On | Off | Off |
| opcache.validate_timestamps | 1 | 1 | 0 (部署後重啟) |
| error_reporting | E_ALL | E_ALL | E_ALL & ~E_DEPRECATED |
| xdebug | On | Off | Off |
| mail() | log only | real SMTP | real SMTP |
遷移注意事項(PHP 5.6 → 7.x → 8.x)
- 移除指令:
magic_quotes_gpc、register_globals、safe_mode、always_populate_raw_post_data、asp_tags(PHP 5.4/7.0/8.0 已移除)
- 新增指令:
zend.exception_ignore_args、opcache.jit*、session.sid_*、cookie_samesite
- 預設值變更:
error_reporting (PHP 8.0 E_ALL)、default_charset (UTF-8)、mbstring.http_input/output (pass)
- ini 檔案位置:PHP-FPM 可用
php_admin_value[directive] = value 在 pool 設定覆蓋
總結
php.ini 調優無銀彈,但遵循「效能:OpCache + realpath_cache + 適度記憶體」、「安全:關閉暴露、禁用危險函式、open_basedir、Session 硬化」、「除錯:開發開啟、生產記錄」三大原則,並針對版本差異清理廢棄指令、啟用新特性,即可構建穩定高效的 PHP 運行環境。建議將 php.ini 納入版控,配合 CI/CD 驗證關鍵指令,實現基礎設施即代碼。