繁体   English   中英

Docker, Symfony nginx/php-fpm 初始化很慢

[英]Docker, Symfony nginx/php-fpm initialized very slow

使用此项目/Docker 设置: https://gitlab.com/martinpham/symfony-5-docker

当我执行docker-compose up -d时,我必须等待大约 2-3 分钟才能真正让它工作。 在加载之前,它给了我“502 Bad Gateway”并记录错误:

2020/05/10 09:22:23 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.28.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.28.0.3:9000", host: "localhost"

为什么 nginx 或 php-fpm 或其他 smth 加载这么慢? 这是我第一次使用 nginx 和 Symfony。 这是正常的吗? 我希望它最多在 1-2 秒内加载,而不是 2-3 分钟。

是的,我见过类似的问题,但对我来说不是合适的解决方案。 应该更改一些 nginx/php-fpm/docker-compose 配置 - 我试过了,但没有运气。 我修改了一点nginx/sites/default.conf (只是添加了 xdebug 的东西)

server {
    listen 80 default_server;
    #listen [::]:80 default_server ipv6only=on;

    server_name localhost;

    root /var/www/public;
    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-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 4 256k;
        fastcgi_buffer_size 128k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #!!!!fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;

        fastcgi_param PHP_VALUE "xdebug.remote_autostart=1
        xdebug.idekey=PHPSTORM
        xdebug.remote_enable=1
        xdebug.remote_port=9001
        xdebug.remote_host=192.168.0.12";
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}

nginx/conf.d/default.conf:

upstream php-upstream {
    server php-fpm:9000;
}

docker-compose.yml:

version: '3'

services:
  database:
    build:
      context: ./database
    environment:
      - MYSQL_DATABASE=${DATABASE_NAME}
      - MYSQL_USER=${DATABASE_USER}
      - MYSQL_PASSWORD=${DATABASE_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
    ports:
      - "3306:3306"
    volumes:
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
      - ./database/data:/var/lib/mysql

  php-fpm:
    build:
      context: ./php-fpm
    depends_on:
      - database
    environment:
      - TIMEZONE=Europe/Tallinn
      - APP_ENV=${APP_ENV}
      - APP_SECRET=${APP_SECRET}
      - DATABASE_URL=mysql://${DATABASE_USER}:${DATABASE_PASSWORD}@database:3306/${DATABASE_NAME}?serverVersion=5.7
    volumes:
      - ../src:/var/www

  nginx:
    build:
      context: ./nginx
    volumes:
      - ../src:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
      - ./logs:/var/log
    depends_on:
      - php-fpm
    ports:
      - "80:80"
      - "443:443"

编辑:

我想我知道为什么现在你的项目需要很长时间才能开始。 我仔细查看了 php-fpm 文件夹中的 Dockerfile,你有这个命令:

CMD composer install ; wait-for-it database:3306 -- bin/console doctrine:migrations:migrate ;  php-fpm 

如您所见,该命令将安装所有 composer 依赖项,然后等待它可以连接到docker-compose.yml配置中定义的数据库容器:

services:
  database:
    build:
      context: ./database
    environment:
      - MYSQL_DATABASE=${DATABASE_NAME}
      - MYSQL_USER=${DATABASE_USER}
      - MYSQL_PASSWORD=${DATABASE_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
    ports:
      - "3306:3306"
    volumes:
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
      - ./database/data:/var/lib/mysql

一旦数据库启动并运行,它将运行src/src/Migrations中的迁移文件以更新数据库,然后启动 php-fpm。

在所有这些完成之前,您的项目将无法准备好,您将收到“502 Bad Gateway”错误。

您可以通过运行docker-compose up来验证这一点以及发生了什么,但这次省略-d参数,这样您就不会在分离模式下运行,这将实时显示所有容器日志。

您将看到一堆日志,包括与作曲家在后台所做的相关的日志,例如:

api-app     |   - Installing ocramius/package-versions (1.8.0): Downloading (100%)         
api-app     |   - Installing symfony/flex (v1.6.3): Downloading (100%)         
api-app     | 
api-app     | Prefetching 141 packages 
api-app     |   - Downloading (100%)
api-app     | 
api-app     |   - Installing symfony/polyfill-php73 (v1.16.0): Loading from cache
api-app     |   - Installing symfony/polyfill-mbstring (v1.16.0): Loading from cache

Composer 安装可能需要更多或更少的时间,具体取决于您是否缓存了所有存储库。

如果您想在开发过程中加快速度,这里的解决方案是从 Dockerfile 中删除composer install命令,并仅在您想要更新/安装新的依赖项时手动运行它。 这样,您可以避免每次运行docker-compose up -d时都运行 composer composer install

要手动执行此操作,您需要连接到容器,然后手动运行composer install ,或者如果您在操作系统中直接安装了 composer,您只需导航到 src 文件夹并运行相同的命令。

这+下面的技巧应该可以帮助您在本地拥有一个足够快的项目。


我有一个类似的配置,一切运行良好,命令docker-compose第一次运行它应该需要一些时间,因为你的图像需要构建,但它甚至不应该花一秒钟的时间来运行。

然而,据我所见,您有很多可能会影响您的性能的已安装卷。 当我在 Mac 上使用 nginx 和 Symfony 运行测试时,我一开始的性能非常糟糕,页面加载至少需要 30 秒。

在我的情况下,一种加快速度的解决方案是在我的一些卷上使用:delegated选项来加快它们的访问速度。

尝试将该选项添加到您的卷中,看看它是否对您有任何改变:

[...]
     volumes:
          - ../src:/var/www:delegated
[...]

如果delegated对您来说不是一个好的选择,请阅读更多关于其他consistent的选项并 在此处cached ,以查看最适合您需求的选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM