- Blog
- Howtos
- anything generator
- apache
- asterisk
- autofs
- autoload
- automount
- backup db
- callcentric
- centos
- chumby
- cipher list
- cookies
- ctags
- dovecot
- glue fleece
- hacking
- httpd
- IE
- iFrame
- ispconfig
- javascript
- lighttpd
- media player
- move networks
- mysql
- mysqldiff
- mythtv
- Network Solutions
- openssl
- os x
- osx
- P3P Compact Policy
- php
- postfix
- proftpd
- proxy
- python
- screen scraping
- shell
- shell scripts
- slapd
- smb
- ssh
- sshfs
- SSLCertificateChainFile
- sslv2
- stunnel
- suphp
- taglist
- telnet
- trace
- verisign
- vi
- vsftpd
- Scripts
- About
Reload httpd via PHP (sorta)
Submitted by adam on Fri, 2006-10-27 10:27.
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');
