在 Debian 上优化 Laravel 性能,通常可以从系统层、PHP 层、Laravel 层、数据库与缓存层几个方面入手。下面按 practical(实际可用)方式给你一套优化清单。
php -v
sudo apt install php-opcache
/etc/php/*/cli/php.ini 和 fpm/php.ini:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0 ; 生产环境
opcache.revalidate_freq=60
sudo apt install php-fpm nginx
Nginx + PHP-FPM 性能明显优于 Apache。
APP_ENV=production
APP_DEBUG=false
composer install --optimize-autoloader --no-dev
composer dump-autoload -o
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache # Laravel 8+
⚠️ 改配置后必须重新 cache
queuebroadcastingsudo apt install redis-server
.env:
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
默认文件缓存慢,Redis 更稳定。
SHOW VARIABLES LIKE 'query_cache%';
User::with('posts')->get();
Cache::remember('users', 600, fn () => User::all());
php artisan queue:work --daemon
Supervisor 管理:
sudo apt install supervisor
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
include fastcgi_params;
}
开启:
php artisan route:list
php artisan telescope # 开发
htop
iotop
✅ Debian 12
✅ Nginx
✅ PHP 8.2 FPM
✅ Redis
✅ MySQL/PostgreSQL
✅ Supervisor
✅ OPcache + Laravel cache
如果你愿意,可以告诉我:
我可以给你更针对性的优化方案。