<!DOCTYPE html>
<html>
<body>
<h1>我的第一张 PHP 页面</h1>
<?php
echo "Hello World!";
$fd = fopen('php://stdout', 'w');
if ($fd) {
fwrite($fd, "7777888");
fwrite($fd, "\n");
fclose($fd);
}
?>
</body>
</html>
对以上代码,如果用php-cgi运行,在命令行(windows和linux)都会获得如下结果
7777888
而对于生产环境的php-fpm是关闭了stdout的,源码如下
int fpm_stdio_init_main() /* {{{ */
{
int fd = open("/dev/null", O_RDWR);
if (0 > fd) {
zlog(ZLOG_SYSERROR, "failed to init stdio: open(\"/dev/null\")");
return -1;
}
if (0 > dup2(fd, STDIN_FILENO) || 0 > dup2(fd, STDOUT_FILENO)) {
zlog(ZLOG_SYSERROR, "failed to init stdio: dup2()");
close(fd);
return -1;
}
close(fd);
return 0;
}
经研究,在linux下可让程序显示加载不同的动态库,从而可以在不修改php源码的基础上解决此问题,配上管道捕捉效果如下
Please slecet a registered process:
1:[2015-11-06 12:27:39.966]fsServer:366(127.0.0.1)
2:[2015-11-06 12:27:40.916]php-fpm:405(127.0.0.1)
3:[2015-11-06 12:27:41.817]sos:367(127.0.0.1)
use name or index:2
100316/1323588888(doRecvDataFunction_011)--Select is:"php-fpm:405(127.0.0.1)".
Start debug stream:"php-fpm:405(127.0.0.1)".
7777888
7777888
7777888
7777888
7777888
7777888