Debugging Remote PHP Codes on EC2 with PhpStorm and Xdebug
PHP codes running in remote EC2 instances can be debugged with PhpStorm and Xdebug.
Prerequisites
Software | Version |
---|---|
PHP | 7.1.7 |
Apache | 2.2.32-1.9 |
PhpStorm | 2017.1.4 |
Settings
Server
Add Xdebug settings to php.ini
and restart Apache by running sudo service httpd restart
.
xdebug.so
may vary depending on user’s environment. [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.
Field | Value |
---|---|
Name | EC2 private IP |
Host | EC2 public IP |
Debugger | Xdebug |
Use path mappings | ON |
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.