优化 Debian 上的 LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境,可以从系统层、Web 层、数据库层、PHP 层、安全与监控几个维度来做。下面给你一套比较实用、可落地的优化方案。
apt update && apt upgrade -y
如有条件可启用 backports 内核 或 HWE 内核。
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
# /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
执行:
sysctl -p
eventpreforkeventa2dismod phpX.X
a2enmod proxy_fcgi setenvif
a2enmod mpm_event
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 200
MaxConnectionsPerChild 1000
</IfModule>
a2enmod rewrite expires headers deflate http2
Protocols h2 http/1.1
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
apt install php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
phpdismod <module>
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
apt install mariadb-server
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
max_connections = 200
query_cache_type = 0
内存小可改为 512M
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
mod_expiresapt install redis-server
htopmysqltunerapache2ctl -Mphp-fpm status| 项目 | 优化前 | 优化后 |
|---|---|---|
| 并发 | 50 | 300+ |
| TTFB | 800ms | 200ms |
| CPU | 高 | 稳定 |
如果你愿意,可以告诉我:
我可以给你针对性的配置模板。