【问题标题】:How do I run PHP's built-in web server in the background?如何在后台运行 PHP 的内置 Web 服务器?
【发布时间】:2015-07-27 20:09:46
【问题描述】:

我编写了一个在持续集成环境中执行的 PHP CLI 脚本。它所做的其中一件事是运行 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 做到这一点的东西。

这可以吗?如果是这样,怎么做? 如果这绝对不可能,我愿意接受其他解决方案。

【问题讨论】:

  • 你是说你想守护 php 进程吗?如果您使用的是 nix 系统,则可以查看 init.d 脚本。我没有用过php5.4的web服务器,但是如果它运行一个进程,它可以放在后台。

标签: php protractor daemon php-5.4


【解决方案1】:

您可以像在后台运行任何应用程序一样执行此操作。

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

这里,nohup 用于防止终端锁定。然后你需要重定向 stdout (>) 和 stderr (2>)。

【讨论】:

    【解决方案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
    

    【讨论】:

    • 这就是我想要的。窗户也有类似的情况吗?也许我可以在内部服务器的路由器脚本中添加任何东西来监听shutdown 命令?
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多