docker+nginx+phpの環境を作る

Dockerでコンテナ化したNginxで、スタティックなコンテンツだけではなく、動的なコンテンツを表示したくなり、Docker+Nginxの環境で更にPHPを追加しました。その方法をまとめました。

目次

Docker + Nginxの環境を構築する

はじめにDocker+Nginxの環境を作ります。NginxをDockerで動かす方法については、以下の記事をご覧ください。

また、Nginxの設定ファイルを編集する必要があります。Nginxの設定ファイルをコンテナからコピーして、編集し、使用す方法については、以下の記事をご覧ください。

この記事では、上記の2つの記事の作業が完了していて、Docker+Nginxの環境が出来ていて、設定ファイルを編集可能な状態になっていることを前提にしています。

Docker ComposeでPHPのコンテナを追加

Docker ComposeでNginxに加えて、PHPのコンテナを追加します。次のようにdocker-compose.ymlを編集し、phpの設定を追加します。

version: "3"
services:
    nginx:
        build: .
        ports:
            - 8080:8080
        volumes:
            - ./content_home:/usr/share/nginx/html

    php:
        image: php:8-fpm
        volumes:
            - ./content_home:/usr/share/nginx/html

追加したのは、php:以下の部分です。

それと、こちらはどちらでも良いのですが、動作確認環境の都合でnginx:portsのポート番号も変更しました。

Nginxの設定

Nginxの設定を変更します。default.confファイルを次のように編集します。

server {
    listen       8080;
    listen  [::]:8080;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

	root   /usr/share/nginx/html;
	index  index.html index.htm index.php;

    location / {
    	try_files $uri $uri/ /index.php$is_args$args;
    }
    
    #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$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

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

変更を加えたのは以下の場所です。

  • rootの定義場所を移動。(location /のスコープ外に移動)
  • indexの定義場所を移動。(rootと同様)
  • indexindex.phpを追加。
  • location /の定義を変更。
    • try_filesの定義を追加。
  • location ~ \.php?の定義を追加。
    • デフォルトでコメントアウトされていた設定をコメント解除。
    • root/usr/share/nginx/html;に変更。
    • fastcgi_passphp:9000に変更。
    • fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name;に変更。

動作テスト

上手く動作するかテストします。content_homeindex.phpファイルを作成し、以下の内容を入力します。

<?php
	echo phpinfo();
?>

次にターミナルで以下を実行します。

docker-compose up -d --build


ブラウザでhttp://localhost:8080/index.phpを開きます。成功すると次のようにphpinfo()の出力結果が表示されます。

phpinfo()の出力結果

著書紹介

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

目次