docker上带有Nginx、PHP7.4FPM和mysql 8的Laravel6比PHP7.1上的Laravel4慢
我一直在一个基于Laravel4.2和PHP7.1的网站上工作。最近,我一直在尝试使用PHP7.4和mysql 8将该站点迁移到Laravel 6。我使用以下设置设置docker 数据库文件:docker上带有Nginx、PHP7.4FPM和mysql 8的Laravel6比PHP7.1上的Laravel4慢,php,performance,docker,nginx,laravel-6,Php,Performance,Docker,Nginx,Laravel 6,我一直在一个基于Laravel4.2和PHP7.1的网站上工作。最近,我一直在尝试使用PHP7.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配置:
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"
迁移进展顺利,但我注意到页面加载相当缓慢
在PHP7.1和Apache上运行的旧代码上的同一页大约需要100-200毫秒,而我的新版本几乎需要1秒
我在bootstrap/app.php中放了一个出口,它仍然需要大约相同的时间。
我注意到app_debug是开着的,我把它关掉了,这样就把在页面上加载“hello”文本的延迟减少到了600-700毫秒左右
我想知道码头工人是否增加了延迟,或者我是否错过了laravel 6上可能会减慢速度的任何设置
在这两个服务器上都禁用了opcache
我一直在尝试测试一些时差。我不知道怎么做,但试了一下
索引页的第一行旧设置-8ms
Docker设置-16ms 在bootstrap app.php的第一行中
旧设置-28毫秒
Docker设置-106ms 在返回$app之前在bootstrap app.php中
旧设置-56ms
Docker设置-206ms 在应用程序执行之前在index.php上执行
旧设置-68ms
Docker设置-254ms 完全加载应用程序后
旧设置-115毫秒
Docker设置-1秒(约) 在Laravel4中,在$app返回后,index.php中有$app->run()。在larave 6中,我们使用了而不是$app->run() 这可能是加载了一些可能导致延迟的东西。我还试着评论了一些中间件,但仍然是一样的
每个请求都需要很长时间才能加载。字体加载需要300~400毫秒,每种字体的加载速度都比apache上的旧代码慢10倍左右。不幸的是,将使用本机PHP/MySQL(例如Mamp)的站点与Docker进行比较没有多大意义。Docker会大大降低站点的速度(在Mac和Windows上都是如此,在Linux上可能会更好),所以站点的速度会大大降低是正常的
如果您关心性能,您应该直接在PC上测试它,或者将站点上传到某个开发服务器上,以查看站点性能 我发现了一些显著提高性能的东西。不同页面上站点的时间戳类似于MAMP的裸机配置 我使用了如下所述的音量优化:
在docker中运行另一个站点,您将看到实际的差异。Docker容器确实增加了一些延迟,但我没有数字。
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);