在 Debian 上优化 Laravel 性能,通常从系统层、PHP 层、Laravel 层、数据库与缓存层几个方面入手。下面给你一套比较实用、可落地的方案。
sudo apt update
sudo apt install -y nginx php-fpm php-cli php-mysql \
php-curl php-mbstring php-xml php-zip php-gd \
redis-server supervisor
编辑 PHP 配置(路径可能不同):
sudo nano /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
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
运行(示例):
php artisan octane:start --server=roadrunner
或使用 Swoole(性能更好):
apt install php-swoole
php artisan octane:start --server=swoole
示例 nginx.conf 片段:
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
keepalive_timeout 30;
gzip on;
gzip_types text/css application/javascript;
location ~* \.(js|css|png|jpg|svg)$ {
expires 30d;
access_log off;
}
.env
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
启动 Redis:
sudo systemctl enable --now redis-server
sudo apt install supervisor
示例 /etc/supervisor/conf.d/laravel.conf:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel/artisan queue:work --tries=3
autostart=true
autorestart=true
numprocs=4
sudo supervisorctl reread
sudo supervisorctl update
EXPLAIN SELECT * FROM users WHERE email = 'x';
避免每次请求新建连接
htopphp-fpm statusredis-cli infocomposer require barryvdh/laravel-debugbar --dev
✅ Debian 12
✅ Nginx
✅ PHP 8.2 + OPcache
✅ Laravel Octane (Swoole)
✅ Redis
✅ Supervisor
✅ config/route/view cache
如果你愿意,我可以: