R
R
Ruslan Absalyamov2020-04-25 14:57:38
Nginx
Ruslan Absalyamov, 2020-04-25 14:57:38

How to open a project in nginx?

Used docker. For a poppy, to raise a web server, but my nginx has risen, but to open the project itself, this did not happen.
What configs did I use
docker-compose

version: '3'

services:
  webserver:
    container_name: nginx_kfnp_yii2
    build:
      context: .
      dockerfile: ./docker/Dockerfile
    environment:
      XDEBUG_CONFIG: remote_host=host.docker.internal
      PHP_IDE_CONFIG: serverName=localhost
    ports:
    - 80:80
    volumes:
    - ./:/var/www
    links:
    - db
    depends_on:
    - db

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

  db:
    container_name: mysql_kfnp_yii2
    image: percona:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: on-failure
    ports:
    - 3306:3306
    expose:
      - 3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: yii2basic
      MYSQL_USER: mysqluser
      MYSQL_PASSWORD: mysqluser


./docker/Dockerfile
#инструкция FROM - указывает на базе какого обараза будем собирать наш контейнер.
FROM debian:latest

#инструкция RUN - указывает какую команду нужно выполнить внутри образа. обновим индекс пакетов
RUN apt-get update

RUN apt-get install -y curl gnupg2 wget ca-certificates lsb-release

RUN echo "deb http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \ | tee /etc/apt/sources.list.d/nginx.list \
    && curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add - \
    && apt-get update

#Инструкция ENV задает переменные окружения с именем <key> и значением <value>.
#Это значение будет находиться в окружении всех команд потомков Dockerfile и могут быть использованы как обычные переменные окружения.
ENV TZ=Europe/Moscow

#указываем временную зону для нашего образа.
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get install -y git nginx python3 perl \
    software-properties-common nano wget zip unzip \
    && apt-get update

#устанавливаем php расширения
RUN apt-get install -y \
    php7.3-fpm php7.3-common php7.3-mysql php7.3-gmp \
    php7.3-curl php7.3-intl php7.3-mbstring php7.3-xmlrpc \
    php7.3-gd php7.3-xml php7.3-cli php7.3-zip php7.3-soap \
    php7.3-imap

#после завершения установок всех пакетов - обновим индекс
RUN apt-get update

RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer && composer global require hirak/prestissimo --no-plugins --no-scripts

#откроем 80 порт
EXPOSE 80

#добавим в контейнер конфигурационный файл php.ini, расположим его на стаднатрном для ubuntu месте
ADD ./docker/conf/php/php.ini /etc/php/7.3/nginx/php.ini

#обязательно добавим конфигурации xdebug
ADD ./docker/conf/php/xdebug.ini /etc/php/7.3/mods-available/xdebug.ini

#добавим конфигурацонный файл для виртуального сервера
ADD ./docker/conf/nginx/test.site.conf /etc/nginx/sites-enabled/test.site.conf

#укажим дирректорию /var/www как рабочую
WORKDIR /var/www

#запускаем процес apache2 в нормальном режиме (На переднем плане).
#Пока живет это процесс, живет и контейнер.
CMD ["nginx", "-g", "daemon off;"]


./docker/conf/nginx/test.site.conf
server {
    listen 80 default_server;
    server_name app-test.local;

    set $base_root /var/www/;
    root $base_root;

    charset UTF-8;
    index index.php index.html;

    location / {
        root $base_root/frontend/web;
        try_files $uri $uri/ /frontend/web/index.php$is_args$args;

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /admin {
        alias $base_root/backend/web/;

        try_files $uri /backend/web/index.php$is_args$args;

        location ~ ^/admin/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location ~ ^/.+\.php(/|$) {
        rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break;
        rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;

        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }

    location ~ /\. {
        deny all;
    }
}


The configs themselves and the project are uploaded to the docker container. And then I also changed /etc/hosts on the LAN.
127.0.0.1 app-test.local
In fact, it should work, only nginx works, but it does not see the site.
5ea42579575fc895274157.png

Because of what this could be and how to fix it. I already climbed into the container and did not catch up with what the problem was. There is no information in the logs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Absalyamov, 2020-04-26
@rusline18

Separated nginx and php into separate container
docker-compose.yml

version: '3'

services:
  app_php:
    container_name: myata_php
    build:
      context: .
      dockerfile: ./docker/Dockerfile
    environment:
      XDEBUG_CONFIG: remote_host=host.docker.internal
      PHP_IDE_CONFIG: serverName=localhost
    volumes:
    - ./:/var/www
    links:
    - db
    depends_on:
    - db

  nginx:
    container_name: myata_nginx
    image: nginx
    volumes:
    - ./docker/conf/nginx:/etc/nginx/conf.d/
    - ./:/var/www
    ports:
      - 80:80
    depends_on:
      - app_php

  adminer:
    container_name: myata_adminer
    image: adminer
    restart: always
    depends_on:
      - db
    ports:
      - 8080:8080

  db:
    container_name: myata_mysql
    image: percona:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: on-failure
    ports:
    - 3306:3306
    expose:
      - 3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: myata
      MYSQL_USER: ruslan
      MYSQL_PASSWORD: kazan1811

Dockerfile
FROM php:7.3-fpm

RUN apt-get update && apt-get install -y \
        git \
        curl \
        wget \
        libzip-dev \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev zlib1g-dev libicu-dev g++ libmagickwand-dev --no-install-recommends libxml2-dev \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl \
    && docker-php-ext-install mbstring zip xml gd pdo_mysql \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && pecl install xdebug-2.7.1 \
    && docker-php-ext-enable xdebug

RUN wget https://getcomposer.org/installer -O - -q \
    | php -- --install-dir=/bin --filename=composer --quiet

WORKDIR /var/www

CMD ["php-fpm"]

test.nginx.conf
server {
#        listen 443 ssl http2;
  listen 80;
  server_name app-test.local;

        proxy_connect_timeout 3000;
        proxy_send_timeout 3000;
        proxy_read_timeout 3000;
        client_max_body_size 20m;
        send_timeout 300;
        set $path "/var/www/frontend/web";
        root $path;
        access_log /var/www/frontend/runtime/logs/app-test-access.log;
        error_log /var/www/frontend/runtime/logs/app-test-error.log;
        charset utf-8;
        sendfile        on;
        keepalive_timeout  65;
        gzip  on;
        gzip_min_length 1024;
        gzip_buffers 12 32k;
        gzip_comp_level 6;
        gzip_proxied any;
        gzip_types text/plain application/xhtml+xml text/xml application/xml application/xml+rss text/css application/javascript;
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$  {
        expires 1d;
        add_header Cache-Control "max-age=86400,  public";
        }

        location / {
                root $path;
                index index.php index.html;
                try_files $uri $uri/ /index.php?$args;
        }

        location ~* \.php$ {
                fastcgi_param SERVER_NAME $host;
                root $path;
                fastcgi_pass app_php:9000;
        fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $path$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_send_timeout 3000;
                fastcgi_read_timeout 3000;
       }
}

server {
        if ($host = app-test.local) {
                return 301 https://$host$request_uri;
        } # managed by Certbot

        listen 80;
        server_name myata.local;
        return 404; # managed by Certbot
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question