下面来看一个关于php调用linux命令无权限的问题解决(visudo),希望文章对大家有帮助.
业务背景:yourcmd为我的linux程序,它对权限要求非常严格,当用php去执行yourcmd程序
系统:CentOS 6.3
apache是php的执行用户
用exec函数去执行linux系统上的程序/usr/local/yourcmd/sbin/yourcmd
立即学习“PHP免费学习笔记(深入)”;
php代码如下:
<?php
$conf_file = "/var/www/html/webroot/test.tmp";
$command = "sudo /usr/local/yourcmd/sbin/yourcmd -t -f {$conf_file}";
exec($command,$out);
print_r($out);测试结果为没有权限
Array ([0] => sudo: no tty present and no askpass program specified)
解决步骤:
$ visudo
1)注释以下行
#Defaults requiretty
2)在文件末尾加入以下
apache ALL=(ALL) NOPASSWD: ALL Cmnd_Alias yourcmd = /usr/local/yourcmd/sbin/yourcmd
最后测试结果:
Array ( [0] => Warning: memory is too small: 1044725760 [1] => test configure is ok )
文章链接:
随便收藏,请保留本文地址!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号