在 Debian 上优化 LNMP(Linux + Nginx + MySQL/MariaDB + PHP),可以从系统层、Nginx、PHP、MySQL、缓存、安全与监控几个维度来做。下面是一套比较实用的优化思路(适用于生产环境,Debian 11/12 均可用)。
# 提高文件描述符限制
ulimit -n 65535
写入 /etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 600
vm.swappiness = 10
执行:
sysctl -p
apt install cpufrequtils
cpufreq-set -g performance
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/javascript;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|woff2)$ {
expires 30d;
access_log off;
}
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_connect_timeout 60s;
建议使用 PHP 8.1 / 8.2 / 8.3
/etc/php/8.2/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 120
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 40
pm.max_requests = 1000
expose_php = Off
disable_functions = exec,passthru,shell_exec,system
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
max_connections = 300
query_cache_type = 0
slow_query_log = 1
apt install mariadb-server
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl
perl mysqltuner.pl
OPTIMIZE TABLE table_name;
apt install redis-server
PHP:
apt install php-redis
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=phpcache:100m;
fastcgi_cache phpcache;
fastcgi_cache_valid 200 10m;
ufw allow 22,80,443
ufw enable
apt update && apt upgrade -y
htopnetdatafail2bantop / htop → CPUfree -m → 内存iostat → 磁盘 IOnginx access log → 慢请求mysqltuner → 数据库如果你愿意,我可以:
你可以告诉我你的 Debian 版本 + 业务类型。