在 Debian 上优化 LNMP(Linux + Nginx + MySQL/MariaDB + PHP),需要从 系统、Nginx、MySQL、PHP、网络、安全 等多个层面进行配置。下面给你一套实战型优化方案,适合生产环境。
apt update && apt upgrade -y
编辑内核参数:
vi /etc/sysctl.conf
推荐配置:
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
vm.swappiness = 10
vm.overcommit_memory = 1
生效:
sysctl -p
vi /etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 10000;
client_max_body_size 50m;
client_body_buffer_size 128k;
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
}
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss;
apt install mariadb-server -y
编辑:
vi /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
max_connections = 300
table_open_cache = 2000
query_cache_size = 0
query_cache_type = 0
tmp_table_size = 64M
max_heap_table_size = 64M
内存越大,
innodb_buffer_pool_size可设到 60–70% RAM
systemctl disable mysql
(如非必须)
编辑:
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 = 80
upload_max_filesize = 50M
post_max_size = 50M
memory_limit = 256M
max_execution_time = 120
fastcgi_pass unix:/run/php/php8.x-fpm.sock;
PHP-FPM:
listen = /run/php/php8.x-fpm.sock
listen.owner = www-data
listen.group = www-data
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=0
apt install redis-server php-redis -y
用于:
apt install ufw
ufw allow 22,80,443/tcp
ufw enable
apt install fail2ban -y
systemctl disable bluetooth
systemctl disable cups
apt install apache2-utils
ab -n 10000 -c 200 http://yourdomain.com/
mysqltuner
| 项目 | 建议 |
|---|---|
| Nginx | worker + epoll + keepalive |
| PHP | OPCache + PHP-FPM socket |
| MySQL | InnoDB Buffer Pool |
| 系统 | 文件句柄 + TCP 参数 |
| 缓存 | Redis / OPCache |
| 安全 | UFW + Fail2ban |
如果你愿意,可以告诉我:
我可以给你一套 精准定制版 LNMP 配置。