Php debug on centos

PHP Debugging with PhpStorm and Xdebug

Here’s how to go about debugging, stepping through, and profiling remote code like a breeze. These are the steps I took to install/enable Xdebug on a remote LAMP stack and debug/profile hosted code using PhpStorm and a Chrome extension. As a bonus I’ll share how I debug cURL requests with Xdebug too.

1. Setup remote hosting with SSH access

I’m using a VMWare development machine with a CentOS LAMP stack to mirror a production RHEL environment. You can use a shared host with SSH access, an AWS EC2 instance, or XAMP/WAMP/MAMP if you like. As long as you can SSH into the server and install Xdebug then you can try this out and speed up your debugging. No more echo «AAAA» statements.

2. Install or compile Xdebug

Xdebug may already be installed and running. You can check first like so.

If it is, then you can move on to the next step. Otherwise, let’s install or compile Xdebug with the official install docs from xdebug.org.

3. Configure Xdebug

First, find out which php.ini file is being parsed. Here is an example from CentOS:

Here are a couple choices of ini files to edit:

  1. You can edit the master php.ini in /etc/php5/apache2/php.ini
  2. You can edit xdebug.ini in /etc/php5/apache2/conf.d/xdebug.ini

If you see a path that has “cli” in it, be aware that may be the command line interface php.ini which will be different than the one Apache PHP uses (the default is mod_php).

I prefer to specify the path to php.ini in one of my Apache config files. For example in httpd.conf I have:

Whichever file you choose to hold your configuration settings, here are the settings I used to enable robust debugging:

Be sure to set xdebug.remote_autostart = Off to prevent your server from crawling on every page request. If it is On that means Xdebug will always try to start a remote debugging session which is probably not what you want.

Читайте также:  Сплит как работает питон

If you are interested in fine-tuning your Xdebug, here are all the settings you can adjust. If you are for now mainly interested in halting code at breakpoints in PhpStorm, or profiling code, then let’s continue.

I’ve found that if you are using WinCacheGrind on Windows, the profiler output name should start with “cachegrind” instead of “callgrind” because WinCacheGrind only recognizes one type of file.

You may want to ensure the write permissions are set to allow Xdebug to write to “/tmp/xdebug/”. This shouldn’t be a problem out of the box. It’s just a gotcha if you find that no profiling logs are being written.

If you are wondering why xdebug.remote_host is commented out above, here is an interesting point from the official docs that says why you can safely omit it:

xdebug.remote_connect_back
Type: boolean, Default value: 0, Introduced in Xdebug > 2.1
If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect to the client that made the HTTP request.

Restart Apache with apachectl restart or systemctl restart httpd.service (CentOS 7), or /etc/init.d/apache2 restart on Debian.

4. Setting the Debugger Configuration in PhpStorm

Set PhpStorm to listen to Xdebug on port 9000

JetBrains, the maker of PhpStorm, has detailed instructions on configuring Xdebug in their IDE. There are a few places settings need to be set, so I defer to their help pages. Essentially, you want to set the Xdebug IDE port to 9000 and the IDE key to PHPSTORM .

5. Triggering Xdebug

On a development box it’s sufficient for the server to connect back to port 9000 on the IDE over HTTP. To connect over an SSH tunnel, here is a guide from the makers of PhpStorm: Remote debugging in PhpStorm via SSH.

First, you must tell PhpStorm to listen for Xdebug connections by clicking the “phone” icon like so:

Enable Xdebug listening in PhpStorm

To trigger debugging, it’s necessary to send a special cookie along with each page request you wish to debug: XDEBUG_SESSION=PHPSTORM

To simplify this, Chrome has an extension called Xdebug helper that let’s you toggle setting this cookie or not.

Xdebug helper for Chrome

With the above settings and the Chrome helper, you can now step through PHP code at breakpoints set in the IDE.

Sample Xdebug breakpoint session

The above is great for debugging simple scripts, but what about scripts called with cURL?

6. Bonus: Xdebugging cURL Requests

If you’re using the settings from step 1, then you have all the tools to Xdebug PHP requests from cURL or wget. It’s actually pretty simple to Xdebug PHP files called from cURL. I’ll present a method for the CLI and from PHP curl.

Читайте также:  Json to html github

The method is to set the XDEBUG_SESSION cookie, and to send the connect-back IP in a custom header. With the settings from step 1, Xdebug will examine $_SERVER[‘HTTP_X_XDEBUG_REMOTE_ADDRESS’] for the IP to connect back to. In order to set that from a custom header you must set the header as

as you must not add the ‘HTTP-‘ part in front because it will be added automatically.

Источник

Install xdebug in centos7 with php7

I just realised I dont have a post on how to install xdebug in centos7 with php7 and setting up xdebug and eclipse. Its a feature that I use every day, and I had assumed I already had a post, but when searching my site ) only found instructions for XAMPP and xdebug.
I’m assuming you already have apache / php installed and working, and that you only need to add xdebug. This post shows how to install xdebug in centos7 with php7.
If you dont alreay have php7 installed in your centos7, you can follow my other post here. Once you have php7 installed, you can then install xdebug with the following:

Install xdebug in centos7 with php7

if you have php7 in centos7 with webtactic and epel you can install xdebug with the following

 yum install php70w-pecl-xdebug.x86_64 

if you have epel and webtactic, but not php7 you can install xdebug with:

Now that xdebug is in your sytstem its time to confgure xdebug

 #this line will be added automatically zend_extension = /usr/local/lib/php/extensions/xxx/xdebug.so #add the following xdebug.remote_log="/tmp/xdebug.log" xdebug.profiler_enable = 1 xdebug.remote_enable=on xdebug.remote_port=9000 xdebug.remote_autostart=0 xdebug.remote_connect_back=on xdebug.idekey=editor-xdebug 

You can find the correct paths to the files needed if you have yum-utils installed and run

 repoquery --list php70w-pecl-xdebug # the output is: /etc/php-zts.d/xdebug.ini /etc/php.d/xdebug.ini /usr/bin/debugclient /usr/lib64/php-zts/modules/xdebug.so /usr/lib64/php/modules/xdebug.so /usr/share/doc/php70w-pecl-xdebug-2.4.1 /usr/share/doc/php70w-pecl-xdebug-2.4.1/CREDITS /usr/share/doc/php70w-pecl-xdebug-2.4.1/LICENSE /usr/share/doc/php70w-pecl-xdebug-2.4.1/README.rst /var/lib/pear/pkgxml/xdebug.xml 

You now need to restart httpd

 service httpd restart # or systemctl restart httpd.service 

Xdebug will now be up and running

If you are running with SELinux enforement on, then you can run this command to let xdebug out:

setsebool -P httpd_can_network_connect 1

Eclipse

Inside eclipse, goto preferences > php > debuggers
change the debugger to xdebug
click configure
change the dropdown for “accept remote session (JIT)” to “any” or “prompt”
click ok

Firefox

Xdebug Helper is the best solution I have found for firefox – simply install the extension from here, navigate to the site you want to debug, and click the little green bug, select debug and reload your page to open in your editors debugger. You might need to add the key you added to your xdebug.ini in the previous steps.

Читайте также:  Скрипты или функции php

Chrome

Xdebug Helper is the best solution I have found for chrome – simply install the extension from here, navigate to the site you want to debug, and click the little green bug, select debug and reload your page to open in your editors debugger. You might need to add the key you added to your xdebug.ini in the previous steps.

You Might Also Like

Read more about the article Making your site faster with Yslow and pagespeed

Making your site faster with Yslow and pagespeed

Read more about the article Twitter bootstrap code snippets

May 8, 2010

Twitter bootstrap code snippets

Read more about the article Identify and remove unused css across entire site

April 24, 2013

Identify and remove unused css across entire site

You must log in to post a comment.

Источник

joseluisq / xdebug_installation.md

If your have an Nginx server, we should restart the PHP processor php-fpm :

$ sudo systemctl restart php-fpm

Or if you have a Apache server, we should restart the httpd server:

$ sudo systemctl restart httpd

Finally, verify if xdebug extension is loaded:

php -r "echo (extension_loaded('xdebug') ? 'xdebug up and running!' : 'xdebug is not loaded!');" // xdebug up and running!

I have been trying to get xdebug working correctly in Eclipse Mars 2 (4.5.2) on Fedora 23. I got it working correctly after setting the xdebug.remote_enable option.

You don’t need to add the line to /etc/php.ini as the PHP loads it anyway.

@rahilwazir It’s true. Now is not necessary to add this line to php.ini. Updated!

I would like to know whether upgrading to a new version will mess up with my old apps? recently I start getting such errors as follows:

/etc/cron.daily/0yum.cron:
Error: Package: php-pecl-xdebug-2.1.4-2.el6.x86_64 (epel)
Requires: php-common >= 5.3.3-38
Installed: php-common-5.3.3-27.el6_5.1.x86_64 (@sl-security)
php-common = 5.3.3-27.el6_5.1
Available: php-common-5.3.3-26.el6.x86_64 (sl)
php-common = 5.3.3-26.el6
Available: php-common-5.3.3-27.el6_5.x86_64 (sl-security)
php-common = 5.3.3-27.el6_5
/etc/cron.daily/cleanscratch:

Cleaning /srv/scratch space
/etc/cron.daily/freshclam:

/etc/cron.daily/freshclam: line 15: /usr/bin/freshclam: No such file or directory

@Verakhs There are some deprecated functions. You should check it out before upgrading. Or. if it isn’t critical, just upgrade and watch for errors.

Ran into a minor issue with the last command to verify if xdebug is running on fedora 27. Apparently the double and single quotes are inverse. ran it successful with :

php -r ‘echo (extension_loaded(«xdebug») ? «xdebug up and running!» : «xdebug is not loaded!»);’

Источник

Оцените статью