上一篇:
Linux /proc优化

php通过sudo执行root指令
[ 2009/10/09 17:15 | by selboo ]
例如
apache使用nobody用户运行,现在需要通过php来重启apache
首先编辑 /etc/sudoers 使用 visudo来编辑
#visudo
在最下面添加一行
nobody ALL=NOPASSWD:/usr/local/apache/bin/apachectl restart
然后注释掉文件中的Defaults requiretty这行
否则会出现sudo: sorry, you must have a tty to run sudo的错误
保存退出即可
测试
写一个php文件,内容如下
引用
<?php
$output = shell_exec("/usr/bin/sudo /usr/local/apache/bin/apachectl restart");
echo $output;
?>
$output = shell_exec("/usr/bin/sudo /usr/local/apache/bin/apachectl restart");
echo $output;
?>