Checking httpd status using php

Hello All,

i am running a script to check if apache is running on a linux server. when i run it on command line, it works but failing on the browser.

This could be as a result of permision issues. how can i go round it

<?php

class Services {

    //put your code here
    public function _construc() {
        
    }

    public function mysqld() {
        $command = "service mysqld status";
        $res = $this->execCommand($command);

        $array = explode(" ", $res);
        $ret = in_array("running...", $array, TRUE) ? TRUE : FALSE;

        return $ret;
    }

    public function startMysqld() {
        $command = "service mysqld restart";
        $res = $this->execCommand($command);
        return $res;
    }

    public function httpd() {
        $command = "service httpd status";
        
        $res = $this->execCommand($command);

        $array = explode(" ", $res);
        $ret = in_array("running...", $array, TRUE) ? TRUE : FALSE;

        return $ret;
    }

    public function startHttpd() {
        $command = "service httpd start";
        
        $res = $this->execCommand($command);
    }

   private function execCommand($command) {
        $res = exec($command);
        return $res;
    }

}

session_start();

include_once 'DBconnect.php';
include_once 'template.php';
include_once './config/config.inc.php';
showheader("Services");

include_once 'header.php';


$ser = new Services();

$mysqld = $ser->mysqld();
$httpd = $ser->httpd();

echo "<table width='100%' align=center><tr><td class=hdr>Service</td><td class=hdr>status</td><td class=hdr>Action</td></tr>";
echo "<tr bgcolor='#FAFAFA'><td><img src='images/s_db.png'>Database</td><td>";
if ($mysqld) {
    echo "Image for Running";
} else {
    echo "<img src='images/s_error.png'>Error!! Service stopped";
}
echo "</td><td><img src='images/s_reload.png'>Reload</td></tr>";
echo "<tr bgcolor='#FAFAFA'><td><img src='images/s_host.png'>Webserver</td><td>";
if ($httpd) {
    echo "Image for Running";
} else {
    echo "<img src='images/s_error.png'>Error!! Service stopped";
}
echo "</td><td><img src='images/s_reload.png'>Reload</td></tr>";
echo "<tr bgcolor='#FAFAFA'><td><img src='images/s_cog.png'>SMS Gateway</td><td>";

echo "</td><td><img src='images/s_reload.png'>Reload</td></tr>";
echo "</table>";

showFooter();

Are you on a shared or dedicated server. If you’re on the former than changes are that the host does not allow exec from PHP. Otherwise, you may be able to tune the php ini to allow exec since it is probably disallowed currently. Also, in most cases only the sudo user can start and stop apache. On most Linux installations the apache service is apache2. So not sure if that might also be part of the problem or you just have a different set-up.

Oddz,

thanks for you response. it is dedicted server and the service is httpd.

I would just try creating a simple file in the web root with a single exec command. If that returns false or errors than it is likely that exec is not currently allowed. I haven’t used exec much but the docs should be a good starting place to understand the meaning behind return values and errors. If it does work than I think the issue lies with needing to use sudo to run those specific commands. That is my guess at it.

I can’t imagine how such a script would be worth the security risk incurred.

Can I ask a question that seems obvious to me, but maybe because I’m missing something? How will your web page run, if the httpd service isn’t running? Or are you checking the httpd service on a different server than the one running your code?

Droopsnoot,

You have a valid question. I will be checking not only the httpd but others service as well such is SMPP service. if i can get it for httpd, i will be able to get it for others

What I was really asking was does Apache / PHP on the server side have some means of still executing when httpd is not running? I haven’t used it in a production environment, only other web servers, and I was kind of saying your code specifically for checking the httpd service is running might as well read

echo "httpd is running";

if it doesn’t have such a feature, because if it is not, your page just won’t appear.

Apache IS httpd, so no. No httpd process, no apache.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.