PHP exec() 在 Raspberry apache 服务器上不起作用


PHP exec() is not working on Raspberry apache server

我有一个带有Apache服务器的Raspberry Pi(PHP5)。我得到了一个代码来控制我的中继模块,但 PHP 不执行 python 脚本。

这是代码:

<html>
<head>
<meta charset="UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<?php
if (isset($_POST['LightON']))
{
exec("sudo python /home/pi/lighton_1.py");
}
if (isset($_POST['LightOFF']))
{
exec("sudo python /home/pi/lightoff_1.py");
}
?>
<form method="post">
<button class="btn" name="LightON">Light ON</button>&nbsp;
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>
</html>

查看php.ini是否启用了exec()。如果不是,请取消注释并重新启动您的 PHP 进程(也许还有 apache)

编辑:
提交编辑后,我注意到您在 PHP exec()语句中使用了sudo。如果您的根用户受密码保护(它确实应该!

您应该尝试使用 shell_exec 命令,它对我有用,我有相同的设置。