logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Retrieving Apache command line output
Author
Otomatic



Joined: 01 Sep 2011
Posts: 213
Location: Paris, France, EU

PostPosted: Mon 01 Nov '21 18:49    Post subject: Retrieving Apache command line output Reply with quote

Hi,

With the PHP script below, there is no problem retrieving the results from Apache on the command line:
Code:

$command = 'CMD /D /C '.$c_apacheExe.' -t -D DUMP_VHOSTS';
ob_start();
passthru($command);
$output = ob_get_contents();
ob_end_clean();

This works very well for other commands like:
Code:

$command = 'CMD /D /C '.$c_apacheExe.' -t -D DUMP_MODULES';
$command = 'CMD /D /C '.$c_apacheExe.' -t -D DUMP_INCLUDES';
$command = 'CMD /D /C '.$c_apacheExe.' -t -D DUMP_RUN_CFG';
and so on...

But, strangely, it doesn't work for the command:
Code:
$command = 'CMD /D /C '.$c_apacheExe.' -t';

that run syntax check for config files whether without errors (Syntax OK) or with errors, the result ($output) is hopelessly empty.
After many tests, verifications, research, it appears that even without errors in the configuration files the result is always sent to STDERR and not to STDOUT.
In a Windows command window, STDOUT and STDERR are "mixed up", so you see everything.

So I had to find a way to retrieve "at the same time" the displays of STDOUT and STDERR by a procedure using proc_open().

I don't know if this is the best way, but it works!
Code:

$command = $c_apacheExe.' -t';

$descriptorspec = array(
   0 => array('pipe', 'rb'), // stdin
   1 => array('pipe', 'wb'), // stdout
   2 => array('pipe', 'wb')  // stderr
);
$process = proc_open(escapeshellarg($command), $descriptorspec, $pipes);
$output = '';
while (!feof($pipes[1])) {
  foreach($pipes as $key => $pipe) {
    $line = stream_get_line($pipe,0);
    if($line !== false ) $output .= $line."\n";
  }
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

And in $output, there is either 'Syntax OK' or errors if there are any.
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Wed 03 Nov '21 23:47    Post subject: Reply with quote

httpd -S runs all that check at once.

I wonder why you parse that?
Back to top
Otomatic



Joined: 01 Sep 2011
Posts: 213
Location: Paris, France, EU

PostPosted: Thu 04 Nov '21 10:45    Post subject: Reply with quote

Hi,

This is part of the "tools" integrated in Wampserver.
Wampserver is mainly a development server for beginners who don't know much about Apache, PHP, MySQL or MariaDB or even about Windows like opening a command window or typing a command line.
This is why various features of Apache (but also of PHP or MySQL) are done through menus accessible from the Wampmanager icon in the taskbar, like :

https://aviatechno.net/files/wamp/wamp3.2.6_tools_menu.jpg

Below, the results of "Check httpd.conf syntax" without error.
https://aviatechno.net/files/wamp/wamp3.2.6_tools_syntax_ok.jpg

with error

https://aviatechno.net/files/wamp/wamp3.2.6_tools_syntax_error.jpg
Back to top


Reply to topic   Topic: Retrieving Apache command line output View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner