在 Linux 上“优化 Laravel”通常分为 系统层(Linux/Web 服务) 和 应用层(Laravel 本身) 两部分。下面按实战角度给你一份较完整的优化清单。
mod_php(性能差)示例 Nginx 关键配置:
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
gzip on;
gzip_min_length 1024;
编辑:
/etc/php/*/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:
free -m
编辑:
/etc/php/*/cli/conf.d/10-opcache.ini
推荐配置:
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0
opcache.revalidate_freq=0
生产环境
validate_timestamps=0,部署后重启 PHP-FPM
ulimit -n 65535
/etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
APP_ENV=production
APP_DEBUG=false
php artisan config:cache
php artisan route:cache
php artisan view:cache
清空:
php artisan config:clear
php artisan route:clear
composer install --optimize-autoloader --no-dev
composer dump-autoload -o
.env:
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
安装:
apt install redis-server
php artisan queue:work --daemon
使用 systemd 守护:
nano /etc/systemd/system/laravel-queue.service
npm run build
并配置 Nginx 长缓存:
location ~* \.(js|css|png)$ {
expires 1y;
}
->select(['id','name'])
->with('relation')
开启慢查询日志:
slow_query_log = 1
systemctl status php8.2-fpm
storage/logs/laravel.log
composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
systemctl restart php8.2-fpm
systemctl restart nginx
如果你愿意,我可以:
你现在的 Laravel 版本和服务器配置是?