优化 Linux 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 通常从系统层、Nginx、PHP、MySQL、缓存与架构几个方面入手。下面给你一套实用、可落地的优化方案(偏生产环境)。
# 修改文件限制
/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
vm.swappiness = 10
执行:
sysctl -p
systemctl disable postfix firewalld --now
# 若用 iptables 或云安全组替代
/var/lib/mysql/usr/local/nginx/logs
放 SSD,提升 IO。worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/css application/javascript application/json image/svg+xml;
location ~* \.(js|css|png|jpg|gif|svg|woff2)$ {
expires 30d;
access_log off;
}
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=fcgi:100m;
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
disable_functions = exec,passthru,shell_exec,system
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
max_connections = 500
query_cache_type = 0
slow_query_log = 1
long_query_time = 1
OPTIMIZE TABLE table_name;
top / htop
iotop
netstat -anp | grep nginx
mysqltuner
php-fpm status
如果你愿意,可以告诉我:
我可以给你一套精准参数模板。