Linux 为什么 php 命令 `exec("service apache2 restart");` 在 ubuntu 上不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8202887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:23:27  来源:igfitidea点击:

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

phplinuxapacheubuntu

提问by EBAG

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

我需要使用配置了 apache 的 php 在我的 Web 服务器上执行一些命令。

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?

我的猜测是因为我的 ubuntu 上的 php 权限!你有什么建议?

采纳答案by Gilles Quenot

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

在文件顶部使用您自己的本地 IP,然后添加一行

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

And last in your PHP file :

最后在你的 PHP 文件中:

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

(You are trying to restart apache by web, maybe you don't know webmininterface ? 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 ;) )

(您正在尝试通过 web 重新启动 apache,也许您不知道webmin界面?我认为有比这种 sudo 方式更好的解决方案。授权 www-data 停止,启动(...)所有这些并不是一件好事服务。最好解释一下为什么要重新启动 apache ;) )

回答by John Zwinck

Did you look at the Apache error log like it says? What's in there?

你有没有像它说的那样查看Apache错误日志?里面有什么?

Almost certainly your PHP script is running without sufficient permissions to restart Apache. Which is probably for the best. If you really really need this to work, consider making a setuid root script to invoke from PHP (note that this should not be used in production, and will probably create a security hole).

几乎可以肯定,您的 PHP 脚本正在运行,但没有足够的权限来重新启动 Apache。这可能是最好的。如果你真的需要它来工作,考虑制作一个 setuid root 脚本来从 PHP 调用(注意这不应该在生产中使用,并且可能会创建一个安全漏洞)。

You could also write a little service which runs as root and accepts commands to restart Apache from your PHP script. That'd be a more "proper" way of doing this, though the task at hand seems itself improper, so I'm not sure you should continue down this path.

您还可以编写一个以 root 身份运行并接受命令以从您的 PHP 脚本重新启动 Apache 的小服务。这将是一种更“正确”的方法,尽管手头的任务本身似乎不合适,所以我不确定您是否应该继续沿着这条路走下去。

回答by joschi

Services (in the sense of system services, like Apache httpd) can only be manipulated (started, stoppped, restarted) by the root user (UID 0).

服务(就系统服务而言,如 Apache httpd)只能由 root 用户(UID 0)操作(启动、停止、重新启动)。

Either you run the PHP script in root's context (badidea) or you use something like sudoto run the commands as super user. This said, there are very serious security implications when running programs with super user privileges, especially if you don't sanitize your inputs properly!

要么在 root 的上下文中运行 PHP 脚本(主意),要么使用诸如sudo 之类的东西以超级用户身份运行命令。这就是说,以超级用户权限运行程序时存在非常严重的安全隐患,特别是如果您没有正确清理输入!