I created a server using php. I also created an html page that acts as the control panel for the php server. If I visit the control panel page and the php server is not running, I want to push on a button which creates a new separate process that launches the php server.
Here is the code that launches the php server (launchserver.php) using a terminal: #!/usr/bin/php -q
<?php
error_reporting(E_ALL);
require_once 'lib/server.php';
require_once 'lib/client.php';
set_time_limit(0);
// Get the local ip address of the host machine (NOTE: Not the loop back address)
$command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'";
$ipaddress = exec ($command);
$server = new Server($ipaddress, 15000);
$server->run();
?>
To run the server, I just open a terminal and execute "php -q launchserver.php".
However, I don't want to launch the php script using a terminal, I want to use my new control panel page, where I can click on a button and the button creates a separate process which executes the launchserver.php script and stands up the server and if I leave the control page, the server does not die because I'm no longer in the control page.
I had to do something similar recently (had to run some php code on another file using an HTML button).
This is my suggestion (I am a "junior", so there probably is a better/simpler solution):
Change your launchserver.php file to this:
<?php
if($_POST) {
error_reporting(E_ALL);
require_once 'lib/server.php';
require_once 'lib/client.php';
set_time_limit(0);
// Get the local ip address of the host machine (NOTE: Not the loop back address)
$command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'";
$ipaddress = exec ($command);
$server = new Server($ipaddress, 15000);
$server->run();
}
?>
Then you need a button with "onclick". e.g. <button onclick="startMyServer();">Start My Server</button>
Then the JS function:
function startMyServer() {
var startServer = 1;
$.post("<full url here>/launchserver.php", { 'startServer': startServer }, function(d){
});
}
This will act like you sent a form to the file. $.post requires JQuery, so be sure to include that.
So if you click the button it will send $_POST to your php file and if $_POST was sent, then the code in the php file runs.
I hope this is helpful. I used this JQuery function to run some php code ( mysql queries located on another file) and it works flawlessly for me.
Since you mentioned that "I do have apache2 httpd web server running with php", it should do the trick for you.