DEV Community

Cover image for Create simple Wordpress site from Docker-Compose
wadjakorn
wadjakorn

Posted on • Updated on

Create simple Wordpress site from Docker-Compose

prerequisites

EZ Step

1.create work directory
mkdir simple-wordpress

2.create docker-compose.yml file in work directory

  • cd simple-wordpress
  • vim docker-compose.yml

3.config docker-compose.yml

version: "3.5"
services:
  mariadb:
    image: mariadb:latest
    container_name: ${APP_NAME}_db
    networks:
      - backend
    restart: always
    environment:
      - TZ=${TZ}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    command:
      - "--character-set-server=utf8mb4"
      - "--collation-server=utf8mb4_unicode_ci"
      - "--max-connections=512"
      - "--innodb-buffer-pool-size=256M"
      - "--innodb-log-buffer-size=32M"
      - "--innodb-file-per-table=1"
    ports:
      - "${MYSQL_EXPOSE_PORT}:3306"
    volumes:
      - "${DB_PATH}:/var/lib/mysql"
    logging:
      driver: json-file
      options: { max-size: "200k", max-file: "1" }
  wordpress:
    image: wordpress:5-fpm
    container_name: ${APP_NAME}_website
    networks:
      - frontend
      - backend
    restart: always
    environment:
      - TZ=${TZ}
      - WORDPRESS_DB_HOST=mariadb:${MYSQL_EXPOSE_PORT}
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_NAME=${MYSQL_DATABASE}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
    volumes:
      - ${WEBSITE_STORAGE}:/var/www/html
    logging:
      driver: json-file
      options: { max-size: "200k", max-file: "1" }
  nginx:
    image: nginx:alpine
    container_name: ${APP_NAME}_nginx
    networks:
      - frontend
    restart: always
    environment:
      - TZ=${TZ}
    ports:
      - "${NGINX_EXPORT_HTTP_PORT}:80"
    volumes:
      - ./nginx/wordpress.conf:/etc/nginx/conf.d/default.conf:ro
      - ${WEBSITE_STORAGE}:/var/www/html
    logging:
      driver: json-file
      options: { max-size: "200k", max-file: "1" }

networks:
  frontend:
    name: ${APP_NAME}_frontend
  backend:
    name: ${APP_NAME}_backend
Enter fullscreen mode Exit fullscreen mode

4.create .env file

  • vim .env
TZ=Asia/Bangkok
APP_NAME=simple-wordpress

MYSQL_USER=wp_user
MYSQL_DATABASE=wp
MYSQL_PASSWORD=wp_password
MYSQL_ROOT_PASSWORD=root_password
MYSQL_EXPOSE_PORT=3306

NGINX_EXPORT_HTTP_PORT=127.0.0.1:80

DB_PATH=./db

WEBSITE_STORAGE=./site
Enter fullscreen mode Exit fullscreen mode

5.create nginx config file

  • in your root project mkdir nginx
  • cd nginx
  • vim wordpress.conf
server {
    listen 80 default;

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

    server_name localhost;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
Enter fullscreen mode Exit fullscreen mode

6.in terminal type command docker-compose up -d

7.browse to localhost

If you like this, just ❤️ it.

Top comments (0)