Dockerでnginx, php-fpmを使用したとき、どうしてもPHPが動作しなかったのでメモ。
今回はDocker Composeを使用した。ディレクトリ及びファイル構成は以下の通り
nginx-php
│ docker-compose.yml
│
├─nginx
│ Dockerfile
│ server.conf
│
└─php-fpm
Dockerfile
index.php
docker-compse.yml
services:
nginx:
build: nginx/.
ports:
- "8080:80"
links:
- web
web:
build: php-fpm/.
nginx/Dockerfile
FROM nginx
RUN sed -i 's/server_name localhost/server_name _/g' /etc/nginx/conf.d/default.conf
ADD server.conf /etc/nginx/conf.d/server.conf
nginx/server.conf
server {
listen 80 default_server;
server_name localhost;
root /var/www/html;
index index.php index.html index.htm;
charset utf-8;
access_log off;
error_log off;
location / {
root /var/www/html;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass web:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
}
}
php-fpm/Dockerfile
FROM php:fpm
RUN sed -i 's/\[::\]/0.0.0.0/g' /usr/local/etc/php-fpm.d/zz-docker.conf
COPY index.php /var/www/html
php-fpm/index.php
<?php
phpinfo();
キモは
RUN sed -i 's/server_name localhost/server_name _/g' /etc/nginx/conf.d/default.conf
のところで、default.confのserver_nameを書き換えてるところ。
これを行わないとhttp://localhost:8080でアクセスした際にdefault.confの内容が優先されPHPが動作しなかった。
server.confのlistenを変えるなどでもいいかもしれないが最終的に80番ポートでlistenするにはこれがいいのではないかと。