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?
Share
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
sudocommand.You’ll want to edit your
/etc/sudoersfile and add lines similar to the following:You may need
nobodyinstead ofwww-data, it depends on the user which Apache runs under. On Debian, Apache typically runs under userwww-data, whereas under Red Hat, often Apache runs under usernobody. Also, the/sbin/service apache2 restartmay need to be/sbin/service apache restartor maybe/sbin/service httpd restart. All depends on your system’s configuration.Once that’s done, in PHP you can use the code:
(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
sudobinary, theservicebinary, 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.