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.
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
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
Mistake #2: I forgot to restart PHP 7.2 PHP-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)
2 - Create the directory to store profiling files & give Apache ownership
3 - Enable Xdebug via php.ini (you can use
phpinfo()
to determine your.ini
file location)Add the following at the bottom of the .ini file (see settings documentation for your needs)
4 - Restart PHP-FPM & Apache
PHP 7.2 is no longer supported, so please upgrade to at least PHP 7.4, but preferably PHP 8.0 or 8.1.
php.ini
file — thexdebug_info()
andphpinfo()
sections should show which files it used.systemctl restart
line for Apache does not automatically do that.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 withecho $USER
).