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

标签 php performance docker nginx laravel-6

我一直在开发基于 Laravel 4.2 和 Php 7.1 的网站。最近我一直在尝试使用 php 7.4 和 mysql 8 将站点迁移到 Laravel 6。我使用以下设置设置了 docker。

数据库文件:

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

Nginx 文件:
FROM nginx:latest
CMD ["nginx"]
EXPOSE 80 443

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
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 撰写
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"


迁移进展顺利,但我注意到页面加载速度很慢。

我在 php 7.1 和 Apache 上运行的旧代码的同一页面大约需要 100 - 200 毫秒,而我的新版本几乎需要 1 秒。

我在 bootstrap/app.php 中放置了一个导出,它仍然需要大约相同的时间。
我注意到 app_debug 已打开并将其关闭,这将延迟减少到大约 600 - 700 毫秒以在页面上加载“你好”文本。

我想知道是 Docker 添加了延迟,还是我错过了 laravel 6 上可能减慢它的任何设置。

opcache 在两者上都被禁用。

我一直在尝试测试一些时差。我对如何做到这一点了解不多,但试了一下。

索引页第一行
旧设置 - 8ms
Docker 设置 - 16 毫秒

在 bootstrap app.php 第一行
旧设置 - 28 毫秒
Docker 设置 - 106 毫秒

在返回 $app 之前的引导 app.php 中
旧设置 - 56 毫秒
Docker 设置 - 206 毫秒

在 app 执行前的 index.php
旧设置 - 68 毫秒
Docker 设置 - 254 毫秒

应用程序完全加载后
旧设置 - 115 毫秒
Docker 设置 - 1 秒(大约)

在 laravel 4 中,在 $app 返回后,我们在 index.php 中有 $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);

这可能会加载一些可能导致一些延迟的东西。我也尝试注释掉一些中间件,它仍然是一样的。

每个请求都需要很长时间才能加载。与 Mamp 上 apache 上的旧代码相比,字体需要 300 ~ 400 毫秒的时间来加载,而且每个字体都慢了大约 10 倍。

最佳答案

不幸的是,将使用原生 PHP/MySQL(例如 Mamp)的站点与 Docker 进行比较没有多大意义。 Docker 会大大降低站点速度(在 Mac 和 Windows 上,也许在 Linux 上要好得多),所以站点速度会慢得多是正常的。

如果您担心性能,您应该直接在您的 PC 上测试它或将站点上传到某个开发服务器以查看站点性能。

关于php - docker 上带有 Nginx、php 7.4 fpm 和 mysql 8 的 Laravel 6 比 php 7.1 上的 Laravel 4 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654284/

相关文章:

php - 如何使用 Mysql Joins 而不是嵌套子查询来获得相同的结果?

php - Common_Model.php 存在,但没有声明类 Common_Model

php - Wordpress 中的 Ajax 请求无法执行我的自定义函数

java - 在这种情况下,java 深拷贝如何实现

performance - 谷歌应用程序脚本 : copying spreadsheet is very slow

c - 以下两种情况在性能上有什么区别吗?

angular - Nginx for Angular没有达到后端api

docker - 使用nfs网络路径作为kubernetes持久卷

php - 在 SS CMS 中编辑(以及添加和删除)数组数据的最佳方法是什么?

docker - 有没有人成功地将Apigee Edge作为Docker容器运行?