优化 Debian 上的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 通常从系统层、Nginx、PHP、MySQL、缓存、安全与监控几个方面入手。下面给你一套实用、可落地的优化清单,适合生产环境。
apt update && apt upgrade -y
编辑 /etc/sysctl.conf:
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
vm.swappiness = 10
执行:
sysctl -p
编辑 /etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll;
}
gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/plain application/json application/javascript text/css;
location ~* \.(jpg|css|js|png)$ {
expires 30d;
access_log off;
}
access_log off;
编辑:
/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
若是高并发 API,可改为
pm = static
disable_functions = exec,passthru,shell_exec,system
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
max_connections = 300
query_cache_type = 0
内存小可改为 512M
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
OPTIMIZE TABLE table_name;
apt install redis-server
PHP 使用:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
适合内容站、API 缓存
ufw allow 22,80,443
ufw enable
server_tokens off;
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
htopnetdatamysqltuner/var/log/nginx//var/log/php-fpm.log✅ Debian 12
✅ Nginx
✅ PHP 8.2+
✅ MariaDB
✅ Redis
✅ OPcache
如果你愿意,我可以:
你现在是哪种业务场景?