Home Backend Development PHP7 How to add Xdebug to macOS PHP7

How to add Xdebug to macOS PHP7

Dec 21, 2021 pm 04:53 PM
macos php7

MacOS system PHP7 adds Xdebug

After Apple released macOS High Sierra, the system finally came with php v7.1. Compared with before, if you want to use For php7, you have to find an additional way (Homebrew or php-osx), which is really convenient.

However, the PHP that comes with the system only has basic configurations. If you want to develop PHP, Xdebug is still necessary. Here is a summary of how to add the Xdebug module to the PHP that comes with the system in macOS High Sierra. [Recommended: PHP7 tutorial]

Basic environment (macOS and PHP information)

    ##macOS High Sierra: v10.13.3
  • PHP: v7.1.7

Installing Xdebug

Xdebug official website installation documentation has MAC recommended methods, since the system comes with PHP It is

v7.1.7, so when selecting, you need to select the installation package php71-xdebug.

How to add Xdebug to macOS PHP7

In addition, since

php71-xdebug in brew depends on php71, it is recommended to add --without -homebrew-phpThis parameter, brew will ignore the installation of php71.

brew install php71-xdebug --without-homebrew-php
Copy after login
Copy after login
But at this time, you may encounter the following error:

phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
Copy after login
prompts that dependencies are missing, causing

phpize to not work properly, phpize is used to prepare the compilation environment of the PHP extension library. In theory, the PHP that comes with the system should have phpize, but it is not in /usr/include/php/* The module it needs is found inside, and when searching /usr/include, it is found that this directory does not exist at all.

Googling around, to solve the problem, you need to complete the relevant content in

/usr/include. In systems before OSX v10.10, you need to manually create a soft link to solve the problem:

sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include /usr/include
Copy after login
But the system after v10.11 has rewritten the security policy, so you will encounter permission problems (

sudo will not work):

ln: /usr/include: Operation not permitted
Copy after login
But fortunately, Apple has Developers have prepared Xcode, which is a very powerful tool, but it is also very large (downloading and installation is a bit slow), and generally we only need the

Command Line Tools it provides. The above problems , in fact, it can be solved as long as you install Command Line Tools:

xcode-select --install
Copy after login
Next, follow the prompts, install and agree to the agreement...


How to add Xdebug to macOS PHP7

After the installation is completed, use

brew to install php71-xdebug:

brew install php71-xdebug --without-homebrew-php
Copy after login
Copy after login
After everything is completed, brew will give a prompt:

To finish installing xdebug for PHP 7.1:
  * /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini was created,
    do not forget to remove it upon extension removal.
  * Validate installation via one of the following methods:
  *
  * Using PHP from a webserver:
  * - Restart your webserver.
  * - Write a PHP page that calls "phpinfo();"
  * - Load it in a browser and look for the info on the xdebug module.
  * - If you see it, you have been successful!
  *
  * Using PHP from the command line:
  * - Run `php -i "(command-line 'phpinfo()')"`
  * - Look for the info on the xdebug module.
  * - If you see it, you have been successful!
Copy after login

Enable Xdebug for PHP

After the above steps, there is Xdebug in the system, but it may not be in the

php.ini configuration file, so Xdebug needs to be added manually Configuration items:

[xdebug]
zend_extension="/usr/local/opt/php71-xdebug/xdebug.so"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.scream = 0
xdebug.show_local_vars = 1
Copy after login
Then restart

php-fpm:

# 关闭php-fpm
sudo killall php-fpm

# 启动php-fpm
sudo php-fpm
Copy after login
Run

php -i "(command-line 'phpinfo()')" | grep xdebug, you can see the configuration content of Xdebug:

xdebug
...
xdebug.remote_autostart => On => On
xdebug.remote_connect_back => On => On
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => On => On
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.remote_timeout => 200 => 200
xdebug.scream => Off => Off
...
Copy after login

Visual Studio Code - PHP Debug

VSCode is currently the most popular One of the development tools, although lightweight, it is not inferior to all kinds of IDEs. It is Microsoft's conscience work. Its capabilities can be expanded by installing different plug-ins. Among them is the

PHP Debug plug-in. It can be used as a bridge to Xdebug to facilitate debugging PHP directly through Xdebug. The official description is very appropriate:

PHP Debug Adapter for Visual Studio Code
The guidance on the official website is also quite good:

  1. Install XDebug


    I highly recommend you make a simple test.php file, put a phpinfo(); statement in there, then copy the output and paste it into the XDebug installation wizard. It will analyze it and give you tailored installation instructions for your environment.In short:

      On Windows: Download the appropiate precompiled DLL for your PHP version, architecture (64/32 Bit), thread safety (TS/NTS) and Visual Studio compiler version and place it in your PHP extension folder.
    • On Linux: Either download the source code as a tarball or clone it with git, then compile it.
    ##Configure PHP to use XDebug by adding
  2. zend_extension=path/ to/xdebug
  3. to your php.ini.The path of your php.ini is shown in your phpinfo()
    output under "Loaded Configuration File".
  4. Enable remote debugging in your php.ini:
  5. [XDebug]
    xdebug.remote_enable = 1
    xdebug.remote_autostart = 1
    Copy after login

    There are other ways to tell XDebug to connect to a remote debugger than remote_autostart, like cookies, query parameters or browser extensions. I recommend remote_autostart because it "just works". There are also a variety of other options, like the port (by default 9000), please see the XDebug documentation on remote debugging for more information.

  6. If you are doing web development, don't forget to restart your webserver to reload the settings
  7. Verify your installation by checking your phpinfo() output for an XDebug section.

这里需要注意的是它推荐开启Xdebug配置项中的remote_autostart这一项。

好了,经过上面的操作,你应该可以跟Demo里面一样在VSCode中调试PHP了。
How to add Xdebug to macOS PHP7

The above is the detailed content of How to add Xdebug to macOS PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open a terminal for macos How to open a terminal for macos Apr 12, 2025 pm 05:30 PM

The following five methods can be used to open a macOS terminal: Use Spotlight Search through application folders Use Launchpad to use shortcut keys Command Shift U through terminal menus

How to view the system name of macos How to view the system name of macos Apr 12, 2025 pm 05:24 PM

How to view system name in macOS: 1. Click the Apple menu; 2. Select "About Native"; 3. The "Device Name" field displayed in the "Overview" tab is the system name. System name usage: identify Mac, network settings, command line, backup. To change the system name: 1. Access About Native Machine; 2. Click the "Name" field; 3. Enter a new name; 4. Click "Save".

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

How to record macos screen How to record macos screen Apr 12, 2025 pm 05:33 PM

macOS has a built-in "Screen Recording" application that can be used to record screen videos. Steps: 1. Start the application; 2. Select the recording range (the entire screen or a specific application); 3. Enable/disable the microphone; 4. Click the "Record" button; 5. Click the "Stop" button to complete. Save the recording file in .mov format in the "Movies" folder.

How to open macos terminal How to open macos terminal Apr 12, 2025 pm 05:39 PM

Open a file in a macOS terminal: Open the terminal to navigate to the file directory: cd ~/Desktop Use open command: open test.txtOther options: Use the -a option to specify that a specific application uses the -R option to display files only in Finder

Who invented the mac system Who invented the mac system Apr 12, 2025 pm 05:12 PM

The macOS operating system was invented by Apple. Its predecessor, System Software, was launched in 1984. After many iterations, it was updated to Mac OS X in 2001 and changed its name to macOS in 2012.

How to install fonts for macos How to install fonts for macos Apr 12, 2025 pm 05:21 PM

Steps to install fonts in macOS: Download the font file from a reliable source. Use the font preview program or terminal to install it into the system font folder (the sudo command is required to share it by users). Verify the installation in Font Book. Select the installed font to use in the application.

How to restart the apache server How to restart the apache server Apr 13, 2025 pm 01:12 PM

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

See all articles