在 Debian 上提升 Laravel 性能,可以从 系统层、PHP 层、Laravel 层、缓存与队列、运维配置 多个维度来做。下面按实战优先级整理。
sudo apt install php8.2 php8.2-fpm php8.2-cli \
php8.2-mysql php8.2-redis php8.2-opcache php8.2-mbstring php8.2-xml
编辑:
/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
生产环境
validate_timestamps=0,改代码后需重启 PHP-FPM
sudo systemctl restart php8.2-fpm
APP_ENV=production
APP_DEBUG=false
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
修改配置后必须重新执行
config/app.php 中移除不需要的 providers
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
gzip on;
gzip_types text/css application/javascript application/json;
location ~* \.(js|css|png|jpg|svg)$ {
expires 30d;
access_log off;
}
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
安装 Redis:
sudo apt install redis-server
SET slow_query_log = 1;
SET long_query_time = 1;
User::with('posts')->get();
php artisan queue:work --daemon
sudo apt install supervisor
示例:
[program:laravel-worker]
command=php /var/www/artisan queue:work --daemon
autostart=true
autorestart=true
ulimit -n 65535
Laravel + Redis + OPcache 对内存友好
systemctl disable bluetooth
✅ 推荐组合
composer require laravel/octane
php artisan octane:start
如果你愿意,可以告诉我:
我可以给你一套 针对性配置方案。