下面按从系统到应用的顺序,给你一套在 Debian 上优化 Laravel 的实用方案(适用于生产环境,不折腾、可落地)。
Laravel 对 PHP 版本要求较高,建议:
php -v
例如 Laravel 10+ 建议 PHP 8.2 / 8.3
安装(以 8.2 为例):
sudo apt update
sudo apt install php8.2 php8.2-cli php8.2-fpm \
php8.2-mysql php8.2-curl php8.2-mbstring \
php8.2-xml php8.2-zip php8.2-bcmath php8.2-redis
编辑:
sudo nano /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
根据内存调整:
max_children ≈ 40~6080~120重启:
sudo systemctl restart php8.2-fpm
编辑:
sudo nano /etc/php/8.2/fpm/conf.d/opcache.ini
推荐配置:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0
opcache.revalidate_freq=0
validate_timestamps=0生产环境必开
server {
listen 80;
server_name example.com;
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
}
gzip on;
gzip_types text/css application/javascript application/json;
}
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
⚠️ 修改配置后记得重新 cache
.env
APP_ENV=production
APP_DEBUG=false
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
安装 Redis:
sudo apt install redis-server
sudo systemctl enable redis
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 1;
$users = User::with('posts')->get();
CREATE INDEX idx_user_email ON users(email);
sudo apt install supervisor
配置:
[program:laravel-worker]
command=php /var/www/laravel/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker
LOG_CHANNEL=stack
LOG_LEVEL=warning
php artisan log:clear
ulimit -n
建议:
sudo nano /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
✅ Debian 12
✅ PHP 8.2 / 8.3
✅ Nginx
✅ PHP-FPM
✅ Redis
✅ Supervisor
✅ OPCache
config/route/view 已 cache如果你愿意,可以告诉我:
我可以给你定制一套参数,直接可用。