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

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.

Related

Linux mount script runs in PHP interpreter but not in web Apache

My website is about copying files. Instead of copy the files to avoid to much time and server lagging, I decided to use overlayfs. The code mount the folder to the location specified in the terminal using the PHP interpreter. But, when I run the php script from Apache the script does not mount the overlay. And the worst is that there is no error output so I can debug what's wrong. I checked the php error log, no output about what happened.
The destination i'm mounting the overlayfs is to another user. For that I need root to execute the mount command. To be able to run the code without using root nor sudo, I took a look at this question. I created the c code, compile it and set the proper permissions (root.root, rwsr,sr...). I ran the code in a php file:
<?php
// filename over.php
print shell_exec("whoami")."\n";
print shell_exec('/var/www/vhosts/user/deployment/exec "sudo mount -t overlay overlay -o lowerdir=/var/www/vhosts/user/deployment/template5_dev,upperdir=/var/www/vhosts/user.deve/httpdocs,workdir=/var/www/vhosts/user/deployment/overlay-work /var/www/vhosts/user.deve/httpdocs"');
What the code does is print the actual user name (to make sure I have an output, to see if the code executed) then merge the folders. The file /var/www/vhosts/user/deployment/exec is the c program then I pass the code to execute as argument.
In the terminal I run: php -f "/var/www/vhosts/user/httpdocs/over.php". I check the merged folder I can see it works. And the output is user.
Then I unmount the overlay sudo umount /var/www/vhosts/user.deve/httpdocs.
I access the php script via the browser, I got the output user but the folder does not merge. I ctrl-f5 multiple times but nothing, no error, no error in log nothing.
I changed the command to shell_exec('/var/www/vhosts/user/deployment/exec "sudo mkdir /var/www/vhosts/user.deve/httpdocs/nouvo"');, the sudo created the folder from the browser.
I noticed that only the mount command does not run properly.
What could be the reason the sudo mount command does not run by apache, and even if there was an error, doesn't it print out the error?
I just take a look at /var/log/kern.log. I can see the mount command got executed from the web-browser. But the log is different from the one the ones executed in the terminal.
From the web-browser:
kernel: [ 149.465459] overlayfs: filesystem on '/var/www/vhosts/user.deve/httpdocs' not supported as upperdir
kernel: [ 151.629192] overlayfs: filesystem on '/var/www/vhosts/user.deve/httpdocs' not supported as upperdir
kernel: [ 153.453612] overlayfs: filesystem on '/var/www/vhosts/user.deve/httpdocs' not supported as upperdir
From the terminal after executing from the browser:
kernel: [ 312.858797] overlayfs: upperdir is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.
kernel: [ 312.858804] overlayfs: workdir is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.
I just don't get the log from the browser since the script is same.

Automatically open website served with PHP built-in webserver

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

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 &>.

Can't open gnome-terminal with php

I actually try to laucnh a gnome-term with a php script, seems i have some problems with the users www-data;
my script make only a ls -l command in a directory (is just for a test) and i run it with a php page in my local-web site.
here the gnome-terminal command in my bash script (he run perfectly when i double-click on him) :
gnome-terminal --working-directory=/opt/cuckoo -x bash -c "ls -l"
and here is the call on the php-page :
system("/my/path/to/the/script/script.sh");
i have some echo in my script and i see them in the php page after i try to run the script with the php.page.
i think www-data don't have the right to do so i give the ownership of the script with the chown command, and at last a try the sudo visudo command and make the script execute like the user www-data is root (with NO PASSWD arg)
But i can't open the terminal and make a ls at last, i try with exec too, and show the result with $ouput butthe result is the same as well.
At last my question is : Php can really run a terminal or maybe a fool myself^^? Thanks for taking time to rescure me ;)
PHP can run everything, but depends who spawns it. Forget just running X apps from a web server - you'll need more than just executing them (permissions, DISPLAY and Xauth settings). Read more about the X clients and architecture.
Probably the right place to ask this is at SuperUser, since the problem is not in the coding itself.

Categories