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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Deploying Small Language Models on Sovereign Cloud
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Trending

  • Debugging senro Pipelines: Replay a Run, Shell Into a Live Step, Audit a Cache
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • MCP Is Coming to Your Desktop Applications
  • How (and Why) to Build Dynamic Data Pipelines

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Sep. 24, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Deploying Small Language Models on Sovereign Cloud
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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