在 CentOS 上优化 LAMP(Linux + Apache + MySQL/MariaDB + PHP)速度,可以从系统层、Apache、MySQL、PHP、缓存、前端几个方向入手。下面给你一套较实用、可落地的优化方案。
systemctl list-unit-files --type=service | grep enabled
关闭用不到的:
systemctl disable --now postfix
systemctl disable --now firewalld # 若有外部防火墙可关
ulimit -n 65535
永久生效:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
/tmp 可用 tmpfs(内存盘)查看当前 MPM:
httpd -V | grep MPM
推荐:
event(高并发)prefork修改:
/etc/httpd/conf.modules.d/00-mpm.conf
LoadModule mpm_event_module modules/mod_mpm_event.so
<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 5000
</IfModule>
httpd -M
注释掉不用的:
#LoadModule status_module
#LoadModule autoindex_module
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
yum install php-fpm
Apache 使用:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
memory_limit = 256M
max_execution_time = 30
opcache.enable=1
opcache.memory_consumption=128
opcache.revalidate_freq=60
yum install php-opcache
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
[mysqld]
innodb_buffer_pool_size = 1G # 物理内存的 50%~70%
innodb_log_file_size = 256M
query_cache_type = 0 # MySQL 8 已移除
max_connections = 300
slow_query_log = 1
OPTIMIZE TABLE table_name;
yum install redis
systemctl enable --now redis
PHP 使用:
yum install php-redis
AddOutputFilterByType DEFLATE text/html text/css application/javascript
ExpiresActive On
ExpiresByType image/jpg "access plus 30 days"
top
htop
iotop
mysqltuner.pl
apachectl fullstatus
如果你能告诉我:
我可以给你更精确的配置文件示例。