yiisoft / yii2-docker

Official Docker images suitable for Yii 2.0
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
381 stars 202 forks source link

There is a working example for nginx/yii/php-fpm in Docker? #130

Closed WebSofter closed 3 years ago

WebSofter commented 3 years ago

I am using this image yiisoftware/yii2-php:7.4-fpm and 2 days looking for a working example. Does it even work? I'm stuck on mistakes:

[error] 21#21: *45 connect() failed (111: Connection refused) while connecting to upstream, client [error] 21#21: *44 no live upstreams while connecting to upstream, client

docker-compose.yml

version: '3.3'
services:
  nginx:
    image: nginx:latest
    container_name: y_nginx
    volumes:
      - ./docker/nginx:/etc/nginx/conf.d
      - ./app/backend:/app/backend
      - ./app/frontend/dist:/app/frontend/dist
      - ./app/logs:/app/logs
    env_file: .env
    ports:
      - 83:80
    depends_on:
      - php
    links:
      - php
    networks:
      - y-network
  php:
    build: 
      context: .
      dockerfile: ./app/backend/api/Dockerfile
    container_name: y_php
    expose:
      - "9000"
    ports:
      - 22080:80
    environment:
      - VIRTUAL_PORT=9000
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./app/backend:/app/backend
      - ./docker/php/www.conf /usr/local/etc/php-fpm.d/www.conf
    #restart: always
    networks:
      - y-network
      - 
networks:
  y-network:
    driver: bridge

Dockerfile

FROM yiisoftware/yii2-php:7.4-fpm

# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
    apt-get install -y procps

#COPY ./docker/php/php-fpm.conf /usr/local/etc/php-fpm.conf
#COPY ./docker/php/www.conf /usr/local/etc/php-fpm.d/www.conf

nginx.conf

#keepalive_timeout  65;

proxy_buffer_size   64k;
proxy_buffers   4 64k;
proxy_busy_buffers_size   64k; 

upstream phpfpm {
    server php:9000 max_fails=3 fail_timeout=30s;
    least_conn;
    keepalive 8;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name localhost;
        server_tokens off;
        root /app/frontend/dist;

        index index.php index.html index.htm index.nginx-debian.html;

        error_log /app/logs/error.log;
        access_log /app/logs/access.log;

        # serve static files directly
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
                access_log off;
                expires max;
                log_not_found off;
        }

        location / {
            try_files $uri $uri/ /index.html;
        }

        location ~ \.php$ {
            try_files $uri =404;

            # Fix for server variables that behave differently under nginx/php-fpm than typically expected
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # Include the standard fastcgi_params file included with nginx
            include fastcgi_params;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            fastcgi_index index.php;
            # Override the SCRIPT_FILENAME variable set by fastcgi_params
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
            fastcgi_pass phpfpm;
            proxy_read_timeout 5m;
        }

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                expires max;
                log_not_found off;
        }

        location ~ /\.ht {
                deny all;
        }
}
schmunk42 commented 3 years ago

This example should work https://github.com/yiisoft/yii2-docker/blob/master/.env-dist#L19-L24

Bildschirmfoto von 2021-05-03 08-46-59