Skip to content
Advertisement

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. 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.

Database File:

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

Nginx File:

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

Nginx conf:

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-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.

I put an exit in the bootstrap/app.php and it still takes around the same time. 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.

I am wondering is the Docker adding the delay or am I missing any setting on laravel 6 that could be slowing it down.

The opcache is disable on both.

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
Docker setup – 16ms

in bootstrap app.php first line
old setup – 28ms
Docker setup – 106ms

in bootstrap app.php before $app is returned
old setup – 56ms
Docker setup – 206ms

At index.php before app is executed
old setup – 68ms
Docker setup – 254ms

after complete load of app
old setup – 115ms
Docker setup – 1 second (approx)

In laravel 4, we had the $app->run() in the index.php after the $app was returned. In larave 6 we have instead of $app->run().

$kernel = $app->make(IlluminateContractsHttpKernel::class);

$response = $kernel->handle(
    $request = IlluminateHttpRequest::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.

Advertisement

Answer

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.

I used volume optimisation as explained here: https://engageinteractive.co.uk/blog/making-docker-faster-on-mac

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement