Cách kích hoạt mô-đun Xdebug có sẵn của ServBay
Là một công cụ phát triển web tích hợp mạnh mẽ, ServBay đi kèm với mô-đun Xdebug và quy trình kích hoạt của nó rất đơn giản. Xdebug là một công cụ debug và phân tích PHP, có thể giúp các nhà phát triển debug code, phân tích hiệu suất và kiểm tra độ phủ sóng mã, là công cụ đắc lực cho việc phát triển và duy trì ứng dụng PHP.
Giới thiệu mô-đun Xdebug
Xdebug là một phần mở rộng PHP mạnh mẽ, được sử dụng để debug và phân tích mã PHP. Nó cung cấp nhiều tính năng debug phong phú, bao gồm debug điểm dừng, theo dõi stack, giám sát biến, v.v. Ngoài ra, Xdebug còn hỗ trợ phân tích hiệu suất và kiểm tra độ phủ sóng mã, giúp các nhà phát triển tối ưu hóa hiệu suất mã và đảm bảo chất lượng mã.
Các đặc điểm chính
- Debug điểm dừng: Xdebug hỗ trợ đặt điểm dừng trong mã, debug từng dòng một, giúp dễ dàng tìm và sửa lỗi.
- Theo dõi stack: Khi xảy ra lỗi, Xdebug có thể tạo thông tin theo dõi stack chi tiết, giúp nhà phát triển nhanh chóng xác định vấn đề.
- Phân tích hiệu suất: Xdebug cung cấp chức năng phân tích hiệu suất, có thể tạo báo cáo hiệu suất chi tiết, giúp nhà phát triển tối ưu hóa hiệu suất mã.
- Kiểm tra độ phủ sóng mã: Xdebug hỗ trợ kiểm tra độ phủ sóng mã, giúp các nhà phát triển đảm bảo rằng kiểm thử đã bao phủ tất cả các đường mã.
- Hỗ trợ môi trường phát triển tích hợp: Xdebug có thể tích hợp với nhiều môi trường phát triển tích hợp (IDE) như PHPStorm, Visual Studio Code, v.v., cung cấp trải nghiệm debug tiện lợi.
Phiên bản mô-đun Xdebug có sẵn của ServBay
ServBay hỗ trợ nhiều phiên bản PHP, và mỗi phiên bản đều được cài sẵn mô-đun Xdebug tương ứng. Các phiên bản cụ thể như sau:
- PHP 5.6, 7.0, 7.1: Xdebug 2.5.5
- PHP 7.2, 7.3, 7.4: Xdebug 3.1.6
- PHP 8.0, 8.1, 8.2, 8.3: Xdebug 3.3.1
- PHP 8.4: Xdebug 3.4.0-dev
Lưu ý
Mỗi phiên bản PHP Xdebug có cổng độc lập, liên kết với số phiên bản, ví dụ cổng Xdebug của PHP 7.2
là 39072
, cổng Xdebug của PHP 8.3
là 39083
Cách kích hoạt mô-đun Xdebug
Theo mặc định, mô-đun Xdebug bị tắt. Các bước kích hoạt mô-đun Xdebug rất đơn giản, chỉ cần sửa đổi tệp cấu hình của phiên bản PHP tương ứng. Dưới đây là các bước chi tiết:
Bước một: Tìm tệp cấu hình
Trước tiên, định vị đến thư mục conf.d
của phiên bản PHP tương ứng. Ví dụ, để kích hoạt mô-đun Xdebug cho PHP 8.3, chúng ta cần chỉnh sửa tệp:
/Applications/ServBay/etc/php/8.3/conf.d/xdebug.ini
Bước hai: Chỉnh sửa tệp cấu hình
Mở tệp xdebug.ini
, bỏ ghi chú nội dung sau:
[Xdebug]
; Uncomment the following line to enable XDebug
zend_extension = xdebug.so
2
3
Bước ba: Cấu hình các tham số Xdebug
Để tận dụng triệt để các chức năng của Xdebug, bạn có thể cấu hình một số tham số thông thường trong xdebug.ini
. Ví dụ:
xdebug.mode=debug,develop
xdebug.start_with_request=yes
xdebug.client_host=localhost
xdebug.client_port=39083
xdebug.log=/Applications/ServBay/logs/xdebug/8.3/xdebug.log
2
3
4
5
Bước bốn: Khởi động lại dịch vụ PHP
Trong bảng điều khiển quản lý dịch vụ của ServBay, khởi động lại dịch vụ PHP tương ứng. Ví dụ, khởi động lại dịch vụ PHP 8.3. Sau khi khởi động lại, mô-đun Xdebug sẽ được tải thành công.
Xác minh mô-đun Xdebug đã được tải thành công hay không
Bạn có thể xác minh mô-đun Xdebug đã được tải thành công bằng cách tạo một tệp PHP đơn giản. Tạo một tệp phpinfo.php
trong thư mục gốc của máy chủ web với nội dung sau:
<?php
phpinfo();
?>
2
3
Truy cập https://servbay.host/phpinfo.php
, tìm thông tin liên quan đến mô-đun Xdebug trên trang thông tin PHP đầu ra. Nếu thấy thông tin liên quan đến Xdebug, điều đó có nghĩa là mô-đun đã được tải thành công.
Cấu hình Xdebug trong PHPStorm
Để sử dụng Xdebug trong PHPStorm để debug, bạn cần thực hiện các bước cấu hình sau:
Bước một: Mở PHPStorm và vào cài đặt
- Mở PHPStorm.
- Nhấp vào
File
trên thanh menu, sau đó chọnSettings
(Windows/Linux) hoặcPreferences
(macOS).
Bước hai: Cấu hình trình thông dịch PHP
- Trong cửa sổ cài đặt, điều hướng đến
Languages & Frameworks
->PHP
. - Trong phần
CLI Interpreter
, nhấp vào nút...
bên phải. - Trong cửa sổ bật lên, nhấp vào dấu
+
để thêm trình thông dịch PHP mới, chọnLocal
. - Đặt đường dẫn trình thông dịch PHP đến đường dẫn của phiên bản PHP tương ứng trong ServBay, ví dụ
/Applications/ServBay/package/php/8.3/8.3.7/bin/php
. - Nhấp vào
OK
để lưu cài đặt.
Bước ba: Cấu hình debug Xdebug
Lưu ý
Mỗi phiên bản PHP Xdebug có cổng độc lập, liên kết với số phiên bản, ví dụ cổng Xdebug của PHP 7.2
là 39072
, cổng Xdebug của PHP 8.3
là 39083
- Trong cửa sổ cài đặt, điều hướng đến
Languages & Frameworks
->PHP
->Debug
. - Trong phần
Xdebug
, đặtDebug Port
là39083
, phù hợp với cấu hìnhxdebug.client_port
. - Đảm bảo rằng tùy chọn
Can accept external connections
đã được chọn. - Nhấp
Apply
rồi nhấpOK
để lưu cài đặt.
Bước bốn: Cấu hình máy chủ ứng dụng PHP Web
- Trong cửa sổ cài đặt, điều hướng đến
Languages & Frameworks
->PHP
->Servers
. - Nhấp vào dấu
+
bên phải để thêm cấu hình máy chủ mới. - Đặt tên máy chủ, ví dụ
ServBay Localhost
. - Đặt
Host
là host tương ứng trong ServBay của bạn, ví dụservbay.new
, cổng là 443 (HTTPS). - Đặt
Debugger
làXdebug
. - Nhấp
OK
để lưu cấu hình.
Bước năm: Khởi động phiên debug
- Trong PHPStorm, mở dự án PHP mà bạn muốn debug.
- Đặt điểm dừng trong mã (nhấp vào khoảng trống bên trái số dòng).
- Nhấp vào nút
Start Listening for PHP Debug Connections
trên thanh công cụ phía trên của PHPStorm (biểu tượng con bọ nhỏ). - Truy cập ứng dụng PHP của bạn trong trình duyệt, Xdebug sẽ tự động kết nối tới PHPStorm và tạm dừng thực thi tại điểm dừng.
Kết luận
ServBay cung cấp một cách tiện lợi để quản lý và kích hoạt mô-đun Xdebug. Thông qua cấu hình đơn giản và các thao tác khởi động lại, các nhà phát triển có thể nhanh chóng kích hoạt mô-đun Xdebug trên các phiên bản PHP khác nhau, từ đó tận dụng tối đa tính năng debug và phân tích mạnh mẽ của nó, nâng cao hiệu quả phát triển và duy trì ứng dụng PHP. Các tính năng debug điểm dừng, phân tích hiệu suất và kiểm tra độ phủ sóng mã của Xdebug làm cho nó trở thành công cụ không thể thiếu đối với các nhà phát triển PHP.