4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel8 + PHP8.1.9 + Nginx + MySql8.0.30で環境構築(Docker)

Last updated at Posted at 2022-09-07

うす、豚野郎です。
今まで、Dockerで一から環境構築をしたことがなかったので今回は行っていこうと思います。

Laravel8のファイルは以前、インストールしたものを使います。

1. ファイルの配置

以下の配置で、ファイルを作成していきます。

/docker_study
├─ /docker
| ├─ /nginx
|   └─ default.conf 
| └─ /php
|   ├─ Dockerfile 
|   └─ php.ini 
├─ /laravel8 # Laravelのソース
└─ docker-compose.yml

2. php.ini

php.ini
[Date]
#タイムゾーンの設定
date.timezone = "Asia/Tokyo"

[mbstring]
#文字コードの設定
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"

[opcache]
#phpの動作に関する設定(PHP公式おすすめ設定)
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

PHP公式のおすすめとのことで、内容は理解していませんが
今回はDockerを使った環境構築が目的なのでこれでよしとします。

3. Dockerfile

FROM php:8.1.9-fpm 
COPY php.ini /usr/local/etc/php/
RUN apt-get update && apt-get install -y \
zlib1g-dev \
libzip-dev \
vim \
&& docker-php-ext-install zip pdo_mysql opcache
WORKDIR /var/www
COPY --from=composer /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER 1 
ENV COMPOSER_HOME /composer
ENV PATH $PATH:/composer/vendor/bin
RUN composer global require "laravel/installer"

今回はPHPのバージョンは最新のPHP8.1.9を使います。

4. default.conf

default.conf
server {
  listen 80;
  root /var/www/laravel8/public;
  index index.php;
  
  location / {
    root /var/www/laravel8/public;
    index index.php;
    try_files $uri $uri/ /index.php$query_string;
  }
  
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass app:9000; 
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

5. docker-compose.yml

docker-compose.yml
version: "3"
services:
  app:
    container_name: app
    build: ./docker/php
    volumes:
      - .:/var/www
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - 8000:80
    volumes:
      - .:/var/www
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    working_dir: /var/www
    depends_on:
      - app
  db:
    # MySQL公式が用意しているDockerイメージ 8.0.30
    # https://gihyo.jp/dev/serial/01/mysql-road-construction-news/0167
    image: mysql/mysql-server:latest
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: database
      MYSQL_USER: db-user
      MYSQL_PASSWORD: db-pass
      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

  # http://localhost:4040/
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=db
      - PMA_USER=root
      - PMA_PASSWORD=root
    # dbのサービス名
    links:
      - db
    ports:
      - 4040:80
    volumes:
      - ./phpmyadmin/sessions:/sessions

version: "3"
services:
  app:
    container_name: app
    build: ./docker/php
    volumes:
      - .:/var/www
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - 8000:80
    volumes:
      - .:/var/www
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    working_dir: /var/www
    depends_on:
      - app
  db:
    # MySQL公式が用意しているDockerイメージ 8.0.30
    # https://gihyo.jp/dev/serial/01/mysql-road-construction-news/0167
    image: mysql/mysql-server:latest
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: database
      MYSQL_USER: db-user
      MYSQL_PASSWORD: db-pass
      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

  # http://localhost:4040/
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=db
      - PMA_USER=root
      - PMA_PASSWORD=root
    # dbのサービス名
    links:
      - db
    ports:
      - 4040:80
    volumes:
      - ./phpmyadmin/sessions:/sessions

6. 画面

・Laravelの初期画面
http://localhost:8000/

・phpMyAdmin
http://localhost:4040/

7. コマンド

・起動

$ docker-compose up -d

・終了

$ docker-compose down

以下のコマンドでコンテナに入れます。

$ docker-compose exec app bash

DBには以下のコマンドで接続できます。

$ docker-compose exec db bash
bash-4.4# mysql -u db-user  -p
Enter password: db-pass
mysql> 

8. 参考

https://tech-blog.rakus.co.jp/entry/20200908/docker
https://goat-inc.co.jp/blog/2478/

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?