4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dockerで(nginxと)php-fpmを連携させたときのメモ

Last updated at Posted at 2019-02-06

nginxの環境を __docker-compose__で構築済。後にPHP(Laravel)のプロジェクトを動作させたいためPHPを実行する環境が必要。調べてみるとphp-fpmと言うモノが必要らしい。

#前提
nginxはド素人。PHP(Laravel)の環境を作る必要があり、使ったことが無いnginxで動かしてみようという軽いノリで始めたメモ。

すでに、nginxに関するメモ有り。
今回は、既に作成したnginxのコンテナとphpのコンテナを連携させる為のメモ。

#docker-compose.yml

docker-compose.yml
version: '3'

services:
  web:
    build:
      context: ./docker
      dockerfile: web.docker
    container_name: web00
    depends_on:
      - app00
    ports:
      - 8080:80
    volumes:
      - ./proj/var/www/html:/var/www/html
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
    networks:
      - proj-net
  app:
    build:
      context: ./docker
      dockerfile: app.docker
    container_name: app00
    volumes:
      - ./src/var/www/html:/var/www/html
    networks:
      - proj-net
network:
  - proj-net

#./docker/app.docker
PHPからMySQLに接続する予定なので docker-php-ext-install pdo_mysqlを実行する。

app.docker
FROM php:fpm
RUN docker-php-ext-install pdo_mysql

#./docker/nginx.conf

PHPファイルにアクセスされた際に、app00に連携されるようにnginx.confに location ~ \.php$ {...}を追記した。

nginx.conf
server {
    listen       80;
    server_name  web00;
    root         /var/www/html;

    location / {
        # root   /usr/share/nginx/html;
        # index  index.html index.htm;
        root   /var/www/html;
        index  index.php index.html index.htm;
    }

    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        # root   /usr/share/nginx/html;
		root   /var/www/html;
    }
    
    location ~ \.php$ {
       fastcgi_pass   app00:9000;
       fastcgi_index  index.php;
       include        fastcgi_params;
       fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
    }

}
4
7
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
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?