如何在后台运行PHP's内置web服务器?


How do I run PHP's built-in web server in the background?

我编写了一个在持续集成环境中执行的PHP CLI脚本。它做的一件事是运行量角器测试。

我的计划是让内置PHP 5.4的内置web服务器在后台运行:

php -S localhost:9000 -t foo/ bar.php &

然后运行量角器测试,将使用localhost:9000:

protractor ./test/protractor.config.js

然而,PHP的内置web服务器并不作为后台服务运行。我似乎找不到任何可以让我在PHP中做到这一点的东西。

这能做到吗?如果有,怎么做?如果这绝对不可能,我愿意考虑其他解决方案。

你可以像在后台运行任何应用程序那样做。

nohup php -S localhost:9000 -t foo/ bar.php > phpd.log 2>&1 &

这里,nohup用于防止终端被锁定。然后需要重定向标准输出(>)和标准输出(2>)。

还有停止内置php服务器在后台运行的方法。当您需要在CI的某个阶段运行测试时,这很有用:

# Run in background as Devon advised
nohup php -S localhost:9000 -t foo/ bar.php > phpd.log 2>&1 &
# Get last background process PID
PHP_SERVER_PID=$!
# running tests and everything...
protractor ./test/protractor.config.js
# Send SIGQUIT to php built-in server running in background to stop it
kill -3 $PHP_SERVER_PID

您可以使用&>将标准错误和标准输出重定向到/dev/null (noWhere)。

nohup php -S 0.0.0.0:9000 -t foo/bar.php &> /dev/null &