在 Debian 上搭建的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,如果从速度(响应时间、并发能力、资源占用)角度优化,可以按下面几个层面来做。下面给你一套实用、可落地的提速方案。
编辑 /etc/sysctl.conf:
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
fs.file-max = 100000
执行:
sysctl -p
/etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
worker_processes auto;
worker_connections 10240;
use epoll;
keepalive_timeout 30;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml;
location ~* \.(jpg|jpeg|png|css|js|woff2)$ {
expires 30d;
access_log off;
}
listen 443 ssl http2;
/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
根据内存调整(每个 PHP 进程约 30–80MB)
apt install php-opcache
php.ini:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
phpdismod <模块名>
[mysqld]
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
query_cache_type = 0
max_connections = 200
内存小就调小
innodb_buffer_pool_size
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=cache:10m;
apt install redis-server
PHP:
apt install php-redis
常用于:
htop, iotop, mysqltuner 排查瓶颈top
htop
iotop
netstat -an | grep :80 | wc -l
mysqltuner
如果你愿意,可以告诉我:
我可以给你针对性的优化配置。