5

How can I serve and open the website from the current directory in one command with php built-in webserver?

The command used for php built-in webserver is:

php [options] -S <addr>:<port> [-t docroot] 

However this is a running command, so the following command does not work:

php -S 127.0.0.1:8000 && open 127.0.0.1:8000 

Purpose is creating a single alias command to open the website in a browser directly after starting the webserver (all from a single command):

alias lserve="php -S 127.0.0.1:8000 && open 127.0.0.1:8000" 
4
  • Put it to the background? nohup ? Commented Jan 5, 2016 at 16:22
  • what is your desired action to open? accessing the server works when you e.g. run lynx 127.0.0.1:8000. as open just starts a program on a new vt which program is then called? Commented Jan 5, 2016 at 16:33
  • 1
    you can e.g. run (php -S 127.0.0.1:8000 &); lynx 127.0.0.1:8000 which starts the srv in the background via a subshell and then the lynx cmd is called connecting to the host just setup. Commented Jan 5, 2016 at 16:40
  • The desired action is to open the website in a browser directly after starting the webserver Commented Jan 5, 2016 at 21:00

2 Answers 2

2

Run the server in background:

php -S 127.0.0.1:8000 & open 127.0.0.1:8000 

Note that I'm using just a single & which starts a job in background. This is not related to the logical and operator &&. Bash's syntax does not allow the command that follows the & to be separated by a ;

However, there is still a problem with that solution. Since the server runs in background, you cannot close both the browser and the server with a single ^C. To achieve that you need to start both commands in a sub shell:

(trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000) 

Now you can put that into an alias:

alias lserve="(trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open http://127.0.0.1:8000)" 
Sign up to request clarification or add additional context in comments.

11 Comments

Now the question is, how to close the running php server in the same ^C
I've answered that already - use a subshell. Btw, what is that open command doing? Is it a shell script?
@user2307317 I've added the trap command. Using it, all processes get closed together once you press ^C. Check this: stackoverflow.com/questions/8363519/…
this does not work for me, as the php server keeps running in the background. As soon as you refresh the page, it restarts the process in the terminal.
let me state it simpler: Open activity monitor; search for php; nothing there. Paste (trap 'kill 0' SIGINT; php -S 127.0.0.1:8000 & open 127.0.0.1:8000); now there is something in activity monitor running under php; ^C in terminal; php still running in activity monitor. when the page which was opened due to the command is refreshed, the terminal reflects and reinstates the original command as never discontinued.
|
0

In the sake of helping someone passing here:

chromium-browser-app=http://127.0.0.1:8000 | php -S 127.0.0.1:8000

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.