revert 'php artisan serve' command in laravel - php

I have a laravel app. I ran 'php artisan serve' command for local testing and my app was served at localhost:8000. But now I want to stop serving it at localhost:8000 ie., I want it not to serve now.
I closed the command line, restarted the server but it is still serving. How can it be done?
Note: I am using windows for testing purposes.

here is what i do
press ctrl+ c
> lsof -i :8000
to check if the port is busy or not if any process is listening to the port 8000 it will be displayed with port id
> sudo kill -9 [PID]
kill the process that listen to that port id
run lsof agin
> sudo lsof -i 8000
vola

Press Ctrl + Shift + ESC. Locate the php process running artisan and kill it with right click -> kill process.
Reopen the command-line and start back the server.
Note that you should be able to kill the process just by sending it a kill signal with Ctrl + C.

I used Ctrl + C in my mac for stop the command php artisan laravel serve

For windows user, type this command for showing the list of current running php artisan process:
tasklist /FI "IMAGENAME eq php.exe"
Will show:
Image Name ---- PID ---- Session Name ---- Session ---- Mem Usage
php.exe --------- XXXX ---- Console ---------- 4 ------- 19.852 K
Look up PID number (XXXX), and type this to kill the process:
taskkill /F /PID XXXX

When I use cygwin to run my php artisan serve on Windows machine, pressing ctrl + c does not kill the process. To stop it, use #jsalonen's solution. Basically you are looking for this then kill it:

Windows 10 with XAMPP server depending on what shell you use to run:
php artisan serve
In Windows CMD: CTRL + C
In XAMPP Shell: CTRL + Pause/Break
In Ubuntu and RedHat/CentOS terminal it's: CTRL + C

This work for me.
sudo kill $(sudo lsof -t -i:port_number)

Ctrl + Pause|Break should do it for Windows.

If your using VS Code, simply do the following:
Open VS Code terminal
Find the drop down box that displays a list of all the running shells
Select "php" from the list and click the garbage bin icon
This deletes the shell and kills the server.

In Windows CMD or smder: CTRL + C

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.

Starting FOREVER or PM2 as WWW-DATA from a PHP script

I have a nodejs script named script.js.
var util = require('util');
var net = require("net");
process.on("uncaughtException", function(e) {
console.log(e);
});
var proxyPort = "40000";
var serviceHost = "1.2.3.4";
var servicePort = "50000";
net.createServer(function (proxySocket) {
var connected = false;
var buffers = new Array();
var serviceSocket = new net.Socket();
serviceSocket.connect(parseInt(servicePort), serviceHost);
serviceSocket.pipe(proxySocket).pipe(serviceSocket);
proxySocket.on("error", function (e) {
serviceSocket.end();
});
serviceSocket.on("error", function (e) {
console.log("Could not connect to service at host "
+ serviceHost + ', port ' + servicePort);
proxySocket.end();
});
proxySocket.on("close", function(had_error) {
serviceSocket.end();
});
serviceSocket.on("close", function(had_error) {
proxySocket.end();
});
}).listen(proxyPort);
I am runing it normally like nodejs script.js, but now i want to include forever or pm2 functionalities as well. When i am root everything works smootly:
chmod -R 777 /home/nodejs/forever/;
-- give rights
watch -n 0.1 'ps ax | grep forever | grep -v grep'
-- watch forwarders (where i see if a forever is opened)
/usr/local/bin/forever -d -v --pidFile "/home/nodejs/forever/file.pid" --uid 'file' -p '/home/nodejs/forever/' -l '/home/nodejs/forever/file.log' -o '/home/nodejs/forever/file.log' -e '/home/nodejs/forever/file.log' -a start /etc/dynamic_ip/nodejs/proxy.js 41789 1.2.3.4:44481 414 file
-- open with forever
forever list
-- it is there, i can see it
forever stopall
-- kill them all
The problem is when i want to run the script from a PHP script with the system or exec functions :
sudo -u www-data /usr/local/bin/forever -d -v --pidFile "/home/nodejs/forever/file.pid" --uid 'file' -p '/home/nodejs/forever/' -l '/home/nodejs/forever/file.log' -o '/home/nodejs/forever/file.log' -e '/home/nodejs/forever/file.log' -a start /etc/dynamic_ip/nodejs/proxy.js 41789 1.2.3.4:44481 414 file
-- open as www-data (or i can do this just by accessing `http://1.2.3.4/test.php`, it is the same thing)
forever list
-- see if it is there, and it is not (i see it in watch)
forever stopall
-- says no forever is opened
kill PID_ID
-- the only way is to kill it by pid ... and on another server all of this works very well, can create and kill forevers from a php script when accessing it from web ... not know why
-- everything is in /etc/sudoers including /usr/local/bin/forever
Why is that? How can i solve this?
I also made some trick, created a user 'forever2', i created a script.sh with this content :
sudo su forever2 user123; /usr/local/bin/forever -d -v --pidFile "/home/nodejs/forever/file.pid" --uid 'file' -p '/home/nodejs/forever/' -l '/home/nodejs/forever/file.log' -o '/home/nodejs/forever/file.log' -e '/home/nodejs/forever/file.log' -a start /etc/dynamic_ip/nodejs/proxy.js 41789 1.2.3.4:44481 414 file;
where user123 is not existent, is just a trick to exit the shell after execution. The script works, runs forever, i can close all forevers with the command forever stopall from root. When i try the same thing running the http://1.2.3.4/test.php or as www-data user i cannot close it from root or www-data, so not even this works.
I tried from Ubuntu 14.04.3 LTS, Ubuntu 14.04 LTS , Debian GNU/Linux 8 ... still the same thing.
Any ideeas?
Thanks.
If you are starting the process from within Apache or the web-server you are already as the www-data user, so doing a sudo su to the user context you already have is likely not necessary.
When you start this forever task you may also be required to shut the terminals/inputs and directly send to background. Something like this:
// Assemble command
$cmd = '/usr/bin/forever';
$cmd.= ' -d -v --pidfile /tmp/my.pid'; // add other options
$cmd.= ' start';
$cmd.= ' /etc/dynamic_ip/nodejs/proxy.js';
// "magic" to get details
$cmd.= ' 2>&1 1>/tmp/output.log'; // Route STDERR to STDOUT; STDOUT to file
$cmd.= ' &'; // Send whole task to background.
system($cmd);
Now, there won't be any output here but you should have something in /tmp/output.log which could show why forever failed, or the script crashed.
If you've been running the script sometimes as root, then trying the same command as www-data you may also be running into a permissions on one or more files/directories created from the execution as root which now conflict when running as www-data.
This is part of PHP security you say you're running it from a php script and your not your running it from Apache via a php script.
PHP web scripts should not have root access as such they run under the same permissions as Apache user www-data.
There are ways to prevent php running as root but run a task as root but it's a little hacky and I'm not going to share the code but I will explain so you can look into it. here is where to start
http://php.net/manual/en/function.proc-open.php
With a Proccess like this you can then execute a proc. Like your script.js via nodeJS using SUDO and then read stdOut and stdErr wait for password request then provide it by writing to stdIn for that process.
Don't forget in doing this the user www-data has to have a password and be in the sudoers list
Per the OPs Comment
Due to the way SUDO works the PATH does not appear to contain the path to the node executables npm, node so your best of building a .sh (bash script) and using sudo to run that.
You still need to monitor this process as it will still ask for a password
#!/bin/bash
sudo -u ec2-user -i
# change this to the path you want to run from
cd ~
/usr/local/bin/pm2 -v

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 a php file forever in ubuntu by command line

I am using web-socket in php to run a chat service problem is when i close the command line the connection also close , is there a solution so that the chat service can run forever .
i am using this command
php -q server.php
create a php file on your server name it as start_server.php
and post this content
<?php
$shell_command = "php -q server.php > /dev/null &";
shell_exec($shell_command);
?>
now execute this command
php start_server.php
I am using screen for running php script and so far I haven't really faced any issues.
screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
More about screen here.
One drawback with using screen is that if you make a change in your script you will have to terminate the virtual terminal and create a new one for the changes to take effect.
Here is a sample code:
$ screen -L php /path/to/php/script
This will create a new virtual terminal.
Some useful commands:
To detach screen (once you are in the virtual terminal): ctrl + a + d
To resume screen (from your main terminal once detached): $ screen -r
If there are multiple screens running on your machine the above command should be followed by the pid.tty.host string which is unique for each screen
To terminate screen: ctrl + c
To list pid.tty.host strings of each terminal: $ screen -ls

Stopping in-built php server on Mac Mavericks - Livecode

I'm developing something in Livecode and I have been experimenting with using Mavericks own in-built php server. I started the server by sending the following command through shell...
php -S localhost:8000
This enabled PHP to run successfully through localhost:8000/
However, I can not work out how to stop/disable PHP now in order to continue testing starting it - when I previously started PHP through the terminal I was able to do ctrl+c to stop php running but since I do not yet know how to do this through my app I get this error instead...
Failed to listen on localhost:8000 (reason: Address already in use)
Anybody know how I can stop it either via the terminal or through my Livecode app? Attempts to stop it through the terminal using just ctrl+c do not work
open a terminal and type:
ps -ef | grep php
it will list the php process with the pid (process id)
something like
$ ps -ef | grep php
501 14263 14133 0 10:25AM ttys001 0:00.21 php -S localhost:8000
501 14355 14265 0 10:25AM ttys002 0:00.00 grep php
The note the number for the line that lists your php process, the second column is your pid
in the example the process id us 14263, kill it:
$ kill 14263
do another ps
$ ps -ef | grep php
501 14358 14265 0 10:26AM ttys002 0:00.00 grep php
$
The process should not be listed anymore

Categories