以下是LNMP環境的安全設置要點,涵蓋系統、組件及運維層面:
webuser)運行Nginx、PHP-FPM,禁止使用root用戶。useradd -r -s /sbin/nologin webuser創建非登錄用戶。ufw或firewalld限制端口,僅開放SSH(22)、HTTP(80)、HTTPS(443)等必要端口。sudo ufw allow 22,80,443。sudo apt update && sudo apt upgrade -y。server_tokens off;。autoindex off;。allow 192.168.1.0/24; deny all;。if ($request_method !~ ^(GET|POST|HEAD)$) { return 405; }。return 301 https://$host$request_uri;。ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';。add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";。limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;。add_header X-Frame-Options "SAMEORIGIN";。CREATE USER 'webuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT SELECT, INSERT, UPDATE ON dbname.* TO 'webuser'@'localhost';
/etc/mysql/my.cnf,設置bind-address = 127.0.0.1。my.cnf中配置ssl-ca、ssl-cert、ssl-key。mysqldump備份數據并測試恢復。php.ini中禁用exec、system等高危函數:disable_functions = exec,passthru,shell_exec。chown -R webuser:webuser /var/www/html,文件權限設為640,目錄設為750。session.cookie_httponly和session.cookie_secure,防止XSS和會話劫持。logrotate定期輪轉日志,避免占滿磁盤。fail2ban防止暴力破解:sudo apt install fail2ban,配置針對Nginx和SSH的防護規則。rkhunter或lynis檢測系統漏洞。/etc/nginx/、/etc/mysql/、/etc/php/等目錄。參考來源:
通過以上步驟可顯著提升LNMP環境的安全性,需定期審查配置并更新安全策略。