“优化 Debian LAMP”一般可以从 系统层、Apache、MySQL/MariaDB、PHP、安全与运维 几个维度来做。下面是一套比较实用、可落地的优化清单(假设你用的是 Debian + Apache + MariaDB/MySQL + PHP)。
apt update && apt upgrade -y
systemctl list-unit-files --type=service | grep enabled
关闭如:bluetooth, cups, rpcbind 等无用服务。
/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
sysctl -p
Debian 默认可能是 prefork,高并发推荐:
a2dismod 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 200
MaxConnectionsPerChild 5000
</IfModule>
a2enmod rewrite deflate headers expires http2
<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>
Options -Indexes
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php-fpm
/etc/php/*/fpm/php.ini
memory_limit = 256M
max_execution_time = 60
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
/etc/php/*/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
apt install mariadb-server
/etc/mysql/mariadb.conf.d/50-server.cnf
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 200
原则:
innodb_buffer_pool_size≈ 物理内存的 60–70%
OPTIMIZE TABLE table_name;
ANALYZE TABLE table_name;
apt install ufw
ufw allow 22,80,443/tcp
ufw enable
PermitRootLogin no
apt install certbot python3-certbot-apache
certbot --apache
ServerTokens Prod
ServerSignature Off
/var/log/apache2/slow_query_loghtopnetdataprometheus + grafanaab -n 1000 -c 100 http://yourdomain.com/
如果你愿意,我可以:
你现在的 Debian 版本和用途是什么?(如:个人博客 / API 服务 / 高并发网站)