Docker Compose分离PHP与Apache服务,Atom中Xdebug调试配置求助
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_fromis 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:9000port mapping from Apache - Replaced
volumes_fromwith 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
catcommand to avoid duplicates - Added
xdebug.logto 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
- Rebuild and start your containers:
docker-compose down && docker-compose build && docker-compose up -d - Verify Xdebug is loaded in the PHP container:
You should seedocker-compose exec php php -m | grep xdebugxdebugin the output. - Set a breakpoint in Atom in a PHP file (e.g.,
index.phpin your./codefolder). - Start the php-debug listener in Atom (click the bug icon in the bottom toolbar, or use the command palette:
PHP Debug: Start Listening). - Visit
http://localhost:8080/index.phpin 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

