Debian LAMP服务器优化可从系统、服务、性能、安全等方面入手,具体如下:
系统基础优化
sudo apt update && sudo apt upgrade,安装安全补丁。sudo apt autoremove 和 sudo apt clean 移除冗余包。/etc/sysctl.conf,调整TCP缓冲、文件描述符等参数(如 net.ipv4.tcp_rmem、net.core.rmem_max)。Apache服务优化
prefork,启用 worker 或 event 模块(sudo a2dismod prefork && sudo a2enmod worker)。/etc/apache2/apache2.conf,优化 MaxRequestWorkers(根据内存调整)、KeepAliveTimeout(建议≤5秒)。mod_expires 和 mod_headers,设置静态资源缓存时间。数据库服务优化
/etc/mysql/my.cnf,设置 innodb_buffer_pool_size 为服务器内存的50%-80%。query_cache_size),定期运行 mysqlcheck --auto-repair 维护表。max_connections 避免资源耗尽。PHP服务优化
php-opcache 并启用,加速脚本执行。php.ini 中的 memory_limit 和 max_execution_time,匹配应用需求。www.conf 中的进程池参数(如 pm.max_children),优化并发处理。网络与资源优化
mod_deflate 启用Gzip/Brotli压缩,减少传输数据量。noatime 选项,减少磁盘访问开销。安全与监控
ufw 限制开放端口,仅允许HTTP(80)、HTTPS(443)等必要端口。htop、Prometheus 等工具实时监控CPU、内存、磁盘使用情况。注意:修改配置后需重启对应服务(如 systemctl restart apache2、systemctl restart mysql),并测试验证优化效果。