Debugging Remote PHP Codes on EC2 with PhpStorm and Xdebug

Debugging Remote PHP Codes on EC2 with PhpStorm and Xdebug

Takahiro Iwasa
Takahiro Iwasa
2 min read
EC2 PHP PhpStorm

PHP codes running in remote EC2 instances can be debugged with PhpStorm and Xdebug.

Prerequisites

SoftwareVersion
PHP7.1.7
Apache2.2.32-1.9
PhpStorm2017.1.4

Settings

Server

Add Xdebug settings to php.ini and restart Apache by running sudo service httpd restart.

[xdebug]
zend_extension="/opt/remi/php71/root/usr/lib64/php/modules/xdebug.so"
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_host = "127.0.0.1"
xdebug.idekey = "IDE_KEY"
xdebug.remote_autostart=true

PhpStorm

Open Run > Edit Configurations..., then select PHP Remote Debug.

Click Servers, and add a target server running your WEB application based on the following configuration.

FieldValue
NameEC2 private IP
HostEC2 public IP
DebuggerXdebug
Use path mappingsON
Absolute path on the server/var/www/html/<YOUR_WEB_APP_ROOT>

Set the IDE key wrote in xdebug.idekey field in php.ini.

Router

If you use a network router, configure port mapping to allow incoming connections to port 9000 to be forwarded to your localhost.

Debugging

Put a breakpoint on any position and turn on Listen mode. After accessing the EC2 instance using WEB browsers, PhpStorm will stop at the breakpoint.

Conclusion

Although we must not directly debug codes running in production, it may be useful for development and staging environment.

I hope you will find this post useful.

Takahiro Iwasa

Takahiro Iwasa

Software Developer at KAKEHASHI Inc.
Involved in the requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.