| 指示詞 | 預設 | 生產建議 |
| display_errors | On | Off(隱藏敏感資訊) |
| display_startup_errors | Off | Off |
| log_errors | Off | On |
| error_log | 空 | /var/log/php/error.log(需可寫) |
| error_reporting | E_ALL & ~E_NOTICE | E_ALL & ~E_DEPRECATED & ~E_STRICT |
資源限制
| 指示詞 | 預設 | 建議調整 |
| memory_limit | 128M | 視專案需求 256M~512M |
| max_execution_time | 30 | Web 30~60,CLI 0(無限制) |
| max_input_time | 60 | 大檔上傳需調大 |
| post_max_size | 8M | 配合 upload_max_filesize |
| upload_max_filesize | 2M | 業務需求調整 |
Session 設定
| 指示詞 | 預設 | 說明 |
| session.save_handler | files | 可改 memcached/redis |
| session.save_path | /tmp | 檔案模式建議 N;/path 分層 |
| session.gc_maxlifetime | 1440 | 秒,配合 gc_probability/divisor |
| session.cookie_httponly | Off | On 防 XSS 竊取 Cookie |
| session.cookie_secure | Off | HTTPS 環境必開 |
| session.use_strict_mode | Off | On 防 Session Fixation |
OPcache 效能加速
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.revalidate_freq=60 ; 生產環境 60 秒檢查一次
opcache.fast_shutdown=1
opcache.validate_timestamps=1 ; 開發環境設 0 即時生效
安全相關
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
disable_classes =
open_basedir = /var/www/:/tmp/ 限制檔案存取範圍
file_uploads = On(需上傳功能)
allow_url_fopen = Off(禁止遠程 fopen,改用 cURL)
allow_url_include = Off
環境差異化管理
建議維護三份設定:
php.ini.development:錯誤顯示開啟、OPcache 關閉、無 open_basedir
php.ini.staging:接近生產,錯誤記錄、OPcache 開啟
php.ini.production:全安全、效能最佳化
部署時透過 CI/CD 複製對應版本或使用環境變數覆蓋關鍵值。
copyright ©
萬盛學電腦網 all rights reserved