Home>

I want to build a PHP execution environment on nginx using Docker and create a new Laravel project.

The laravel project creation itself was successful,localhostAccess to nginx404 Not FoundOccurs.

I did not understand it even if I checked it, so I will ask you a question. Nice to meet you.

Expected value

Display a laravel demo screen

Constitution

project
├ docker-compose.yml
├ docker
│ ├ php
│ │ ├ php.ini
│ │ └ Dockerfile
│ └ nginx
│ └ default.conf
└ server
The laravel project is included in the server.

The actual source
//docker-compose.yml
version: '3'
services:
  php:
    container_name: php
    build: ./docker/php
    volumes:
    -./server:/var/www
  nginx:
    image: nginx
    container_name: nginx
    ports:
    -80:80
    volumes:
    -./server:/var/www
    -./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
    -php
  db:
    image: mysql:5.7
    container_name: db-host
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: database
      MYSQL_USER: docker
      MYSQL_PASSWORD: docker
      TZ:'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
    -./docker/db/data:/var/lib/mysql
    -./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
    -./docker/db/sql:/docker-entrypoint-initdb.d
    ports:
    -3306:3306
FROM php:7.2-fpm
COPY php.ini /usr/local/etc/php/
RUN apt-get update \
 &&apt-get install -y zlib1g-dev mariadb-client \
 &&docker-php-ext-install zip pdo_mysql
#Composer install
COPY --from=composer /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
ENV PATH $PATH:/composer/vendor/bin

WORKDIR /var/www
RUN composer global require "laravel/installer"
//default.conf
server {
  listen 80;
    index index.php index.html;
    root /var/www/public;
  location/{
    root /var/www/public;
    index index.html index.php;
    try_files $uri $uri/ /index.php$query_string;
    }
  location ~ \.php${
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
  }
 }
The executed command
$docker-compose up -d
$docker-compose exec php bash
$composer create-project laravel/laravel
What I tried
  • Not Found, so create a project in /var/www/public
    →403 Forbidden error
# ls -ld /var/www/public/project/
drwxr-xr-x 26 root root 832 Jun 29 14:05 /var/www/public/project/ // Has execute permission. The root directory also has execute permission.
Referenced site

https://qiita.com/A-Kira/items/1c55ef689c0f91420e81

  • Answer # 1

    $composer create-project laravel/laravel

    If you run/var/www/laravelThe Laravel project will be installed in this directory.
    default.confof/var/www/laravel/publicAnd you need to change and build and regenerate the image.

    Or

    $composer create-project laravel/laravel.

    Then the Laravel project will be installed in the current directory.

    From this point onward, it has nothing to do with the main subject, but I will supplement it. default.confAbout the supplement.

    There are recommended settings from the official website, so we recommend that you refer to them.
    https://readouble.com/laravel/7.x/ja/deployment.html

    docker/php/DockerfileAbout the supplement.
    COPY php.ini /usr/local/etc/php/

    php.iniSince it is easy to change, it is recommended to execute the COPY command as soon as possible.
    If you set it to the beginning, the cache efficiency is not good.


    RUN apt-get update \
     &&apt-get install -y zlib1g-dev mariadb-client \
     &&docker-php-ext-install zip pdo_mysql

    Laravel server requirements must meet the following requirements:
    https://readouble.com/laravel/7.x/ja/installation.html

    PHP >= 7.2.5
    BCMath PHP extension
    Ctype PHP extension
    Fileinfo PHP extension
    JSON PHP extension
    Mbstring PHP extension
    OpenSSL PHP extension
    PDO PHP extension
    Tokenizer PHP extension
    XML PHP extension

    By default, BCMath PHP extension library is not enough, so please add it.
    Also,mariadb-clientI don't think I need it.

    &&docker-php-ext-install zip pdo_mysql bcmath

    ENV COMPOSER_ALLOW_SUPERUSER 1
    ENV COMPOSER_HOME /composer
    ENV PATH $PATH:/composer/vendor/bin

    A layer is created each time the ENV instruction is executed, so you can save the number of layers by executing with one liner as shown below.

    ENV COMPOSER_ALLOW_SUPERUSER=1 \
      COMPOSER_HOME=/composer \
      PATH=$PATH:/composer/vendor/bin

    RUN composer global require "laravel/installer"

    We have introduced the Laravel installer, but the latest Laravel installer only supports PHP 7.3 and later versions.
    In addition, PHP7.2 will run out of security support in about four months, so if there is no particular reason, we recommend using the latest version 7.4.