3

I want to restart apache2 when I load a page with the following code:

exec('/etc/init.d/apache2 reload', $output, $return);
if(!$return) {
    $result = "<script>console.log('can not restart apache2');</script>";
    echo $result;
    echo $output;
} else {
    $result = "<script>console.log('restart apache2 successfuly');</script>";
    echo $result;
}

And in file etc/sudoers I add this lines:

Cmnd_Alias      RESTART_APACHE = /etc/service apache2 restart
www-data ALL=NOPASSWD: RESTART_APACHE

But the result return can not restart apache2.

Am I do something wrong?

tuanptit
  • 353
  • 2
  • 5
  • 14
  • 3
    Possible duplicate of [why php command \`exec("service apache2 restart");\` does't work on ubuntu?](http://stackoverflow.com/questions/8202887/why-php-command-execservice-apache2-restart-doest-work-on-ubuntu) – cb0 Aug 31 '16 at 06:13
  • Your sudoers is saying restart is allowed but you are reloading in your exec(). This is a really bad idea in terms of security, why do you want this? Why not stick with SSH access, or a process monitor or something? – jedifans Aug 31 '16 at 06:28

3 Answers3

1

@tuanptit
<?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.

But the best way 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.

Community
  • 1
  • 1
Manish
  • 3,443
  • 1
  • 21
  • 24
  • I changed to use this line `` but not working, it's say `httpd: unrecognized service` in error.log file in apache. And I also can not found `/sbin/service` in server – tuanptit Aug 31 '16 at 06:41
0

The Apache service probably dont have rights to restart itself

user3332631
  • 334
  • 1
  • 3
  • 11
0

The solution already discussed here.

How do you restart Apache with a (web) button click?

In the sudoers files you have "restart", in php file you have "reload"

Check if you need to use /sbin/service instead of /etc/service

Make sure the commands match in PHP and sudoer file

Community
  • 1
  • 1
Raghav Tandon
  • 459
  • 5
  • 9