Laravel 6 与 Nginx,php 7.4 fpm 和 mysql 8 on docker 比 Laravel 4 on php 7.1 慢
Laravel 6 with Nginx, php 7.4 fpm and mysql 8 on docker is slower than Laravel 4 on php 7.1
我一直在开发基于 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"
迁移进行得很顺利,但我注意到页面加载速度很慢。
我的旧代码 运行ning 在 php 7.1 和 Apache 上的同一页面大约需要 100 - 200 毫秒,而我的新版本几乎需要 1 秒。
我在 bootstrap/app.php 中设置了一个出口,它仍然需要大约相同的时间。
我注意到 app_debug 已打开,我将其关闭,这将延迟减少到大约 600 - 700 毫秒以在页面上加载 'hello' 文本。
我想知道是 Docker 添加了延迟,还是我遗漏了 laravel 6 上的任何可能会减慢速度的设置。
两者都禁用了 opcache。
我一直在尝试测试一些时差。我不太了解如何做,但试了一下。
索引页第一行
旧设置 - 8 毫秒
Docker 设置 - 16 毫秒
在bootstrapapp.php第一行
旧设置 - 28 毫秒
Docker 设置 - 106 毫秒
在 bootstrap app.php $app 返回之前
旧设置 - 56 毫秒
Docker 设置 - 206 毫秒
在应用程序执行前index.php
旧设置 - 68 毫秒
Docker 设置 - 254 毫秒
应用程序完全加载后
旧设置 - 115 毫秒
Docker 设置 - 1 秒(大约)
在laravel 4中,我们在$app返回后在index.php中有$app->运行()。在 larave 6 中,我们使用的不是 $app->运行().
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
这是否正在加载可能导致延迟的内容。我也试过注释掉一些中间件,还是一样。
每个请求都需要很长时间才能加载。字体加载需要 300 ~ 400 毫秒,与 Mamp 上 apache 上的旧代码相比,一切都慢了大约 10 倍。
不幸的是,将使用原生 PHP/MySQL(例如 Mamp)工作的网站与 Docker 进行比较没有多大意义。 Docker 使网站变慢很多(在 Mac 和 Windows 上都如此,也许在 Linux 上要好得多)所以网站变慢是正常的。
如果您担心性能,您应该直接在您的 PC 上测试它或将站点上传到某个开发服务器以查看站点性能。
我发现了一些可以显着提高性能的东西。站点在不同页面上的时间戳类似于使用 MAMP 的裸机配置。
我按照此处的说明使用了音量优化:
https://engageinteractive.co.uk/blog/making-docker-faster-on-mac
我一直在开发基于 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"
迁移进行得很顺利,但我注意到页面加载速度很慢。
我的旧代码 运行ning 在 php 7.1 和 Apache 上的同一页面大约需要 100 - 200 毫秒,而我的新版本几乎需要 1 秒。
我在 bootstrap/app.php 中设置了一个出口,它仍然需要大约相同的时间。 我注意到 app_debug 已打开,我将其关闭,这将延迟减少到大约 600 - 700 毫秒以在页面上加载 'hello' 文本。
我想知道是 Docker 添加了延迟,还是我遗漏了 laravel 6 上的任何可能会减慢速度的设置。
两者都禁用了 opcache。
我一直在尝试测试一些时差。我不太了解如何做,但试了一下。
索引页第一行
旧设置 - 8 毫秒
Docker 设置 - 16 毫秒
在bootstrapapp.php第一行
旧设置 - 28 毫秒
Docker 设置 - 106 毫秒
在 bootstrap app.php $app 返回之前
旧设置 - 56 毫秒
Docker 设置 - 206 毫秒
在应用程序执行前index.php
旧设置 - 68 毫秒
Docker 设置 - 254 毫秒
应用程序完全加载后
旧设置 - 115 毫秒
Docker 设置 - 1 秒(大约)
在laravel 4中,我们在$app返回后在index.php中有$app->运行()。在 larave 6 中,我们使用的不是 $app->运行().
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
这是否正在加载可能导致延迟的内容。我也试过注释掉一些中间件,还是一样。
每个请求都需要很长时间才能加载。字体加载需要 300 ~ 400 毫秒,与 Mamp 上 apache 上的旧代码相比,一切都慢了大约 10 倍。
不幸的是,将使用原生 PHP/MySQL(例如 Mamp)工作的网站与 Docker 进行比较没有多大意义。 Docker 使网站变慢很多(在 Mac 和 Windows 上都如此,也许在 Linux 上要好得多)所以网站变慢是正常的。
如果您担心性能,您应该直接在您的 PC 上测试它或将站点上传到某个开发服务器以查看站点性能。
我发现了一些可以显着提高性能的东西。站点在不同页面上的时间戳类似于使用 MAMP 的裸机配置。
我按照此处的说明使用了音量优化: https://engageinteractive.co.uk/blog/making-docker-faster-on-mac