优化 Linux + LAMP(Linux + Apache + MySQL/MariaDB + PHP)环境,可以从系统层、Web 服务、数据库、PHP、缓存、安全与监控多个维度入手。下面给你一份实用、可落地的优化清单,适合生产环境。
# 提高文件描述符限制
ulimit -n 65535
/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
net.ipv4.tcp_keepalive_time = 600
sysctl -p
systemctl disable --now postfix
systemctl disable --now bluetooth
a2dismod php7.4 mpm_prefork
a2enmod mpm_event
/etc/apache2/mods-enabled/mpm_event.conf
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
</IfModule>
a2dismod status autoindex cgi
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>
a2enmod http2
/etc/my.cnf 或 /etc/mysql/my.cnf
[mysqld]
innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 300
slow_query_log = 1
long_query_time = 1
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl
perl mysqltuner.pl
EXPLAIN SELECT * FROM orders WHERE user_id=1;
a2enmod proxy_fcgi setenvif
a2enconf php7.4-fpm
/etc/php/7.4/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
apt install redis-server
Options -Indexes
LimitRequestBody 10485760
apt update && apt upgrade -y
htopiotopnetdataPrometheus + Grafanaab -n 10000 -c 200 http://domain.com/
如果你愿意,可以告诉我:
我可以直接帮你给出一份针对性配置。