Closed4

Dockerにphp-fpmコンテナ作るよ

ranran

はじめに

  • docker、docker composeはインストール済み
  • nginxコンテナは構築済み
ranran

Dockerfileを作る

ubuntu
mkdir php
ubuntu
vi php/Dockerfile
php/Dockerfile
FROM php:8.3-fpm-bullseye AS base

WORKDIR /var/www

RUN apt-get update && apt-get install -y \
    git \
    curl \
    zip \
    unzip \
    && docker-php-ext-install pdo_mysql

FROM base AS development

COPY php.ini-development /usr/local/etc/php/php.ini

FROM base AS production

COPY php.ini-production /usr/local/etc/php/php.ini

bullseyeってなに:

FROM ~ ASってなに:

環境ごとのphp.iniをつくる

ubuntu
curl -o php/php.ini-development https://raw.githubusercontent.com/php/php-src/master/php.ini-development
ubuntu
curl -o php/php.ini-production https://raw.githubusercontent.com/php/php-src/master/php.ini-production

developmentとproductionの違いってなに:

src/php.iniを作る

ubuntu
mkdir src
ubuntu
vi src/index.php
src/index.php
<?php

echo '<h1>Hello World</h1>';

compose.yamlを編集

ubuntu
vi compose.yaml
compose.yaml
services:                                                                                
  web:                                                                                   
    image: nginx:1.27.0                                                                  
    ports:                                                                               
      - ${WEB_PUBLISHED_PORT:-80}:80
    volumes:
      - ./src:/var/www/html
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app

  app:
    build:
      context: ./php
      dockerfile: Dockerfile
      target: ${APP_BUILD_TARGET:-development}
    volumes:
      - ./src:/var/www/html

${}ってなに:

.envを編集

ubuntu
vi .env
.env
WEB_PUBLISHED_PORT=8080
APP_BUILD_TARGET=development

nginx/conf.d/default.confを編集

ubuntu
vi nginx/conf.d/default.conf
nginx/conf.d/default.conf
server {
    listen       80;
    listen  [::]:80; .htaccess files, if Apache's document root
    server_name  localhost;one
    root /var/www/html;
    index index.php;t {
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
      root /var/www/html;
      index index.php;
      try_files $uri $uri/ /index.php?$query_string;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   app:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY "";
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

fastcgi_param HTTP_PROXY "";ってなに:

try_files uri $uri/ /index.php?query_string;ってなに:

ranran

再ビルド

ubuntu
docker compose up -d --build --remove-orphans

動作確認

ubuntu
$ curl http://localhost:8080
<h1>Hello World</h1>

動いてるネ

このスクラップは4ヶ月前にクローズされました