why xdebug doesn't work in apache

2019-07-28 16:57发布

问题:

/var/www/html/index.php

<?php
   echo 'a';
   echo 'a';

when index.php is execute in cli mode as below

$ php /var/www/html/index.php

xdebug worked fine

However, when execute by apache like

$ curl 'http://localhost:80?XDEBUG_SESSION_START=1'
aa

nothing happened to xdebug.

Below is the error log of apache, nothing special

1 [Fri Jun 17 05:02:15.526773 2016] [mpm_prefork:notice] [pid 30840] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.17 configured -- resuming normal operations │ qualified domain name, using 10.0.2.15. Set the 'ServerName' dir 2 [Fri Jun 17 05:02:15.526839 2016] [core:notice] [pid 30840] AH00094: Command line: '/usr/sbin/apache2'

I'm using Apache/2.4.7 , PHP/5.5.9 , Ubuntu , vim and DBGPavim in Vagrant virtual machine. How can I find out the reason why xdebug doesn't work via apache?

回答1:

After a lot of searching, this is what finally worked for me. Hope this helps !

I'm using PHP 7.0.21, Zend Engine v3.0.0 and Xdebug v2.5.0. I ran the following commands before restarting apache

# Step 1 : In xdebug.ini or php.ini enable xdebug
sed -i 's/;zend_extension=xdebug.so/zend_extension=xdebug.so/' /etc/php7/conf.d/xdebug.ini
echo -e "xdebug.remote_enable=1\nxdebug.remote_handler=dbgp\nxdebug.remote_mode=req\nxdebug.remote_host=127.0.0.1\nxdebug.remote_port=9000" >> /etc/php7/conf.d/xdebug.ini

# Step 2: Copy the php + xdebug configuration files to apache
cp /etc/php7/php.ini /etc/apache2/ && \
cp /usr/lib/php7/modules/xdebug.so /usr/lib/apache2 && \
cp /etc/php7/conf.d/xdebug.ini /etc/apache2/conf.d/ && \

My file layout after the fix was

/etc
|--php7
|     |-- php.ini
|     |-- conf.d
|              |-- xdebug.ini
|--apache2
|     |--php.ini
|     |-- conf.d
|              |-- xdebug.ini


/usr/lib
|--php7
|    |--modules
|          |--xdebug.so
|--apache2
|    |--modules
|          |--xdebug.so

Found this document really helpful.