Unable to run Xdebug in PhpStorm, connections are refused - likely due to port 9000 not being open or not being forwarded - phpstorm

I'm trying to debug my PHP code running on a remote server using PhpStorm's Xdebug feature. A few weeks ago I was able to do this on another computer, but I no longer have that computer. I reinstalled and configured PhpStorm and can run SFTP and SSH with the remote server on the new computer. I tried using PuTTY, and was successfully able to use it to SSH Tunnel between the two computers and run Xdebug. But it wasn't a 'good' as the way I was able to do this on the other computer, which didn't need PuTTY.
I believe that the problem has to do with setting up Port-9000 forwarding. I added a rule for this to my BitDefender BOX2 for the local computer I develop on, but I still get refused. The tech at BitDefender thought that there may be another port that needs to be opened/forwarded in addition to port 9000.
Because the PuTTY method works on the new computer, I'm confused. Why does this work with PuTTY, but not directly with PhpStorm (without the help of PuTTY)?

Xdebug only needs port 9000, so that is the only port that PhpStorm will listen on, and Xdebug needs to connect to. I don't know BitDefender, but perhaps you only allowed outgoing connections, and not the incoming ones that you should allow?
Are the two machines on the same network, or is your machine behind a NAT network to the outside world, where your remote machine lives? In that case, you probably can't get around using your SSH tunnel with PuTTY.
You don't mention any settings, but it is worthwhile to check what shows up in the xdebug log file (when configured with xdebug.remote_log=/tmp/xdebug.log on your remote machine). It will show what Xdebug tries to connect to, and whether (and sometimes even why) the connection failed.

Related

Minikube with PhpStorm and Xdebug

I have developed a PHP based application. This application runs actually in Kubernetes and mainly using minikube on my machine. I am using PhpStorm as IDE and I also use Xdebug for debugging purposes.
What I know from my researches are that when you start PhpStorm with Xdebug, it will start listening on a port (9000 by default). When I connect to my container (in minikube), I am able to reach the IDE at the port 9000 with netcat :
nc <my_ip_adress> 9000
This shows me a message telling that connection is open so I am able to reach the IDE from my container.
Then, when I try to use Xdebug, it is not working and Xdebug doesn't stop at the breakpoint. I was guessing that IDE should also reach the container and that part I am not sure and I don't know how to do it..
Anyone already setup this kind of configuration with minikube and PhpStorm / Xdebug?
For xdebug to work, it only needs to connect to client host. there is no need for client (in this case phpstorm) to connect to your pods as well.
I have the same setup using docker for mac. What i did to make it work:
changed xdebug.client_host configuration to host.docker.internal which is defined automatically in minikube /etc/host and can access host machine resources
made sure I have proper xdebug key defined in php.ini xdebug.idekey
made sure I use the xdebug helper extension and have the same idekey defined there
made sure I use 9003 to listen in phpstorm which is the default port for xdebug 3
If you try to debug a script with xdebug_info() in it, it will tell you exactly what Xdebug tried to do, if anything at all.
You can also make a log file by setting xdebug.log=/tmp/xdebug.log and xdebug.log_level=10, and then this log file will show even greater detail as to what went on.
Please note that although a connection can be open, you might not have the right process. Port 9000 is also used by PHP-FPM, which is why Xdebug 3 now uses 9003 by default.

phpstorm debugging mamp but not my remote server

I have tried many ways to debug my remote server but I am unable to do so. My ftp and sftp and remote db is configures to my phpstorm 9 but I cannot debug my remote server it is connecting to my mamp server and debugging ,y local files but not connecting to server username and password . Basically it fails at mysql_connect but works for mamp.How can I make it deubug with server.Everything else is synced with server but I cannot debug. I really appreciate any help.
Edit: Should I install x-debug on my server(cpanel) also ?
php.ini
[xdebug]
zend_extension="/usr/local/opt/php55-xdebug/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_connect_back=1
Maybe I didn't understand your question/problem. These are the ways you can debug your code
Debug the local code that uses the local database
This is the easiest setup and it probably already works on your system. You have all the files on the local computer and also you have an instance of MySQL running on it. The code connects to localhost:3306, the xdebug extension is installed and it can connect to PhpStorm, everybody is happy.
Debug the local code that uses the remote database
You can have all the PHP files on localhost and use the local mamp stack to debug it; you control the environment, xdebug works and happily collaborates with PhpStorm. You want the code to be able to use the remote (live) database.
In this case you need a way to access the database. Either you create a MySQL user that allows you to connect from the IP address of the local computer (a firewall along the way might prevent this), or start a ssh session that creates a tunnel from the local port 3306 (or any other open port you choose) to port 3306 of the database server (assuming the host where you ssh is allowed to connect to it). You can do this by running
ssh user#remote_host -L 3306:database_host:3306
(replace user, remote_host and database_host with your actual values)
If you have a MySQL server installed and running on localhost then the local port 3306 is not open and ssh cannot use it as the source port of the tunnel. Use another port instead (let's say you use 13306):
ssh user#remote_host -L 13306:database_host:3306
Modify the local configuration files of your application to use localhost as database server and 13306 as database port.
Debug the remote code
If you want to debug the live code (it uses the live database) then you have to upload the code on the web server (the live environment) and make it work there (be able to connect to the database etc).
In order to be able to debug it you need to have the xdebug PHP extension installed on the server and properly configured in the server's php.ini configuration file.
The debugger (the remote xdebug extension) needs to connect to your local computer where PhpStorm is listening on port 9000. This is either impossible or making it happen requires changing configuration here and there in several places (that might be out of your control); we better forget about it.
We can use the ssh tunnel trick: start a ssh connection to the server that creates a tunnel from local port 9000 to the servers port 9000:
ssh user#remote_host -L 9000:localhost:9000
Test if it works
PhpStorm provides a tool that uploads a script on the web server then tries to access it to check if the xdebug extension is properly configured. Depending on the version of PhpStorm you use, you can find it either in the menu (Run -> Web Server Debug Validation, on PhpStorm 9) or somewhere in the Settings (PHP -> Servers or around, on older versions).

How to Create SSH tunnel to do port forwarding with putty to a remote MySQL-db w/o server setup?

I want to SSH connect to a REMOTE MySQL db by pointing to, say, the LOCAL 3306 port; so far, I was able to do so by:
Installing/setting up FreeSSHd in the remote server (Windows 7).
Create an SSH tunnel and do port forwarding using putty.exe in the local
machine (Windows 8.1)
I followed the instructions of these two articles here (in spanish, sorry :/ ):
FreeSSHd on the server
SSH tunneling
I've heard somewhere that if the remote server is running on Linux the step 1 is not always required. Do you guys know if there's a way/weird-trick to skip step 1 (setting up anything on the remote server, rather than installing MySQL Server)?
Oh I see why this is not possible: It looks like some Linux distros come with a program similar to FreeSSHd by default, and there for, you can skip installing other tools on the server though now I'm sure the configuration will be always required anyways.

Cannot connect to remote mysql from Mac lion

I'm a mac novice and trying to get a developer setup to develop php sites locally. The sites he develops have a mysql backend and that is on a remote box. We got xampp installed and working, he can connect from the website to the local mysql box but he cannot connect to the remote box. He is using the exact same connection info I am using from my windows system. I do not have any issues. Also the mysql server is setup to accept all users (we are currently using root) regardless of hostname.
I searched his system and I could not find a mysql.sock file. I do not know what this does, but googled some articles that mentioned.
Please help if you can.
Try Telnetting to the server from your host and see if you get a response.
telnet host 3306
If that doesn't work, you probably have some local firewall on your mac that is blocking the connection. Also, one thing you don't mention is that you can connect from a windows computer and not a mac... are they on the same network? If they're on different networks or in different places, that could indicate a local network issue.
This is definitely not an issue with mysql, but with something on the Mac itself.
The mysql.sock file is relevant only to the machine the MySQL server is running on, for local clients to communicate with the server. For any remote machines, they'll be connecting via TCP on port 3306.
Make sure that port 3306 is open in the server machine's firewall, and that MySQL has been configured to allow TCP connections.

Connecting/Tunneling to remote server to bypass firewall

I want to try out some of the MySQL software, like Workbench, on the MySQL Db I develop on at work. After many failed attempts to make the connection, I finally asked one of the server admins if I was doing something wrong and was informed that the Db is behind firewall. So I can use phpMyAdmin, since it's installed server-side, but not Excel, Workbench, etc (from my machine).
So I would like to know if there is a fairly standard way to make a VPN-like connection to the server. Currently I use an SSH client to connect with no problem. But obviously that's not linking my local apps to the server. So can I make the connection in such a way that my whole system (so to speak) is considered signed on to the server? VPN is the closest analogy I can make, but that's not an option.
And....
Is that considered fairly "black hat" or is just something I don't know how to do but all the cool kids are doing it legitimately?
Thanks
This is simple using SSH tunneling. Simply do something akin to the following:
ssh -f username#your.remote.host -L 4040:your.remote.host:3306 -N
This does the following:
-f - forks SSH into background
username#your.remote.host - the user & host for SSH to connect to
-L 4040:your.remote.host:3306 - Listen for local connections on port 4040, and forward them via SSH to your.remote.host port 3306
-N - tells SSH not to issue a command on the remote host
You would then be able to connect to your mysql server (assuming the above ports are correct) using:
mysql --host=localhost --port=4040 --user=mysqluser -p
SSH tunnelling is excellent and can make life a lot easier.
The advantages are that it is all running over an encrypted port, 22, so the security is better and you can also compress the session, so over a slow network might see a bit of a performance improvement...
If you are using Windows, I would recommend puTTY which is available easily if you google it... Once connected, you can assign a local port which forwards to a port on the remote machine. In puTTY, this is in the Connection->SSH->Tunnels dialog.
I often use this for forwarding VNC - so if you have localport 5900 forwarding to the remote address 5900, you can connect to localhost:5900 as if you were connecting to the remote IP address.
It is also useful if there is a "hop" to a remote network - e.g. you aren't limited to forwarding to the ssh server you are connected to, you can also connect to other servers via the ssh server you are using.
Finally, I don't think that there is anything illegitimate about this option - you are using the ssh connection as intended and have been granted access to the server you are using. If anything, it is increased security...
Admins where I am have an Open-VPN that connect their personnal computer at home to servers at work, but it is used only for maintenance and 'emergency'.
I don't think it is good for security to have "holes" in the firewall, especially to a private place, where there is no firewall to protect your personnal computer.
These kind of practise is possible but has to be retricted to minimum