温馨提示×

如何优化Debian的LAMP

小樊
44
2026-08-26 01:19:19
栏目: 智能运维

优化 Debian 上的 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 可以从 系统层、Apache、数据库、PHP、安全、缓存 等多个维度进行。下面给你一套 实用、可落地的优化方案,适用于生产环境。


一、系统层优化(基础)

1. 更新系统

apt update && apt upgrade -y

2. 关闭不必要的服务

systemctl list-units --type=service --state=running
systemctl disable 不需要的服务

3. 调整文件描述符限制

vi /etc/security/limits.conf

添加:

* soft nofile 65535
* hard nofile 65535

4. 优化内核参数

vi /etc/sysctl.conf

示例:

net.core.somaxconn = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
vm.swappiness = 10

应用:

sysctl -p

二、Apache 优化(重点)

1. 选择合适的 MPM(推荐 event)

apt install apache2

查看当前 MPM:

apache2ctl -M | grep mpm

推荐:

  • event(高并发)
  • 不推荐 prefork(内存占用大)

切换:

a2dismod mpm_prefork
a2enmod mpm_event

2. 优化 MPM 配置

vi /etc/apache2/mods-available/mpm_event.conf

示例(2G 内存服务器):

<IfModule mpm_event_module>
    StartServers             2
    MinSpareThreads          25
    MaxSpareThreads          75
    ThreadLimit              64
    ThreadsPerChild          25
    MaxRequestWorkers        150
    MaxConnectionsPerChild   1000
</IfModule>

3. 关闭无用模块

apache2ctl -M
a2dismod autoindex cgi status

4. 开启 KeepAlive(适度)

vi /etc/apache2/apache2.conf
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100

5. 启用压缩

a2enmod deflate
a2enmod headers

示例:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>

三、PHP 优化

1. 使用 PHP-FPM(推荐)

apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php-fpm

2. 调整 PHP-FPM 配置

vi /etc/php/*/fpm/pool.d/www.conf

推荐:

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

3. 禁用危险函数

vi /etc/php/*/fpm/php.ini
disable_functions = exec,passthru,shell_exec,system,proc_open

4. 调整 PHP 性能参数

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 120
opcache.enable=1

四、MySQL / MariaDB 优化

1. 使用 MariaDB(Debian 默认)

apt install mariadb-server

2. 基础优化(my.cnf)

vi /etc/mysql/mariadb.conf.d/50-server.cnf

示例(2G 内存):

innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
max_connections = 150
query_cache_type = 0
query_cache_size = 0

3. 安全初始化

mysql_secure_installation

五、缓存优化(性能提升明显)

1. 启用 OPCache

opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000

2. 使用 Redis(可选)

apt install redis-server php-redis

六、安全优化

1. 防火墙

apt install ufw
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable

2. Apache 安全头

Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"

3. 隐藏版本信息

ServerTokens Prod
ServerSignature Off

七、日志与监控

1. 日志轮转

apt install logrotate

2. 监控工具

  • htop
  • iotop
  • netdata
  • prometheus + grafana

八、推荐优化顺序(实战)

  1. 系统基础优化
  2. Apache MPM + KeepAlive
  3. PHP-FPM + OPCache
  4. MariaDB 参数调整
  5. 启用缓存(Redis / 静态缓存)
  6. 安全加固

九、性能对比(优化前后)

项目 优化前 优化后
并发
内存
响应时间
稳定性 一般

如果你愿意,我可以:

  • ✅ 根据你的 服务器配置(内存/CPU/用途) 给出 精准参数
  • ✅ 帮你 排查 LAMP 性能瓶颈
  • ✅ 给你一套 生产环境一键优化脚本

直接告诉我你的 Debian 版本 + 服务器配置 + 使用场景 即可。

0