As Mark B said, you need root privileges to restart Apache. The best way to handle this, IMHO, is to let the user know that Apache is running under access to restart Apache through the sudo .
You want to edit the /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 Apache is running on. On Debian, Apache usually runs under the www-data user, while on Red Hat, Apache often runs under the nobody user. In addition, /sbin/service apache2 restart may require /sbin/service apache restart or maybe /sbin/service httpd restart . It all depends on your system configuration.
After that, in PHP you can use the code:
exec('/sbin/service apache2 restart');
(Obviously, this is a change if the Apache restart command is different on your server.)
Please note: this may be considered a security risk! If you do this, you completely trust the sudo binary, the service binary and your system to abide by the rules and prevent the Apache / PHP process from getting the root shell. I highly recommend asking http://serverfault.com for an understanding of what you are doing here.
source share