以下是Ubuntu上PHP的安全設置要點:
修改php.ini配置
display_errors = Off
(生產環境關閉詳細錯誤顯示),log_errors = On
,指定錯誤日志路徑error_log=/var/log/php_errors.log
。disable_functions=eval,exec,system,passthru,shell_exec,popen
等。upload_max_filesize=2M
,post_max_size=8M
,file_uploads=On
。open_basedir=/var/www:/tmp
(限制PHP僅能訪問指定目錄)。session.cookie_secure=On
(僅HTTPS傳輸),session.cookie_httponly=On
(禁止JS訪問)。啟用安全模塊
opcache.enable=1
。系統權限與防火墻
www-data
用戶。其他安全措施
sudo apt update && sudo apt upgrade
,修復PHP及系統漏洞。具體配置需根據實際環境調整,修改后重啟Apache/Nginx及PHP服務生效。