duankousong9637 2019-07-28 17:00
浏览 107

通过Apache HTTP服务器从网站执行python脚本

I have a Raspberry 3B+ and I would like to run a python script on it. This script should be executed when a button on a website is pressed. I have created an Apache2 http server on the Raspberry. It works fine, I can access it from a browser on my notebook. Also the button is displayed as desired. In /var/www/html/index.php I have

<?php
if (isset($_POST['BUTTON']))
{
$output=shell_exec('sudo python /var/www/newDir/test.py');
echo "Output: <pre>$output</pre>";
}
?>

The contents of test.py is simply

print "it works"

The problem is that when I push the button on the website, it just displays "Output:" and that's it. Expected is "Output:it works". So the button works, but the python script is not executed.

When I run sudo python /var/www/newDir/test.py in the console, it displays "it works", so the python script works fine alone.

What I have tried so far:

  1. making the python script executable

sudo chmod +x /var/www/newDir/test.py

now test.py has permissions -rwxr-xr-x

  1. modifing sudoers file:

sudo visudo

and I added www-data raspberrypi=NOPASSWD: /var/www/newDir/test.py

  1. giving Apache permission to run scripts

sudo a2enmod mpm_prefork cgi

  1. adding right to execute CGI to the file /etc/apache2/sites-enabled/000-default.conf:

In this file inside the VirtualHost block I added:

<Directory /var/www/newDir>
Options +ExecCGI
</Directory>

Please if you have any more ideas, describe them in more detail because I am a beginner. Thank you so much in advance!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?