DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Smart Deployment Strategies for Modern Applications
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Java Backend Development in the Era of Kubernetes and Docker

Trending

  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Identity in Action
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Setup Wordpress Debug Environment With Docker and Xdebug

Setup Wordpress Debug Environment With Docker and Xdebug

By 
Kunkka Li user avatar
Kunkka Li
·
Sep. 24, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

It's a common practice to debug PHP using Xdebug. Xdebug is an extension for PHP to assist with debugging and development. 

In this article, I'm going to introduce a more convenient way to quickly set up development environment with PHPStorm, Docker, Docker compose and Xdebug.  The source code for docker compose configs are available in the Github: https://github.com/liqili/wordpress-xdebug-docker/ 

First of all, suppose we have installed Docker and Docker compose in a Linux-like OS, and installed a Wordpress site in production. Then, what we need to do is to setup a development environment for that site so that we can customize plugins or what else. This post will help you learn how to:

  • Use Docker compose to mange docker containers.
  • Export production db to dev Mysql docker container.
  • Integrate Xdebug plugin with docker container as well as PHPStorm.
  • How to initialize MySQL container with sql script file. 

docker-compose.yml for container management

YAML
xxxxxxxxxx
1
33
 
1
version: "3"
2
3
services:
4
  mysql:
5
    image: mysql:5.7
6
    container_name: mysqldb
7
    ports:
8
      - "3306:3306"
9
    command: --max_allowed_packet=1073741824 --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
10
    environment:
11
      MYSQL_ROOT_PASSWORD: 123
12
      MYSQL_DATABASE: log
13
    volumes:
14
      - ./configs/db/:/docker-entrypoint-initdb.d # mysql will execute the initial sql script under the mount point one by one
15
      - db-data:/var/lib/mysql
16
  wordpress-app:
17
    build:
18
      ./configs/php/
19
    container_name: wordpress-app
20
    ports:
21
      - "80:80"
22
      - "443:443"
23
    restart: always
24
    environment:
25
      XDEBUG_CONFIG: "remote_host=host.docker.internal" #This config works for MacOS, otherwise should be ip address of the host machine because docker container cannot find the host by localhost.
26
    volumes:
27
      - ../local-wordpress:/var/www/html # mount your local wordpress site here
28
      - /tmp/wp-errors.log:/tmp/wp-errors.log
29
    depends_on:
30
      - mysql
31
32
volumes:
33
  db-data:

Customize Dockerfile for Apache Web Server

We need to download Xdebug source code and prepare a customized php.ini and dockerfile:

Download the specific version Xdebug source code according to your php version, in this post I'm using php7.4, so I use xdebug-2.9.6(https://xdebug.org/files/xdebug-2.9.6.tgz).

 Dockerfile for php apache web server(please modify php.ini and xdebug location accordingly):

Dockerfile
xxxxxxxxxx
1
19
 
1
FROM php:7.4-apache
2
COPY ./local/php.ini /usr/local/etc/php/php.ini
3
COPY ./xdebug-2.9.6 /app/xdebug
4
RUN apt-get update && apt-get install -y \
5
curl \
6
mariadb-client \
7
wget \
8
zlib1g-dev \
9
libzip-dev \
10
unzip \
11
libmemcached-dev zlib1g-dev \
12
&& docker-php-ext-install pdo pdo_mysql mysqli zip \
13
&& a2enmod rewrite \
14
&& cd /app/xdebug/ \
15
&& phpize \
16
&& ./configure \
17
&& make \
18
&& mkdir -p /usr/local/lib/php/extensions/no-debug-non-zts-20190902/ \
19
&& cp modules/xdebug.so /usr/local/lib/php/extensions/no-debug-non-zts-20190902/ \


Customize php.ini by adding the following lines to enable xdebug.

Plain Text
xxxxxxxxxx
1
 
1
xdebug.remote_enable=1
2
zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so

Configure your IDE(PHPStorm)

I get used to JetBeans IDEs, so I will only use PHPStorm to illustrate how to configure you IDE. For other IDEs, there are a lot of manuals available that can help with the Xdebug configuration. 

Xdebug configuration

Finally, we need to install browser debug toolbar. Once installed, configure the option to set IDE Key to PHPSTORM. 

IDE key


You will be all set.  Then you can start the docker containers and start debug in your IDE.

Shell
xxxxxxxxxx
1
 
1
docker-compose up --build //build and start up docker containers


Docker (software) Debug (command) WordPress

Opinions expressed by DZone contributors are their own.

Related

  • Smart Deployment Strategies for Modern Applications
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • How We Diagnosed a Hidden Scheduler Failure in a Docker Swarm Cluster Serving 2 Million Users
  • Java Backend Development in the Era of Kubernetes and Docker

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook