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

3

16

I've written a PHP CLI script that executes on a Continuous Integration environment. One of the things it does is it runs Protractor tests.

My plan was to get the built-in PHP 5.4's built-in web server to run in the background:

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

And then run protractor tests that will use localhost:9000:

protractor ./test/protractor.config.js

However, PHP's built-in web server doesn't run as a background service. I can't seem to find anything that will allow me to do this with PHP.

Can this be done? If so, how? If this is absolutely impossible, I'm open to alternative solutions.

Rhinestone answered 15/5, 2015 at 19:27 Comment(1)
Are you saying you want to daemonize the php process? If you're on a nix system you could look into init.d scripting. I haven't used php5.4's web server but if it runs a process it can be put in the background.Jordanson
O
29

You could do it the same way you would run any application in the background.

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

Here, nohup is used to prevent the locking of your terminal. You then need to redirect stdout (>) and stderr (2>).

Ogdon answered 15/5, 2015 at 19:31 Comment(0)
I
18

Also here's the way to stop built-in php server running in the background. This is useful when you need to run the tests at some stage of 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
Interdictory answered 24/9, 2017 at 17:2 Comment(1)
Great that was what im am looking for. Is there a similar sulotion for windows too ? Maybe anything that I can add in the router-script of the internal server to listen for a shutdown command ?Palpitate
R
1

You can use &> to redirect both stderr and stdout to /dev/null (noWhere).

nohup php -S 0.0.0.0:9000 -t foo/bar.php &> /dev/null &
Rainwater answered 11/7, 2019 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.