运行 PHP 的内置网络服务器如何在后台运行?

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

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

我的计划是在后台获取内置 PHP 5.4's built-in web server 到 运行:

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 用于防止您的终端被锁定。然后你需要重定向 stdout (>) 和 stderr (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

您可以使用 &> 将 stderr 和 stdout 重定向到 /dev/null (noWhere)。

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