在 Debian 上搭建的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)环境,提速通常从系统层、Nginx、PHP、MySQL、缓存、硬盘/网络几个维度入手。下面给你一套实用可落地的优化方案。
systemctl disable --now bluetooth
systemctl disable --now cups
只保留 SSH、Nginx、PHP-FPM、MySQL
ulimit -n 65535
写入:
/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
gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/css application/javascript image/svg+xml;
open_file_cache max=10000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
location ~* \.(js|css|png|jpg|gif|ico|svg)$ {
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
/etc/php/*/fpm/conf.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
disable_functions = exec,passthru,shell_exec,system
/etc/mysql/my.cnf
[mysqld]
innodb_buffer_pool_size = 1G # 内存一半
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 200
SHOW PROCESSLIST;
开启慢日志:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
apt install redis-server
PHP:
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m;
ext4 或 xfsnoatime,nodiratime
✅ Nginx + PHP-FPM + OPcache
✅ Redis 缓存
✅ Gzip + 浏览器缓存
✅ SSD + 合理 MySQL 参数
如果你愿意,可以告诉我:
我可以直接给你一份可直接用的配置文件。