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:
- making the python script executable
sudo chmod +x /var/www/newDir/test.py
now test.py has permissions -rwxr-xr-x
- modifing sudoers file:
sudo visudo
and I added www-data raspberrypi=NOPASSWD: /var/www/newDir/test.py
- giving Apache permission to run scripts
sudo a2enmod mpm_prefork cgi
- 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!!