1. 服务器与PHP基础配置优化
/etc/sysctl.conf,添加或修改以下参数以提升网络性能:net.core.somaxconn = 262144、net.core.netdev_max_backlog = 262144、net.ipv4.tcp_max_syn_backlog = 262144、net.ipv4.tcp_tw_reuse = 1;运行sudo sysctl -p使配置生效。php.ini(如/etc/php/8.2/fpm/php.ini),添加以下配置:opcache.enable=1、opcache.enable_cli=1、opcache.memory_consumption=512m、opcache.interned_strings_buffer=64m、opcache.max_accelerated_files=10000、opcache.revalidate_freq=60、opcache.jit=tracing、opcache.jit_buffer_size=256m;重启PHP-FPM(sudo systemctl restart php8.2-fpm)使配置生效。sudo fallocate -l 8G /swapfile → sudo chmod 600 /swapfile → sudo mkswap /swapfile → sudo swapon /swapfile;永久生效需添加到/etc/fstab。2. Laravel框架核心配置优化
.env文件,设置APP_ENV=production、APP_DEBUG=false,禁用调试模式以减少不必要的性能开销。php artisan config:cache、php artisan route:cache、php artisan view:cache(开发环境勿用,否则修改不生效)。with()方法预加载关联数据,避免N+1查询问题。例如:$books = Book::with('author')->get()(替代$books = Book::all(); foreach($books) { $book->author; })。3. 数据库性能优化
email、created_at)添加索引,使用Schema::table()方法或在迁移文件中定义。例如:Schema::table('users', function (Blueprint $table) { $table->index(['email', 'created_at']); });通过EXPLAIN语句分析查询性能。SELECT *,只查询必要字段;使用join代替子查询;使用whereExists替代whereIn处理复杂关联查询。4. 缓存策略优化
sudo apt install redis-server),修改.env文件:CACHE_DRIVER=redis、SESSION_DRIVER=redis;Redis的高性能内存存储可显著提升缓存读写速度。Cache::remember()方法缓存频繁访问但不常变化的数据。例如:public function getPopularPosts() { return Cache::remember('popular_posts', 3600, function () { return Post::where('views', '>', 1000)->orderBy('views', 'desc')->take(10)->get(); }); }(3600秒=1小时过期)。5. 队列处理优化
.env文件:QUEUE_CONNECTION=redis,Redis的高吞吐量适合队列任务的高效处理。php artisan make:job SendEmail(假设发送邮件为耗时任务),在handle()方法中编写具体逻辑。/etc/supervisor/conf.d/laravel.conf(示例):[program:laravel-worker] process_name=%(program_name)s command=php /var/www/html/artisan queue:work --queue=default --tries=3 autostart=true autorestart=true user=www-data redirect_stderr=true stdout_logfile=/var/www/html/worker.log;运行sudo supervisorctl reread && sudo supervisorctl update启动服务。6. 使用Laravel Octane提升性能
composer require laravel/octane。php artisan octane:install --server=frankenphp安装服务;启动服务:php artisan octane:start(默认监听localhost:8000)。/etc/nginx/sites-available/example.com),将请求代理给Octane服务:location /index.php {
try_files /not_exists @octane;
}
location / {
try_files $uri $uri/ @octane;
}
location @octane {
set $suffix "";
if ($uri = /index.php) { set $suffix "?$query_string"; }
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:8000$suffix;
}
重启Nginx(sudo systemctl restart nginx)使配置生效。7. 监控与性能分析
composer require laravel/telescope;发布资产并迁移:php artisan telescope:install、php artisan migrate;注册Telescope(在AppServiceProvider.php的register方法中添加Telescope::ignoreMigrations(),避免迁移文件干扰)。AppServiceProvider.php的boot方法中添加DB::listen,将查询记录到Telescope:use Illuminate\Support\Facades\DB;
use Laravel\Telescope\Telescope;
public function boot() {
Telescope::night();
DB::listen(function ($query) {
Telescope::recordQuery($query);
});
}
通过Telescope后台可查看查询耗时、执行次数等,快速定位性能瓶颈。