为什么 php 命令 `exec("service apache2 restart");` 在 ubuntu 上不起作用? [英] why php command `exec("service apache2 restart");` does't work on ubuntu?
问题描述
我需要使用配置了 apache 的 php 在我的 web 服务器上执行一些命令.
I need to execute some commands on my web server with php configured with apache.
exec("service apache2 restart", $output);
print_r($output);
<小时>
输出:
Array (
[0] => * Restarting web server apache2
[1] => Action 'start' failed.
[2] => The Apache error log may have more information.
[3] => ...fail!
)
我的猜测是因为我的 ubuntu 上的 php 权限!你有什么建议?
My guess is it's because of permissions of php on my ubuntu! What do you suggest?
推荐答案
你需要运行:
visudo
检查您是否有类似的行
Host_Alias LOCAL=192.168.0.1
在文件顶部加上你自己的本地IP,然后添加一行
with your own local IP at the top of the file, then add a line
www-data LOCAL=NOPASSWD:/usr/bin/service
最后在你的 PHP 文件中:
And last in your PHP file :
exec("/usr/bin/sudo /usr/bin/service apache2 restart");
(您正在尝试通过 web 重新启动 apache,也许您不知道 webmin 接口?我想有比这种 sudo 方式更好的解决方案.授权 www-data 停止、启动 (...) 所有服务并不是一件好事.最好解释一下你为什么要重启 apache ;))
(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 ;) )
这篇关于为什么 php 命令 `exec("service apache2 restart");` 在 ubuntu 上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!