You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose分离PHP与Apache服务,Atom中Xdebug调试配置求助

Fixing PHP Debugging with Docker Compose, Apache, PHP-FPM, and Atom

Let’s get your debugging setup working smoothly. I’ll go through your current config, spot the issues, and give you corrected, optimized versions along with testing steps.

1. Fix Docker Compose Configuration

Your current docker-compose.yml has a couple of issues that can break connectivity and compatibility:

  • Mapping port 9000 on the Apache container is unnecessary (and can conflict with Xdebug) — Apache communicates with PHP-FPM via the internal Docker network, not through a published port.
  • volumes_from is deprecated; we’ll replace it with a shared volume approach for clarity.

Here’s the revised docker-compose.yml:

version: '3.8'
services:
  httpd:
    image: httpd:latest
    ports:
      - 8080:80
    volumes:
      - ./docker/httpd/conf:/usr/local/apache2/conf
      - ./code:/usr/local/apache2/htdocs  # Share code directly instead of volumes_from
    depends_on:
      - php
    networks:
      - php-app-network

  php:
    build: ./docker/php/
    expose:
      - 9000
    volumes:
      - ./code:/usr/local/apache2/htdocs
    networks:
      - php-app-network

networks:
  php-app-network:
    driver: bridge

Key changes:

  • Removed the 9000:9000 port mapping from Apache
  • Replaced volumes_from with direct shared volume for the code directory
  • Added a dedicated Docker network to ensure reliable service discovery between Apache and PHP-FPM

2. Optimize PHP Dockerfile Xdebug Configuration

Your Dockerfile uses repeated sed commands which can lead to duplicate settings (you have xdebug.remote_autostart set twice). Let’s rewrite this to be cleaner and more reliable:

FROM php:7.2-fpm
RUN docker-php-ext-install pdo_mysql json mysqli

# Install and configure Xdebug properly
RUN pecl install xdebug-2.9.8  # Specify compatible Xdebug version for PHP 7.2
RUN docker-php-ext-enable xdebug

# Write Xdebug config in one go to avoid duplicates
RUN cat <<EOF >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
xdebug.remote_enable=1
xdebug.remote_host=host.docker.internal  # Use this for Docker Desktop (Windows/macOS); for Linux, use your host's internal IP
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_connect_back=0
xdebug.remote_autostart=1
xdebug.idekey=xdebug-atom
xdebug.log=/tmp/xdebug.log  # Add logging to debug connection issues
EOF

Key improvements:

  • Specified a compatible Xdebug version (2.9.8 works best with PHP 7.2)
  • Consolidated all Xdebug settings into a single cat command to avoid duplicates
  • Added xdebug.log to help diagnose connection failures
  • Used host.docker.internal (Docker Desktop) instead of hardcoded IP — for Linux, replace this with your host machine’s internal IP (e.g., 192.168.1.100)

3. Complete Atom php-debug Plugin Configuration

Your PathMaps are incomplete — this is critical for mapping remote files (in the container) to your local Atom project files. Here’s the full, correct config:

Go to Atom → Settings → Packages → php-debug → Settings, then update the settings to:

"php-debug": {
  "PathMaps": [
    "/usr/local/apache2/htdocs;/path/to/your/local/code/directory"
  ],
  "ServerPort": 9000,
  "IdeKey": "xdebug-atom"
}

Replace /path/to/your/local/code/directory with the absolute path to your ./code folder on your machine (e.g., /Users/you/projects/my-php-app/code on macOS, C:\projects\my-php-app\code on Windows).

4. Verify Apache Configuration

Make sure your Apache httpd.conf (in ./docker/httpd/conf) has the necessary mod_proxy_fcgi setup to forward PHP requests to PHP-FPM:

Add these lines to the end of httpd.conf:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

# Forward PHP requests to the PHP-FPM service
ProxyPassMatch ^/(.*\.php)$ fcgi://php:9000/usr/local/apache2/htdocs/$1

This tells Apache to send all .php requests to the php service (via the Docker network) on port 9000.

5. Test the Setup

  1. Rebuild and start your containers:
    docker-compose down && docker-compose build && docker-compose up -d
    
  2. Verify Xdebug is loaded in the PHP container:
    docker-compose exec php php -m | grep xdebug
    
    You should see xdebug in the output.
  3. Set a breakpoint in Atom in a PHP file (e.g., index.php in your ./code folder).
  4. Start the php-debug listener in Atom (click the bug icon in the bottom toolbar, or use the command palette: PHP Debug: Start Listening).
  5. Visit http://localhost:8080/index.php in your browser — Atom should hit the breakpoint and pause execution.

If you still have issues, check the Xdebug log in the container:

docker-compose exec php cat /tmp/xdebug.log

This will show connection attempts and any errors.

内容的提问来源于stack exchange,提问作者Shahzad Fateh Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:03:29