![](/img/trans.png)
[英]sesion being created with umask 0 on docker laravel php-fpm nginx
[英]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-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"
迁移进行得很顺利,但我注意到页面加载速度很慢。
我在 php 7.1 和 Apache 上运行的旧代码的同一页面大约需要 100 - 200 毫秒,而我的新版本几乎需要 1 秒。
我在 bootstrap/app.php 中放置了一个出口,它仍然需要大约相同的时间。 我注意到 app_debug 已打开,我将其关闭,这将延迟减少到大约 600 - 700 毫秒以在页面上加载“你好”文本。
我想知道是 Docker 增加了延迟,还是我错过了 laravel 6 上可能会减慢它的任何设置。
两者都禁用了 opcache。
我一直在尝试测试一些时差。 我对如何做到这一点知之甚少,但试了一下。
索引页第一行
旧设置 - 8ms
Docker 设置 - 16 毫秒
在 bootstrap app.php 第一行
旧设置 - 28ms
Docker 设置 - 106 毫秒
在 $app 返回之前的 bootstrap app.php 中
旧设置 - 56ms
Docker 设置 - 206 毫秒
在应用程序执行之前的 index.php
旧设置 - 68ms
Docker 设置 - 254 毫秒
应用程序完全加载后
旧设置 - 115ms
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);
这是否正在加载可能导致某些延迟的内容。 我也试过注释掉一些中间件,结果还是一样。
每个请求都需要很长时间才能加载。 字体加载需要 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.