运行 script.php 时出错:端口 9000 忙。

20

我在phpstorm中设置了php.ini调试配置(Debug config)。尝试使用以下命令在phpstorm Event log中输出php脚本的调试信息:

"Error running script.php: Port 9000 is busy"

php.ini 结束:

        [XDebug]
        zend_extension="/usr/lib/php5/20090626+lfs/xdebug.so"
        xdebug.remote_enable=1
        xdebug.remote_port="9000" (the default port is 9000)
        xdebug.profiler_enable=1
        xdebug.profiler_enable_trigger = 1
        xdebug.profiler_output_dir="/etc/php5/xdebug/profiler_output_dir"

pStorm中的调试端口也设置为9000。使用 netstat -na 命令输出:

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN 

如果我设置到其他端口会怎样?例如将其设置为10001似乎可以完成任务。或者只是如何使其正常工作。我不确定我是否理解xDebug的工作原理。它就像在phpstorm中运行Debug("script.php")(Shift+F9)并在文件中设置断点一样吗?
有人有想法吗?
编辑:
来自:http://xdebug.org/docs/remote
xdebug.remote_port
Type: integer, Default value: 9000
The port to which Xdebug tries to connect on the remote host. Port 9000 is the default for both the client and the bundled debugclient. As many clients use this port number, it is best to leave this setting unchanged.

如果我将端口更改为9000以外的其他端口,比如超过50k的端口,会发生什么?

1
"端口9000被占用" -- 这意味着另一个应用程序(不是 PhpStorm...或者完全不同的 PhpStorm 实例)已经在该端口上监听。IDE 在该端口上侦听(接收连接),而 xdebug 是连接到 IDE 而不是相反的方式。官方的 xdebug 文档详细解释了 xdebug 如何与调试客户端(IDE)一起工作:http://xdebug.org/docs/remote。 - LazyOne
7个回答

16

正在运行在9000端口上的进程可能是PhpStorm本身。

像Jacob建议的那样检查当前使用的端口(在Windows命令提示符中运行:netstat -a -b):

enter image description here

如果PhpStorm.exe:9000下列出,您只需要在设置>构建、执行、部署>调试器中更改内置服务器的端口,例如改为63342(这是我团队成员PhpStorm安装的默认设置)。


在OSX上,您可以使用-lsof -n -i4TCP:9000 | grep LISTEN找到绑定到端口9000的应用程序。 - carpii

8

GNU/Linux解决方案(因为世界不止Windows):

根据@aqm的解决方案,首先找到使用9000端口的进程。

netstat -tulpn | grep :9000

您将会得到如下输出:
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      3993/programname

您需要使用PID命令来终止进程,具体命令如下:
kill -9 3993
注意: 可能您的进程未显示其PID,这可能是因为您正在运行使用默认端口9000的PHP-FPM,如果是这种情况,您可以更改PHP-FPM端口或PHP Xdebug端口。
解决方案1-更改Xdebug配置:
编辑/etc/php/php.ini 搜索xdebug.remote_port=9000并替换为新端口
解决方案2-更改PHP-FPM端口:
编辑/etc/php/php-fpm.conf 搜索带有listen = 127.0.0.1:9000的行,将9000替换为新端口
然后重新加载服务:
systemctl reload php-fpm

然后编辑您的nginx或apache配置文件,告诉它使用新端口。在/etc/httpd/httpd.conf/etc/nginx/nginx.conf中搜索127.0.0.1:9000,并将其替换为您的新端口。

重新加载您的服务器:

systemctl reload nginx

或者

systemctl reload httpd

希望能对您有所帮助 :), 你好。

6

无法通过 lsof 和 netstat 确定运行在 9000 端口的进程的 pid。临时最简单的解决方案就是在 phpstormphp.ini 中将端口更改为未被实际使用的其他端口(例如 10k)。


5

基本上是有其他进程正在使用9000端口并且没有释放,通常是因为我同时运行了另一个IDE。

Windows解决方案 ->

  1. 查找正在使用9000端口的进程

打开cmd.exe

netstat -o

C:\Users\AMC75>netstat -o

Active Connections

 Proto  Local Address          Foreign Address        State           PID
 TCP    10.6.176.132:27018     livlt0124661:55375     ESTABLISHED     11104
 TCP    10.6.176.132:49271     chilynfe01:5061        ESTABLISHED     10120
 TCP    10.6.176.132:49379     hhefs14:netbios-ssn    ESTABLISHED     4
 TCP    10.6.176.132:49383     chifs08:netbios-ssn    ESTABLISHED     4
 TCP    10.6.176.132:51034     157.55.56.143:40002    ESTABLISHED     11104
 TCP    10.6.176.132:51038     db3msgr6011007:https   ESTABLISHED     11104
 TCP    10.6.176.132:51049     wb-in-f125:5222        ESTABLISHED     10860
 TCP    10.6.176.132:51282     wpapp020:50124         ESTABLISHED     848
 TCP    10.6.176.132:53014     ec2-107-23-104-135:https  ESTABLISHED     10860
 TCP    10.6.176.132:53015     ec2-107-23-104-135:https  ESTABLISHED     10860
 TCP    10.6.176.132:54774     157.56.116.205:12350   ESTABLISHED     11104
 TCP    10.6.176.132:54822     a23-198-48-60:https    CLOSE_WAIT      7500
 TCP    10.6.176.132:55166     upmon070:3306          ESTABLISHED     2652
 TCP    10.6.176.132:55261     lhr08s03-in-f9:http    ESTABLISHED     10860
 TCP    10.6.176.132:55570     upmon070:3306          ESTABLISHED     10980
..... BLAH BLAH BLAH
  1. 终止进程

使用命令 taskkill /PID XXXX

  1. 利润...? 现在您应该能够运行调试器了

5

给未来的自己:

在我的情况下,这是由于使用虚拟机端口转发(Virtual Box上的Vagrant)引起的。我认为转发9000是一个好主意。

解决方法:连接到标准Virtual Box的VM IP 10.166.53.1 enter image description here

/etc/php5/cgi/conf.d/xdebug.ini 的内容。

; xdebug configuration
; xdebug.remote_host = localhost
zend_extension =  /usr/lib/php5/20121212/xdebug.so
xdebug.remote_connect_back = 1
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_handler = dbgp
xdebug.remote_mode = req
xdebug.idekey = "PHPSTORM"
xdebug.remote_log = /var/log/xdebug.log

1
我也遇到了同样的问题。显然,您不需要转发此端口。删除端口9000的转发对我有用。现在我可以使用Vagrant + XDebug + PhpStorm。 - Giel Berkers

2

首先,虽然与主题有些偏离,但我认为这可以帮助。我为每个docker-composer项目使用不同的网络接口。这样,我就可以在/etc/hosts中映射一个持久的域名而不用担心任何问题。例如,在这个例子中是192.168.33.102 project.domain.local

以下是yaml设置示例:

nginx:
  build: docker-images/nginx
  links:
    - "php"
  ports:
    - "192.168.33.102:80:80"
php:
  build: docker-images/php/7.0.1-fpm
  ports:
    - "192.168.33.102:10000:9000"

PHP容器监听9001端口用于php7.0-fpm,监听9000端口用于xdebug,不会暴露任何端口。

PhpStorm调试端口设置为默认的9000,并且服务器主机设置为192.168.33.102(或者来自/etc/hosts的值,即project.domain.local)。

如上所述,在PHP容器的端口中,可以将不同的映射端口设置为10000,它可以是与9000不同的任何其他值,以消除PhpStorm的忙碌错误。

此外,重要的是在docker-compose上绑定端口。如果没有映射端口,则无法正常工作,使用9000: 9000也无法正常工作。

希望对其他人有所帮助!

附:这是xdebug.ini:

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true

0

对我来说可行的方法是:

在Linux上使用以下命令查看已使用的端口:

sudo netstat -tulpn | grep :90 

结果

tcp        0      0 0.0.0.0:9000            0.0.0.0:*               OUÇA       22719/docker-proxy  
tcp6       0      0 :::9000                 :::*                    OUÇA       22726/docker-proxy  
tcp6       0      0 :::9003                 :::*                    OUÇA       5252/java

然后我在 Dockerfile 上进行了更改:

&& echo "xdebug.client_port=9001" >> /usr/local/etc/php/conf.d/xdebug.ini \

关于phpstorm设置:

设置 > PHP > 调试:

Xdebug部分:调试端口9001


注意:client_port 是 xdebug3 中 remote_port 的替代品,还有一些其他变量也发生了变化,例如默认端口从 9000 变为 9003,请参见 https://xdebug.org/docs/upgrade_guide。 - Sandra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接