Reload httpd via PHP (sorta)

The problem:
Apache forked processes (PHP, CGI, etc) runs as a different user (usually www or some variant) which doesn't have permission to restart Apache. To further compound the problem, PHP doesn't allow you to sudo to a user that does have the privileges (they call this a "security feature"). Also, if it did have the privileges, it cannot reload httpd directly because that's it's parent process.

The Fix:
Have PHP invoke a binary that does have the ability to sudo. From there, put a reload request into the atq (this assumes you have atd running) to avoid the whole parent process problem.

The code:

/***********************************************************
 * make sure to chmod +s the binary after compiling:
 * cc -o httpd_reload httpd_reload.c ; chmod +s httpd_reload
 ***********************************************************/

#include <stdio.h>
#include <stdlib.h>

int main() {
  if (!setuid(geteuid())) {
    system("/bin/echo '/sbin/service httpd reload > /dev/null 2>&1' | /usr/bin/at now");
  } else {
    printf("Couldn't set UID to effective UID\n");
    return 1;
  }
  return 0;
}

PHP code:
You can use other scripting languages here...

exec('/path/to/httpd_reload');