神戸学院大学 経営学部 林坂ゼミ

Docker 入門トップページ

« 戻る 次へ »

Docker 入門

さまざまな Web サーバを Docker Compose で構築する

nginx で PHP を動かす

次は nginx で PHP プログラムを動かすサンプルを試してみます.なお,このページのサンプルコードは GitHub の web08NginxPHP フォルダで公開しています.これまでと同様に新規ディレクトリを作成し,次のファイルやディレクトリを作成します.

% tree -a -F ⏎
./
├── docker-compose.yml
├── nginx/
│   └── default.conf
├── Readme.md
└── web/
    └── html/
        └── index.php

4 directories, 4 files

全体の設計図であるである docker-compose.yml を準備します.ここでは php:8.4-fpm (FastCGI Process Manager) というイメージを使用します.このイメージには Web サーバが含まれていないので,nginx と組み合わせて利用します.これは高速なレスポンスが求められる本番環境に適しています.

docker-compose.yml
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./web/html:/var/www/html
    depends_on:
      - php

  php:
    image: php:8.4-fpm
    volumes:
      - ./web/html:/var/www/html

次に nginx の設定ファイルを準備します.

nginx/default.conf
server {
    listen 80;
    server_name localhost;

    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
    }
}

さらに PHP プログラムを含んだトップページを準備します.

web/html/index.php
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Nginx PHP Test</title>
</head>
<body>
  <?php echo '<h1>Hello Nginx PHP</h1>'; ?>
  <p>
    このサイトは PHP プログラムを Nginx で動かしています.
  </p>
</body>
</html>

すべてのファイルの準備ができたらコンテナを起動します.

% docker compose up -d ⏎
[+] Running 13/13
 ✔ php Pulled                                                              15.5s
   ✔ b2feff975e6d Already exists                                            0.0s
   ✔ 5ece5e8db45d Pull complete                                             0.9s
   ✔ e5378f2f406c Pull complete                                            11.5s
   ✔ 2f485dc7ef4b Pull complete                                            11.5s
   ✔ 326fe30853c1 Pull complete                                            11.6s
   ✔ 5713fbd7efe3 Pull complete                                            11.6s
   ✔ 92ca1b1ce7ba Pull complete                                            11.9s
   ✔ efbb6b6f9fa8 Pull complete                                            11.9s
   ✔ 67d0733b2713 Pull complete                                            11.9s
   ✔ 69bf7eb47177 Pull complete                                            11.9s
   ✔ 4f4fb700ef54 Pull complete                                            11.9s
   ✔ 6306d88fe570 Pull complete                                            11.9s
[+] Running 3/3
 ✔ Network web08nginxphp_default    Crea...                                 0.0s
 ✔ Container web08nginxphp-php-1    Star...                                 0.5s
 ✔ Container web08nginxphp-nginx-1  St...                                   0.2s
%

新たな Docker イメージがダウンロードされたことが分かります.

% docker image ls ⏎
REPOSITORY   TAG          IMAGE ID       CREATED       SIZE
php          8.4-fpm      2f47f19aea32   3 weeks ago   505MB
nginx        latest       17848b7d08d1   5 weeks ago   198MB
httpd        latest       8875809932eb   6 weeks ago   147MB
php          8.2-apache   b187632cf9b0   6 weeks ago   513MB
%

コンテナの起動が確認できたので Web ブラウザで http://localhost/ に接続します.

docker-2025-web-16

動作確認を終えたらコンテナを終了,破棄します.

% docker compose down ⏎
[+] Running 3/3
 ✔ Container web08nginxphp-nginx-1  Re...                                   0.2s
 ✔ Container web08nginxphp-php-1    Remo...                                 0.1s
 ✔ Network web08nginxphp_default    Remo...                                 0.2s
% docker container ls -a ⏎
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
%