在 Linux 上运行 Laravel 慢,通常不是 Laravel 本身的问题,而是环境、配置或使用方式导致的。下面按最常见原因 → 排查方法 → 解决建议来说明。
Laravel 是 PHP 框架,没开 OPcache 会非常慢。
✅ 检查:
php -m | grep opcache
✅ 正确配置(php.ini):
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.revalidate_freq=0
opcache.validate_timestamps=0 ; 生产环境
✅ 重启:
sudo systemctl restart php-fpm
php artisan serve这是开发服务器,性能极差。
❌ 慢:
php artisan serve
✅ 正确做法:
检查:
php artisan route:list
php artisan config:cache
✅ 生产环境必须:
php artisan config:cache
php artisan route:cache
php artisan view:cache
✅ 建议:
/mnt/c以下会严重拖慢:
debug=true✅ 生产必须:
APP_DEBUG=false
常见 Laravel 性能杀手。
❌ 慢:
$users = User::all();
foreach ($users as $user) {
$user->posts;
}
✅ 正确:
User::with('posts')->get();
✅ 检测:
php artisan telescope:watch
EXPLAIN SELECT * FROM orders WHERE user_id = 1;
pm = dynamic
pm.max_children = 50
sudo chown -R www-data:www-data storage bootstrap/cache
time curl http://localhost
storage/logs/laravel.log
composer require barryvdh/laravel-debugbar --dev
| 情况 | 响应时间 |
|---|---|
| 开 OPcache + FPM | 20–80ms |
| 没 OPcache | 300ms+ |
| artisan serve | 500ms+ |
| WSL1 + /mnt | 1s+ |
你可以告诉我:
我可以直接帮你精准定位并给一份优化配置。