Ubuntu – run php script in the background

command linePHPscripts

I tried the following command line code to run a php-script in the background.

php server.php &

This works. The problem is: if I close the terminal window on my computer, the php-script also stops. I want it to run "infinite". It's a websocket server so it has to be running constantly.

How can I achieve this?

Thanks in advance!

Edit: I found the answer already (asked this question way to early I guess).

nohup php server.php &

Best Answer

You can use screen to open a virtual terminal, where the script can run.

I recommend to use screen -S php_script. Then you can run your script (even without &). With CTRL + A, D you can disconnect from the termial. You can reconnect with screen -r back to the terminal.