优化 Debian 上的 LAMP(Linux + Apache + MySQL/MariaDB + PHP)性能,需要从 系统层、Web 服务器、数据库、PHP、缓存、代码与监控 多个方面入手。下面给你一套实用、可落地的优化清单,适合生产环境。
systemctl list-unit-files --type=service | grep enabled
关闭不需要的服务,减少资源占用。
ulimit -n
建议至少 65535:
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
apt install apache2-mpm-event
查看当前 MPM:
apache2ctl -M | grep mpm
编辑:
/etc/apache2/mods-available/mpm_event.conf
示例(2–4 核服务器):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
apache2ctl -M
禁用示例:
a2dismod autoindex status cgid
KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100
apt install php-fpm
a2enmod proxy_fcgi setenvif
a2enconf php8.x-fpm
编辑:
/etc/php/8.x/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
pm.max_requests = 500
disable_functions = exec,passthru,shell_exec,system
apt install php-opcache
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=0
SHOW ENGINES;
编辑:
/etc/mysql/mariadb.conf.d/50-server.cnf
示例(2–4GB 内存):
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_type = 0
max_connections = 100
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
a2enmod cache cache_disk
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 7 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
apt install redis-server php-redis
用于:
a2enmod http2
a2enmod deflate
apt install certbot python3-certbot-apache
certbot --apache
htop
iotop
mytop
apt install apache2-utils
ab -n 1000 -c 100 http://example.com/
✅ Debian 12
✅ Apache + mpm_event
✅ PHP-FPM + OPcache
✅ MariaDB + InnoDB
✅ Redis
✅ HTTP/2 + HTTPS
你可以告诉我:
我可以给你一份 “量身定制的 LAMP 配置模板”,直接可用。