skip to Main Content

I’m trying to do some PHP profiling using Xdebug on a local environment. I’m running Ubuntu 22.04

I’m using PHP-FPM to run PHP v7.2 through a virtualhost.

php -v outputs

Zend Engine v4.1.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.7, Copyright (c), by Zend Technologies
    with Xdebug v3.1.4, Copyright (c) 2002-2022, by Derick Rethans

On /tmp/xdebug I’ve run

sudo chown $USER:$USER /tmp/xdebug

At the bottom of my /etc/php/7.2/fpm/php.ini & /etc/php/8.1/apache2/php.ini files I’ve put:

[xdebug]
zend_extension=xdebug
xdebug.output_dir = /tmp/xdebug
xdebug.remote_port=9001
xdebug.mode=develop,trace,profile

I then run

sudo systemctl restart apache2

The enabled Xdebug features are showing up in phpinfo() in my main /localhost/ directory (using PHP 8.*), but not in the one using PHP 7.2

In my virtualhost .conf file I’m setting the PHP version like (I’m including this because I’m not sure if I need to do anything in this .conf file or not)

<FilesMatch .php$>
        # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server
        SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>

The image below shows phpinfo() picking up my Xdebug settings on PHP 8.*; the directory using PHP 7.2 shows no Xdebug settings enabled.

XDEBUG FEATURES

My question(s) / Issues:

  • How do I get PHP 7.2 to recognize my Xdebug settings?
  • How do I get Xdebug to output profile files to /tmp/xdebug? (I plan to use Webgrind)

2

Answers


  1. Chosen as BEST ANSWER

    In case it helps someone, I made two basic mistakes. Thank you @Derick

    Mistake #1: My xdebug profiling directory should have been owned by www-data

    sudo chown www-data /tmp/xdebug
    

    Mistake #2: I forgot to restart PHP 7.2 PHP-FPM

    sudo systemctl restart php7.2-fpm
    

    So, compiled, the steps I took to get Xdebug working with PHP 7.2 PHP-FPM (Assumes installed LAMP environment)

    1 - Install Xdebug (I'm on Ubuntu)

    sudo apt-get install php-xdebug
    

    2 - Create the directory to store profiling files & give Apache ownership

    sudo mkdir /tmp/xdebug
    sudo chown www-data /tmp/xdebug
    

    3 - Enable Xdebug via php.ini (you can use phpinfo() to determine your .ini file location)

    sudo nano /etc/php/7.2/fpm/php.ini 
    

    Add the following at the bottom of the .ini file (see settings documentation for your needs)

    [xdebug]
    zend_extension=xdebug
    xdebug.output_dir = /tmp/xdebug
    xdebug.remote_port=9001
    xdebug.mode=develop,trace,profile
    

    4 - Restart PHP-FPM & Apache

    sudo systemctl restart php7.2-fpm
    sudo systemctl restart apache2
    

  2. I’m using PHP-FPM to run PHP v7.2

    PHP 7.2 is no longer supported, so please upgrade to at least PHP 7.4, but preferably PHP 8.0 or 8.1.

    the directory using PHP 7.2 shows no Xdebug settings enabled.

    • Make sure that you edit the right php.ini file — the xdebug_info() and phpinfo() sections should show which files it used.
    • Did you restart your PHP 7.2 PHP-FPM as well? The systemctl restart line for Apache does not automatically do that.

    sudo chown $USER:$USER /tmp/xdebug

    This is unlikely to be correct, as it needs to be the Apache user (often www-data) that can write to that directory, and not your shell user (which is likely just your log-in username — you can check that with echo $USER).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search