在Debian上优化ThinkPHP性能可从以下方面入手:
启用OPcache
安装并启用OPcache加速PHP代码执行,编辑/etc/php/8.x/fpm/php.ini(根据PHP版本调整路径),设置:
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0 # 生产环境关闭
重启PHP-FPM:sudo systemctl restart php8.x-fpm。
优化数据库
User::with('profile'))减少N+1查询问题。OPTIMIZE TABLE整理表碎片。配置缓存策略
php-redis扩展,在config/cache.php中设置默认驱动为Redis。php think optimize:route和php think optimize:autoload生成缓存。Web服务器优化
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.x-fpm.sock;
}
gzip on;
gzip_types text/css application/javascript;
重启Nginx:sudo systemctl restart nginx。代码与依赖优化
composer install --no-dev --optimize-autoloader。系统级优化
top或htop定位瓶颈。参考来源: