在 Debian 上优化 Laravel 性能,可以从 系统层、PHP 层、Laravel 层、缓存与运维 几个方面来做。下面是一套比较实用、可落地的优化方案。
Laravel 对 PHP 版本有要求,建议使用 PHP 8.1 / 8.2 / 8.3
php -v
升级示例(Debian 12 为例):
sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-curl php8.2-mbstring php8.2-xml php8.2-zip
推荐组合:
检查:
systemctl status php8.2-fpm
编辑:
/etc/php/8.2/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
重载:
sudo systemctl reload php8.2-fpm
编辑:
/etc/php/8.2/fpm/conf.d/10-opcache.ini
推荐配置:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=0 ; 生产环境关闭
重载 PHP-FPM。
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
⚠️ 注意:
.env
APP_DEBUG=false
APP_ENV=production
适合高并发场景:
composer require laravel/octane
php artisan octane:install
使用 Swoole 或 RoadRunner:
php artisan octane:start --server=swoole
✅ 可显著提升 QPS
composer install --optimize-autoloader --no-dev
耗时任务不要同步执行:
php artisan queue:work --daemon
建议用:
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
安装 Redis:
sudo apt install redis-server
Laravel 防 N+1:
User::with('posts')->get();
生产环境开启:
APP_DEBUG=false
并使用:
php artisan query:debug
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
gzip on;
gzip_types text/css application/javascript application/json;
启用 HTTP/2:
listen 443 ssl http2;
top
htop
php-fpm status
ab -n 1000 -c 50 http://your-site.com/
或:
wrk -t4 -c100 -d30s http://your-site.com/
✅ Debian 12
✅ Nginx + PHP-FPM
✅ PHP 8.2 + OPCache
✅ Laravel config/route/view cache
✅ Redis(cache + queue)
✅ Supervisor 守护队列
✅ 可选:Laravel Octane
如果你愿意,可以告诉我:
我可以给你一份 针对性更强的优化配置。