Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why php command `exec("service apache2 restart");` does't work on ubuntu?

I need to execute some commands on my web server with php configured with apache.

exec("service apache2 restart", $output);
print_r($output);

output:

Array (
    [0] =>  * Restarting web server apache2
    [1] => Action 'start' failed.
    [2] => The Apache error log may have more information.
    [3] =>    ...fail! 
)

My guess is it's because of permissions of php on my ubuntu! What do you suggest?

like image 316
EBAG Avatar asked Nov 20 '11 16:11

EBAG


1 Answers

You need to run :

visudo

check that you have a line like

Host_Alias LOCAL=192.168.0.1 

with your own local IP at the top of the file, then add a line

www-data       LOCAL=NOPASSWD:/usr/bin/service

And last in your PHP file :

exec("/usr/bin/sudo /usr/bin/service apache2 restart");

(You are trying to restart apache by web, maybe you don't know webmin interface ? I think there's betters solutions than this sudo way. It's not a good thing to authorize www-data to stop, start (...) all the services. Better explain why you'd like to restart apache ;) )

like image 79
Gilles Quenot Avatar answered Sep 22 '22 15:09

Gilles Quenot