Local Dev Setup

My local WordPress environment setup is based on Docker. The main advantage I get from using Docker is control of the versions of PHP and MySql as they vary depending on the Hosting Websites.

The steps I take when developing a site are:

  1. Run setup bash file
    • Initializes site directory
    • Downloads the latest WordPress
    • Copies default themes & plugins into site directory
    • Copies wp-config.php & docker compose files into site directory
    • Copies .gitignore file and initializes a git repo in the site directory
    • Creates initial git commit
  2. Develop site locally
  3. Use Duplicator Plugin to move site to Production

This process works best for completely new websits. In cases when I have existing pages or maybe have to fix up an existing site, I make some minor tweaks. For example, I may have to change the versions of the docker images or update the wp-config to enable debugging and also to use local credentials.

Here is the setup.sh that I use to initialize a new site:

while getopts s: flag
do
    case "${flag}" in
        s) site=${OPTARG};;
    esac
done
cd ..
mkdir $site
cd $site
#create data directory
mkdir data

#pull latest WordPress
git init &&\
curl https://raw.githubusercontent.com/github/gitignore/master/WordPress.gitignore >.gitignore &&\
curl -LO https://wordpress.org/latest.zip &&\
unzip latest.zip &&\
rm latest.zip &&\
cp ../base/wp-config.php ./wordpress/wp-config.php
cp ../base/docker-compose.yml ./docker-compose.yml

#copy plugins & themes as well
cp -R ../base/plugins/* ./wordpress/wp-content/plugins
cp -R ../base/themes/* ./wordpress/wp-content/themes


git add --all &&\
git commit -sq -m "Initial commit of WordPress core files."

My docker-compose.yml file then looks like this:

services:
  db:
    image: mariadb:10.6.4-focal
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - ./data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
    expose:
      - 3306
      - 33060
  wordpress:
    image: wordpress:latest
    ports:
      - 80:80
    restart: always
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
    volumes:
      - ./wordpress:/var/www/html

In the compose file, I specify volumes for the mysql and wordpress containers, so that each site directory contains all the data for that website.

Keep an eye out for a seperate post on the plugins and themes that I use as defaults.

Leave a Reply

Your email address will not be published. Required fields are marked *