CentOS LNMP 性能调优策略
一 系统层优化
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
在 systemd 服务单元中添加:[Service]
LimitNOFILE=65535
LimitNPROC=65535
vm.swappiness=10
vm.dirty_background_ratio=5
vm.dirty_ratio=10
net.core.somaxconn=65535
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=30
二 Nginx 调优
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 8192;
use epoll;
multi_accept on;
accept_mutex off; # 高并发下通常关闭更稳
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
keepalive_requests 1000;
}
client_body_buffer_size 128k;
client_max_body_size 50m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
三 PHP-FPM 调优
[www]
user = nginx
group = nginx
pm = static
pm.max_children = 150
pm.max_requests = 1000
slowlog = /var/log/php-fpm/www-slow.log
request_slowlog_timeout = 3s
pm.status_path = /php_status
内存估算:若单进程约 25MB,则 150 进程约 3.75GB(在 8GB 内存机器上可接受)。opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=7963
opcache.revalidate_freq=60
四 MySQL MariaDB 调优
五 压测与瓶颈定位流程
wrk -t4 -c100 -d60s http://127.0.0.1/index.php),记录 Requests/sec、P95/P99 延迟、错误率。