简体   繁体   English

docker 上带有 Nginx、php 7.4 fpm 和 mysql 8 的 Laravel 6 比 php 7.1 上的 Laravel 4 慢

[英]Laravel 6 with Nginx, php 7.4 fpm and mysql 8 on docker is slower than Laravel 4 on php 7.1

I have been working on a site built on Laravel 4.2 and on Php 7.1.我一直在开发基于 Laravel 4.2 和 PHP 7.1 的网站。 Recently I have been trying to migrate the site to Laravel 6 with php 7.4 and mysql 8. I setup docker with the following settings.最近我一直在尝试使用 php 7.4 和 mysql 8 将站点迁移到 Laravel 6。我使用以下设置设置了 docker。

Database File:数据库文件:

FROM mysql:8.0.18
ADD data_x.sql /docker-entrypoint-initdb.d
CMD ["mysqld"]
EXPOSE 3306

Nginx File: Nginx 文件:

FROM nginx:latest
CMD ["nginx"]
EXPOSE 80 443

Nginx conf: Nginx 配置:

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;
    root /var/www/public/superadmin;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}

Php-fpm php-fpm

FROM php:7.4.0-fpm-buster
RUN docker-php-ext-install pdo_mysql
CMD ["php-fpm"]
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
EXPOSE 9000

docker-compose docker-compose

version: '3'

services:
  nginx:
    build:
      context: ./nginx
    volumes:
      - ../laravelproject:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php-fpm
    ports:
      - "80:80"
      - "443:443"

  php-fpm:
    build:
      context: ./php-fpm
    volumes:
      - ../laravelproject:/var/www
      - ../laravelproject/serve_config/custom.ini:/usr/local/etc/php/conf.d/custom.ini
    links:
      - database:mysql

  database:
    build:
        context: ./database
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=secret
      - MYSQL_ROOT_PASSWORD=docker
    command: ['--default-authentication-plugin=mysql_native_password']
    ports:
      - "3306:3306"

The migration is going smooth, but i noticed the pages load quite slowly.迁移进行得很顺利,但我注意到页面加载速度很慢。

The same page on my old code running on php 7.1 and Apache takes about 100 - 200ms while my new version would almost take 1 second.我在 php 7.1 和 Apache 上运行的旧代码的同一页面大约需要 100 - 200 毫秒,而我的新版本几乎需要 1 秒。

I put an exit in the bootstrap/app.php and it still takes around the same time.我在 bootstrap/app.php 中放置了一个出口,它仍然需要大约相同的时间。 I noticed the app_debug was on and I turned it off and that reduced the delay to around 600 - 700ms to load a 'hello' text on a page.我注意到 app_debug 已打开,我将其关闭,这将延迟减少到大约 600 - 700 毫秒以在页面上加载“你好”文本。

I am wondering is the Docker adding the delay or am I missing any setting on laravel 6 that could be slowing it down.我想知道是 Docker 增加了延迟,还是我错过了 laravel 6 上可能会减慢它的任何设置。

The opcache is disable on both.两者都禁用了 opcache。

I have been trying to test some time differences.我一直在尝试测试一些时差。 I have not much knowledge of how to do it, but gave it a shot.我对如何做到这一点知之甚少,但试了一下。

The index page first line索引页第一行
old setup - 8ms旧设置 - 8ms
Docker setup - 16ms Docker 设置 - 16 毫秒

in bootstrap app.php first line在 bootstrap app.php 第一行
old setup - 28ms旧设置 - 28ms
Docker setup - 106ms Docker 设置 - 106 毫秒

in bootstrap app.php before $app is returned在 $app 返回之前的 bootstrap app.php 中
old setup - 56ms旧设置 - 56ms
Docker setup - 206ms Docker 设置 - 206 毫秒

At index.php before app is executed在应用程序执行之前的 index.php
old setup - 68ms旧设置 - 68ms
Docker setup - 254ms Docker 设置 - 254 毫秒

after complete load of app应用程序完全加载后
old setup - 115ms旧设置 - 115ms
Docker setup - 1 second (approx) Docker 设置 - 1 秒(大约)

In laravel 4, we had the $app->run() in the index.php after the $app was returned.在 Laravel 4 中,我们在 $app 返回后在 index.php 中有 $app->run() 。 In larave 6 we have instead of $app->run().在 larave 6 中,我们用 $app->run() 代替了。

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

could this be loading something that could be contributing to some latency.这是否正在加载可能导致某些延迟的内容。 I also tried commenting out some middleware and it was still the same.我也试过注释掉一些中间件,结果还是一样。

Every request is taking a long time to load.每个请求都需要很长时间才能加载。 The fonts are taking 300 ~ 400 ms to load and everyhting is about 10 times slower compared to the old code on apache on Mamp.字体加载需要 300 ~ 400 毫秒,并且与 Mamp 上的 apache 上的旧代码相比,每一个都慢了大约 10 倍。

Unfortunately comparing site working in native PHP/MySQL (for example Mamp) with Docker doesn't make much sense.不幸的是,将在本机 PHP/MySQL(例如 Mamp)中工作的站点与 Docker 进行比较并没有多大意义。 Docker slows sites a lot (both on Mac and Windows, maybe it's much better on Linux) so this is normal that site will be much slower. Docker 会大大降低站点的速度(在 Mac 和 Windows 上,也许在 Linux 上会好得多),因此站点会慢得多是正常的。

If you are concerned about performance you should test it directly on your PC or upload site to some dev server to see site performance.如果您担心性能,您应该直接在您的 PC 上测试它或将站点上传到某个开发服务器以查看站点性能。

I found something that drastically improved the performance.我发现了一些可以大大提高性能的东西。 The time stamps for the site on different pages are similar to the bare metal configuration with MAMP.不同页面上站点的时间戳类似于使用 MAMP 的裸机配置。

I used volume optimisation as explained here: https://engageinteractive.co.uk/blog/making-docker-faster-on-mac我使用了如下解释的音量优化: https : //engageinteractive.co.uk/blog/making-docker-faster-on-mac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM