优化 Linux 下的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,通常从 系统层、Nginx、PHP、MySQL、缓存、安全与监控 几个方面入手。下面给你一套实用、可落地的优化方案。
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
vm.swappiness = 10
执行:
sysctl -p
ulimit -n 65535
永久生效:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
systemctl disable --now postfix
systemctl disable --now bluetooth
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/plain text/css application/json application/javascript;
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
access_log off;
}
listen 443 ssl http2;
www.conf
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
高并发可改为
pm = static
disable_functions = exec,passthru,shell_exec,system
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
max_connections = 500
query_cache_type = 0
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
SHOW FULL PROCESSLIST;
yum install redis
systemctl enable --now redis
PHP 使用:
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=PHP:100m;
yum update -y
htopiotopnetstat -anp | grep nginxmysqltunerab -n 10000 -c 500 http://domain.com/
如果你愿意,可以告诉我:
我可以给你一套 定制化优化配置。