温馨提示×

如何在Debian上优化Laravel性能

小樊
42
2025-02-20 08:28:13
栏目: 智能运维
Debian服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Debian上优化Laravel性能可以通过多种方法实现,以下是一些关键的优化策略:

1. 使用Telescope进行深度剖析与实时监控

  • 安装Telescope:通过Composer安装Laravel Telescope:
    composer require laravel/telescope
    
  • 发布Telescope资产
    php artisan telescope:install
    php artisan migrate
    
  • 注册Telescope:在app/Providers/AppServiceProvider.php中注册Telescope:
    use Laravel\Telescope\Telescope;
    use Laravel\Telescope\TelescopeApplicationServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function register()
        {
            Telescope::ignoreMigrations();
        }
    
        public function boot()
        {
            Telescope::night();
        }
    }
    
  • 监控查询:使用DB::listen记录查询:
    DB::listen(function ($query) {
        Telescope::recordQuery($query);
    });
    

2. 优化队列处理机制

  • 配置队列驱动程序:在.env文件中设置队列驱动程序:
    QUEUE_CONNECTION=redis
    
  • 创建作业类:使用Artisan命令创建作业类:
    php artisan make:job SendEmail
    
  • 调度作业:分派作业到队列:
    use App\Jobs\SendEmail;
    SendEmail::dispatch($user);
    
  • 优化队列
    • 实施批处理。
    • 建立稳健的重试机制。
    • 根据负载动态扩展队列工作者。

3. 使用Laravel Octane提高性能

  • 安装Octane:通过Composer安装Octane:
    composer require laravel/octane
    
  • 安装服务:以FrankenPHP为例安装服务:
    php artisan octane:install --server=frankenphp
    
  • 启用服务:启动Octane服务:
    php artisan octane:start
    
  • 配置Nginx:将站点请求代理给Octane服务。

4. 开启OpCache加速PHP执行

  • 编辑php.ini
    opcache.enable=1
    opcache.enable_cli=1
    opcache.jit=tracing
    opcache.jit_buffer_size=256m
    opcache.memory_consumption=512m
    opcache.interned_strings_buffer=64m
    opcache.max_accelerated_files=10000
    opcache.revalidate_freq=60
    opcache.validate_timestamps=1
    opcache.fast_shutdown=1
    opcache.save_comments=1
    

5. 配置Nginx和PHP-FPM

  • 安装Nginx
    sudo aptitude install nginx
    
  • 配置Nginx:编辑/etc/nginx/sites-available/fmtmis文件:
    server {
        listen 80;
        root /var/www/code/fmtmis/webroot/public;
        index index.php;
        server_name fmtmis.testing;
    
        location / {
            try_files uri uri/ /index.php?query_string;
        }
    
        location \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
    
        location /\.ht {
            deny all;
        }
    }
    
  • 安装PHP-FPM
    sudo aptitude install php7.2-fpm php7.2-gd php7.2-json php7.2-mbstring php7.2-mysql php7.2-xml php7.2-zip
    
  • 配置Supervisor:建立Supervisor配置文件并启动Horizon:
    sudo vim /etc/supervisor/conf.d/fmtmis.conf
    
    [program:fmtmis-horizon]
    process_name=%(program_name)s
    command php /var/www/code/fmtmis/webroot/artisan horizon
    autostart=true
    autorestart=true
    user=root
    redirect_stderr=true
    stdout_logfile=/var/www/code/fmtmis/webroot/horizon.log
    
    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start fmtmis-horizon
    

通过以上步骤,您可以在Debian上显著提升Laravel应用程序的性能。这些优化策略包括使用Telescope进行监控、优化队列处理、使用Octane提高执行速度、开启OpCache加速PHP执行以及正确配置Nginx和PHP-FPM。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:如何在Debian上优化Tomcat性能

0