1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerインスタンス連携におけるNginx設定 (PHP/nginx/MySQL)

1
Posted at

はじめに

devcontainerPHPのフルスタックアプリケーション開発環境を作成しました。
その過程でnginxの設定に詰まったので振り返りを兼ねて共有します!

結論

nginx, PHPとディレクトリを分けた状態でDockerインスタンスを連携させるにはDocumentRootの設定を追加し、nginx.confrootのパスを修正する必要がありました。

達成したかったこと

nginxを使いhttp://localhost/ にアクセスしたら index.phpのページが表示されるようにしたいと思いました。

ソースコードのディレクトリは以下の通りです。

my-php-app/
│
├── .devcontainer/                  
│   ├── devcontainer.json   # 開発環境の設定      
│   └── docker-compose.yml  # php, nginx, dbと各コンテナを設定         
│
├── nginx/                  
│   ├── default.conf        # phpアプリのルーティングを設定  
│   └── Dockerfile 
│
├── php_code/                  
│   ├── index.php           # / にアクセスした際に表示されるページ
│   └── Dockerfile
│
└── README.md                # プロジェクトの説明

修正前のdocker-compose.yml, nginx.confの設定は以下のとおりです。

docker-compose.yml

version: "3.9"
services:
	nginx:
	build: ../nginx/
	ports:
		- 80:80
	volumes:
		- ../:/var/www/html/

php:
	build: ../php_code/
	volumes:
		- ../:/var/www/html/

db:
	image: mariadb
	volumes:
		- mysql-data:/var/lib/mysql
	environment:
		MYSQL_ROOT_PASSWORD: mariadb
		MYSQL_DATABASE: ecomdb

volumes:
	mysql-data:

nginx.conf

server {
	listen 80 default_server;
	root /var/www/html;
	index index.html index.php;
	
	charset utf-8;
	
	location / {
	proxy_pass http://php;
	}
	
	location = /favicon.ico { access_log off; log_not_found off; }
	location = /robots.txt { access_log off; log_not_found off; }
	
	access_log off;
	error_log /var/log/nginx/error.log error;
	
	sendfile off;
	
	client_max_body_size 100m;
	
	location ~ /.ht {
	deny all;
	}
}

この設定でDockerコンテナを立ち上げ、 http://localhost/ にアクセスするとindex.phpにアクセスすることができませんでした。ですが、index.phpファイルをルート配下に移動するとアプリを表示することができました。しかし、ディレクトリを分けることでコードの可読性を向上させたく、index.php/php_code配下に置きたいと思いました。

index.php/php_code配下に置いた際のログは次のとおりです。

[Thu Jun 04 19:13:46.527416 2026] [autoindex:error] [pid 16:tid 16] [client 172.18.0.2:45812] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive

解決

設定修正箇所は2箇所ありました。

  1. DocumentRootを変更
  2. nginx.confrootのパスが/php_codeを指すように変更

まず1についてです。
nginxのDockerインスタンスからphpのDockerインスタンスにアクセスするため、nginx/default.confで次のように設定しています。

proxy_pass http://php;

ですが、PHPはデフォルトでドキュメントのルートとして/var/www/htmlを指すため、コンテナのvar/www/html/php_code/配下にあるindex.phpにアクセスすることができませんでした。
このDocumentRootを変更するため、PHPコンテナのDockerfileに次の設定を追加します。

# DocumentRoot を php_code に変更

ENV APACHE_DOCUMENT_ROOT=/var/www/html/php_code

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf \

&& sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

次に2についてです。
1 は http://localhost/ にアクセスした際の振る舞いなので、他のページに遷移しようとしても失敗します。
これを解消するためにnginxdefault.confにてドキュメントルートを変更する必要があります。
具体的には次のように変更します。

root /var/www/html/php_code;

これでURLからPHPアプリのページを表示し、別のページに遷移ができるようになりました!
もし何かお気づきのことがあればご指摘いただけると幸いです。

参考リンク

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?