Automatically open website served with PHP built-in webserver - php

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"

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)"

In the sake of helping someone passing here:
chromium-browser-app=http://127.0.0.1:8000 | php -S 127.0.0.1:8000

Related

How can I load directory after run local server in terminal?

when I write cd project in my terminal then I get this line:
MacBook-Pro:project work$
I run my local server like this
MacBook-Pro:project work$ php -S 127.0.0.1:8000 -t public
After that I see this:
PHP 7.2.6 Development Server started at Tue May 29 10:45:40 2018
Listening on http://127.0.0.1:8000
Document root is /Users/work/project/public
Press Ctrl-C to quit.
But then I want to go back to my project folder MacBook-Pro:project work$ but when I write cd oder cd project nothing happens. Only when I press Ctr-C then this line MacBook-Pro:project work$appears again. Do I really have to quit my server to go into my project folder?
You should be able to run
php -S 127.0.0.1:8000 -t public &
in your terminal. & will set the task to be run in the background. Any output the command produces will be output to your terminal though.
To stop the command (Ctrl-C in this case) you first need to fg in your terminal to get it to the foreground, then Ctrl-C to quit.
If you're running the server in the foreground, then it is responsible for handling all input. You don't have direct access to the shell.
If you don't want that, then open another terminal, or run the server in the background, or use a multiplexer like screen or tmux.

run the PHP buit-in web server from file cmd

I often use the PHP built-in server from CMD (windows) with: php -S 127.0.0.2:8000
But, I want to create a CMD File, which I just double click to run the server.
I tried with : exec, chell_exec, system but it doesn't work.
What you need is a batch programm.
Take a look on these sources:
[1]
[2]
How to do it:
create a file e.g. yourbatch.bat
add following line: php -S 127.0.0.2:8000 or start php -S 127.0.0.2:8000
save
double click your file

Start as background process PHP built in server on Windows

I am using PHP built in server for testing and I was wondering is there a way you can hide cmd window when launching built in server using command php -S 127.0.0.1:809 -t Folder
I am currently working on Windows 10 so I need a Win solution.
not hundred percent sure on this, but you might try this one from:
What is cmd's equivalent to Bash's & (ampersand) for running a command without waiting for it to terminate?
so yours could be something like:
start /B php -S 127.0.0.1:809 -t Folder
You can create vbs script (run.vbs) and you can put this code in it
Dim oShell
Set oShell = WScript.CreateObject ("WSCript.shell")
oShell.run "cmd /C CD resource\php && php -S 127.0.0.1:809 -t HTML", 0
Set oShell = Nothing
0 in line signal for not displaying command line window.

How to shutdown default PHP server "php -S"

I can start the default PHP server with
php -S localhost:8000 but how to stop the server? Usually with CTRL + C yes, but if i want to do it from another terminal?
starting:
nohup php -S localhost:8000 &
pid=$!
echo $pid >/var/run/php.pid
stopping
pid=$(cat /var/run/php.pid)
kill $pid
That's only an example without proper error handling
Find the process ID, and issue a kill command. On Ubuntu linux, you can use "top" command line utility to see and stop running processes.
Once you start top, use "L" to search for "php", and you'll se it's process id
Open terminal, run htop. Press / to search and type in php -S or localhost or whatever command you've entered, F3 to search for next references if needed.
Press F9 to kill the process.

How to run PHP's build in server in the background

I upgraded to PHP 5.5, and I can't run the server in the background anymore.
ablock#desktop:~/site$ php -S localhost:3000 -t public/ &
[1] 9689
ablock#desktop:~/site$
[1]+ Stopped php -S localhost:3000 -t public/
ablock#desktop:~/site$
As you can see, the server stops right away.
When a process is set to run in the background (using the & operator) it can no longer write to the terminal. A SIGTTOU signal is generated and it's default action is to terminate the process since it no longer is able to write to stdout.
By redirecting stdout somewhere writable we can make sure that there will be no SIGTTOU signal and thous no termination of the process.
php -S localhost:3000 -t public/ 1>/dev/null &
1> means stdout, while 2> means stderr, used for errors. Both can be redirected to a file or a pseudo-devices using &>.

Categories