优化 Debian 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 性能,一般从 系统层、Nginx、PHP-FPM、数据库、系统资源与监控 几个层面来优化。下面给你一套实用、可落地的优化方案(按重要性排序)。
apt update && apt upgrade -y
Debian 新版本 + 新内核通常性能更好,支持更好的 TCP / IO 调度。
查看当前:
ulimit -n
修改(永久):
vi /etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
或修改 systemd 服务:
vi /etc/systemd/system.conf
DefaultLimitNOFILE=65535
vi /etc/sysctl.conf
常用优化:
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
vm.swappiness = 10
vm.dirty_ratio = 10
应用:
sysctl -p
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
keepalive_timeout 65;
keepalive_requests 10000;
client_max_body_size 50m;
client_body_buffer_size 128k;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/css application/javascript application/json text/xml text/plain;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
access_log off;
}
一般推荐使用 dynamic 或 ondemand(低内存)
编辑:
vi /etc/php/8.x/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
pm.max_requests = 1000
如果是高并发网站:
pm = static
pm.max_children = 100
vi /etc/php/8.x/fpm/php.ini
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 120
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.jit_buffer_size=128M
重启:
systemctl restart php8.x-fpm
/etc/mysql/my.cnf 或 /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
max_connections = 300
query_cache_type = 0
skip-name-resolve
apt install redis-server
PHP 使用:
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=phpcache:100m inactive=60m max_size=1g;
htop
vmstat 1
iostat -x 1
nginx -t
systemctl status php8.x-fpm
| 现象 | 原因 | 优化 |
|---|---|---|
| CPU 高 | PHP 慢 | 开启 OPcache |
| 内存爆 | FPM 进程多 | 调整 pm |
| IO 高 | 数据库 | innodb_buffer_pool |
| 并发低 | 文件描述符 | 调大 |
✅ OPcache + PHP-FPM 优化
✅ Nginx worker & keepalive
✅ MySQL innodb_buffer_pool
✅ 系统 ulimit + sysctl
✅ Redis / FastCGI Cache
如果你愿意,可以告诉我:
我可以直接给你一套 定制参数配置,而不是通用方案。