鉢本 一二のブログ

Docker + nginx + mariadb + wordpress を構築をしてみた

作成日 :
更新日 :
folder docker
local_offer docker,wordpress,nginx

wordpress のコンテナを新規で構築した時の方法をメモ。
とりあえず、動くことを目的としている。

ホストの環境

使用するイメージとバージョン

ファイルとフォルダ構成

任意のフォルダ/
    ├ .env
    ├ db-data/
    ├ docker-compose.yml
    ├ nginx/
    │   ├ log/
    │   ├ nginx/
    │   │   └ default.conf
    │   └ nginx.conf
    └ wordpress/

各ファイルの設定

docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx:1.19.6
    volumes:
      - "./nginx/nginx.conf:/etc/nginx/nginx.conf"
      - "./nginx/conf.d:/etc/nginx/conf.d"
      - "./nginx/log:/var/log/nginx"
      - "./wordpress:/var/www/html"
    ports:
      - "${NGINX_HOST_PORT}:80"
    depends_on:
      - wordpress
    restart: always
    container_name: nginx
  wordpress:
    image: wordpress:5.6-php7.4-fpm
    depends_on:
      - db
    volumes:
      - "./wordpress:/var/www/html"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: ${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE}
    container_name: wordpress
  db:
    image: mariadb:10.5.8
    restart: always
    volumes:
      - "./db-data:/var/lib/mysql"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      TZ: Asia/Tokyo
    ports:
      - 3306:3306
    container_name: db
volumes:
    db:

.env

# MySQL
MYSQL_ROOT_PASSWORD=passowrd
MYSQL_DATABASE=wordpress
MYSQL_USER=root
MYSQL_PASSWORD=password

# nginx
NGINX_HOST_PORT=8080

nginx.conf

nginxのインストール時に生成される nginx.conf をそのまま使っている。

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

default.conf

server {
    listen 80;
    server_name localhost;
    charset utf-8;

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

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location ~* /wp-config.php {
        deny all;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(\.+)$;
        fastcgi_index index.php;
        fastcgi_pass wordpress:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~* .(html|css|js|jpe?g|png|gif|svg|mpg|flv|swf)$ {
        expires max;
        access_log off;
    }
}

docker を実行

docker-compose up -d

コンテナ立ち上げでエラーがなければ、localhost:8080 にアクセス。