8

I'm playing around with my VM and the code I'm developing requires that I restart apache every time to capture the changes. I thought it would be nice to just have a bookmarklet, or link or button click I could run to do this. Any thoughts on how to accomplish this with PHP on a CentOS 5 dev VM?

qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • 1
    You'd need to have a script that can get root permissions, or is in sudoers to this... Apache cannot restart itself, because the apache child processes the PHP scripts run in cannot affect their parent process, which is usually running as root. – Marc B Aug 25 '11 at 16:53
  • 1
    @Marc `which is usually running as root` ??? I have a doubt about that. – Luc M Aug 25 '11 at 17:02
  • 1
    @Luc: there's always one apache process running as root. The children it forks to handle actual requests will run as nobody/www-data/etc... – Marc B Aug 25 '11 at 17:03

3 Answers3

17

As Marc B said, you need root privs to be able to restart Apache. The best way to handle this, IMHO, is to give the user that Apache runs under access to restart Apache via the sudo command.

You'll want to edit your /etc/sudoers file and add lines similar to the following:

Cmnd_Alias      RESTART_APACHE = /sbin/service apache2 restart

www-data ALL=NOPASSWD: RESTART_APACHE

You may need nobody instead of www-data, it depends on the user which Apache runs under. On Debian, Apache typically runs under user www-data, whereas under Red Hat, often Apache runs under user nobody. Also, the /sbin/service apache2 restart may need to be /sbin/service apache restart or maybe /sbin/service httpd restart. All depends on your system's configuration.

Once that's done, in PHP you can use the code:

exec('/sbin/service apache2 restart');

(Obviously changing that if the command to restart Apache differs on your server.)

Please note: this could very well be considered a security risk! If you do this, you fully trust the sudo binary, the service binary, and your system to obey the rules and not let an Apache/PHP process get a root shell. I highly recommend asking on http://serverfault.com for the implications of what you're doing here.

Josh
  • 10,961
  • 11
  • 65
  • 108
4
<?php echo shell_exec('service httpd restart &'); ?>

You might have permissions problems with such a script attempting to do this though. It sounds like you're in a full-on dev environment though, so it shouldn't matter for you to give elevated privileges to it.

Rogus
  • 1,218
  • 10
  • 11
  • 1
    Actually, I have to take this back: as the others say (I just tried it) you can't restart apache from within an apache process (php). – Rogus Aug 25 '11 at 17:03
  • yeah I was thinking about this but then I ran into an issue where the script that would trigger the restart keeps apache running because apache doesnt time out until the web process ends – qodeninja Aug 25 '11 at 17:03
0

A file contains the process ID for apache. Just send a HUP to it. A simple script (sh) could be written to achieve this - and place it in the cgi-bin directory.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • This won't work as the PID you need to send an HUP to will be running as root, and the UID of the process PHP will be running under will be either `nobody`, `www-data`, or some other non-root user – Josh Aug 26 '11 at 19:18
  • @Josh - Consider the configuration parameters i.e. user directive http://httpd.apache.org/docs/2.2/mod/mpm_common.html#user – Ed Heal Dec 12 '13 at 19:54
  • @Ed: that should never be set to `0` or `root`. [Apache should always be run as a dedicated user.](http://unix.stackexchange.com/questions/83734/with-which-user-should-i-run-web-servers-redis-mongodb) – Josh Dec 12 '13 at 20:33
  • @John - Exactly - Read *YOUR* comment on the 26 Aug – Ed Heal Dec 13 '13 at 00:36