以下是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环境的安全性,需定期审查配置并更新安全策略。