forked from sinacms/MultiHttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-server.php
More file actions
executable file
·47 lines (38 loc) · 1.62 KB
/
bootstrap-server.php
File metadata and controls
executable file
·47 lines (38 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
date_default_timezone_set('Asia/Shanghai');
$php_version = phpversion();
$php_major = floatval(substr($php_version, 0, 3));
defined('WEB_SERVER_PORT') ? : define('WEB_SERVER_PORT', mt_rand(1024,2048));
define('TEST_SERVER', 'http://'.WEB_SERVER_HOST . ':' . WEB_SERVER_PORT);
// Define SIGKILL if pcntl is not found
if (!function_exists('pcntl_signal')) {
define('SIGKILL', 9);
}
if ($php_major < 5.4) {
define('WITHOUT_SERVER', true);
} else {
// Command that starts the built-in web server
$command = sprintf('php -S %s:%d -t %s >./server.log 2>&1 & echo $!', WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);
// Execute the command and store the process ID
$output = array();
exec($command, $output, $exit_code);
// sleep for a second to let server come up
sleep(1);
$pid = (int) $output[0];
// check server.log to see if it failed to start
$server_logs = file_get_contents("./server.log");
if (strpos($server_logs, "Fail") !== false) {
// server failed to start for some reason
print "Failed to start server! Logs:" . PHP_EOL . PHP_EOL;
print_r($server_logs);
exit(1);
}
echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;
register_shutdown_function(function() {
// cleanup after ourselves -- remove log file, shut down server
global $pid;
unlink("./server.log");
posix_kill($pid, SIGKILL);
echo sprintf('%s - Web server terminal on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid) . PHP_EOL;
});
}