Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let php artisan serve run as a background server to work like Apache?

Tags:

php

I setup a Laravel app on a VPS. It's for demonstration purposes only.

I would ssh login to the VPS using PuTTY and type:

php artisan serve --host x.x.x.x

Everything works fine. However, when I close the PuTTY connection, the server shuts down.

Is it possible to let the artisan server run in the background just like Apache?

like image 963
user2837851 Avatar asked Aug 22 '14 01:08

user2837851


2 Answers

You can run any shell command in the background by adding & to the end. If you want it to continue to run after you've disconnected, then run it with nohup

nohup php artisan serve &

To kill it later, you'll be given a process ID, but don't be fooled because this kicks off other processes that will persist even when killed. To get the actual server PID, you can find it by filtering ps output with grep

ps -ef | grep "$PWD/server.php"

Should give you some output like this:

jeff 23978 23977 0 16:50 pts/4 00:00:00 /usr/bin/php7.0 -S 127.0.0.1:8000 /path/to/laravel-project/server.php

jeff 24059 18581 0 16:51 pts/4 00:00:00 grep --color=auto /path/to/laravel-project/server.php

The first number after your username is the PID you want to kill.

kill 23978

Don't do this for a production site, but a quick demo might be ok.

like image 183
Jeff Puckett Avatar answered Oct 06 '22 21:10

Jeff Puckett


Probably the quickest way to do this, at least on a temporary level, is to use screen - You can run it in a screen session and then Ctrl-a then d in Putty/shell to minimize it. It will continue running after your session closes.

You can resume and kill or restart later.

like image 30
nkozyra Avatar answered Oct 06 '22 22:10

nkozyra