Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /home/adam/sites/adamyoung.net/index.php:2) in drupal_send_headers() (line 1043 of /home/adam/sites/adamyoung.net/includes/bootstrap.inc).
  • Warning: Cannot modify header information - headers already sent by (output started at /home/adam/sites/adamyoung.net/index.php:2) in drupal_send_headers() (line 1043 of /home/adam/sites/adamyoung.net/includes/bootstrap.inc).
  • Warning: Cannot modify header information - headers already sent by (output started at /home/adam/sites/adamyoung.net/index.php:2) in drupal_send_headers() (line 1043 of /home/adam/sites/adamyoung.net/includes/bootstrap.inc).
  • Warning: Cannot modify header information - headers already sent by (output started at /home/adam/sites/adamyoung.net/index.php:2) in drupal_send_headers() (line 1043 of /home/adam/sites/adamyoung.net/includes/bootstrap.inc).
  • Warning: Cannot modify header information - headers already sent by (output started at /home/adam/sites/adamyoung.net/index.php:2) in drupal_send_headers() (line 1043 of /home/adam/sites/adamyoung.net/includes/bootstrap.inc).

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');

Comments

Thanks for the post. I build the file and it works at restarting apache so long as I am logged in as root but when I try and run it from exec command with php - nada?!?! I am not really adept at unix yet so I fumbled around just figuring out how to compile the source to begin with. I just did gcc to compile your code? Is that right? I just took your code copied it as httpd_reload.c did a gcc to it. Created a httpd_reload file - changed it to be executable and it runs and gives me this output...

job 5 at 2009-04-04 13:09

not sure what this is?

Anyhow - I am stuck... any advice would be appreciated. hguth@listingsmagic.com

Did you use the compile commands from the source code?
cc -o httpd_reload httpd_reload.c

Is the at deamon running?
/etc/init.d/atd start

Did you chmod the binary?
chmod +s httpd_reload

The binary returns the same output for me and it is reloading httpd.

Saat ini <a href="http://psychobone.idnyul.com/">Mobil Keluarga Ideal Terbaik Indonesia</a> menjadi perhatian tidak hanya keluarga indonesia, melainkan segmen di Indonesia. <a href="http://idnyul.com/newport-beach-houses/">Newport Beach Houses</a> is a charming coastal community in Orange County, California. It's also well known as the wealthiest city in the United States.

You really are an expert in your field. This is very helpful for web developers like me. Thanks a lot! <a href="http://gownsforweddings.com/">gowns for weddings</a>

You have a very interesting post that is worth sharing. Thanks! <a href="http://www.defineerp.com/enterprise-resource-planning-system/">enterprise resource planning system</a>

Very interesting post worth mentioning. Thanks for sharing this code. <a href="http://internshipcoverletter.com/resume-cover-letter/">resume cover letter</a>