优化 Debian + Laravel 可以从 系统层面、PHP 层面、Laravel 层面、数据库与缓存、运维监控 多个维度来做。下面给你一套比较实用、可落地的优化清单。
systemctl list-unit-files --type=service
禁用不必要的:
systemctl disable bluetooth
编辑:
/etc/security/limits.conf
www-data soft nofile 65535
www-data hard nofile 65535
noatimemount -o remount,noatime /
apt install php8.2-fpm php8.2-cli
apt install php8.2-opcache
/etc/php/8.2/fpm/conf.d/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 ; 生产环境
/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
只保留:
APP_ENV=production
APP_DEBUG=false
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
composer install --optimize-autoloader --no-dev
composer require laravel/octane
php artisan octane:install
可选:
官方 FPM 适合普通项目,Octane 适合 API / 高并发
User::with('posts')->get();
使用:
php artisan telescope
或 Laravel Debugbar 排查
innodb_buffer_pool_size = 1G
query_cache_type = 0
slow_query_log = 1
Schema::table('orders', function ($table) {
$table->index('user_id');
});
'mysql' => [
'read' => [...],
'write' => [...]
]
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
php artisan queue:work --daemon
加 systemd / supervisor 管理
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
gzip on;
gzip_min_length 1024;
启用 HTTP/2:
listen 443 ssl http2;
php artisan schedule:run
| 方案 | 吞吐提升 |
|---|---|
| 默认 Laravel | 1x |
| OPcache | 2–3x |
| config/route cache | +20% |
| Laravel Octane | 5–10x |
如果你愿意,可以告诉我:
我可以直接给你一套 针对你项目的优化方案 + 配置文件模板。