在 Linux 的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境下,提速一般从 Nginx、PHP、MySQL、系统/磁盘、缓存 几个层面入手。下面给你一套实用、可落地的优化方案。
worker_processes auto;
worker_cpu_affinity auto;
查看 CPU 核数:
nproc
events {
worker_connections 10240;
use epoll;
multi_accept on;
}
http {
keepalive_timeout 30;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_max_body_size 20m;
}
gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/css text/js application/json application/javascript image/svg+xml;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
access_log off;
}
编辑:
vim /etc/php-fpm.d/www.conf
常见配置(根据内存调整):
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 500
查看内存:
free -h
vim /etc/php.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
重启:
systemctl restart php-fpm
vim /etc/my.cnf
基础优化(2G~4G 内存):
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 200
thread_cache_size = 50
table_open_cache = 2000
重启:
systemctl restart mysql
slow_query_log = 1
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 1
proxy_cache_path /data/cache levels=1:2 keys_zone=mycache:10m max_size=1g;
yum install redis -y
systemctl enable redis
systemctl start redis
PHP 使用 Redis 缓存 session / 数据。
ulimit -n 65535
vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
vim /etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
vm.swappiness = 10
生效:
sysctl -p
top
htop
iotop
netstat -antp
php-fpm status
mysqltuner
✅ Nginx + OPcache
✅ Redis 缓存
✅ Gzip + 静态缓存
✅ SSD + CDN
如果你愿意,可以告诉我:
我可以给你针对性参数。