Can't execute Python script from PHP document - php

I am running a PHP document on an Apache server on my Raspberry Pi and I want it to run a file when a button is clicked. I put some echo commands under the command to make the file run and it prints out but the file doesn't run. The index.php file and lightson.py and lightsoff.py files are all in the same directory (/var/www) and I have added #!/usr/bin/env python to the top of both files and made them executable by using chmod +x lightson.py. If I run the command from the shell it works and turns on the light just like I want with the exact same command as in the file but yet it wont run through the command. The code:
<html>
<head>
<title>Light Controller</title>
</head>
<?php
if (isset($_POST['LightON']))
{
shell_exec("sudo python /var/www/lightson.py");
echo("on");
}
if (isset($_POST['LightOFF']))
{
shell_exec("sudo python /var/www/lightsoff.py");
echo("Off");
}
?>
<form method="post">
<button name="LightON">Light ON</button>
<button name="LightOFF">Light OFF</button><br><br>
</form>
</html>

as you said you are running it like apache->php->shell_exec(SUDO..)
So the apache user has to be in sudoers file, better you don't give sudo to apache instead give apache (www-data) user right to run your python program
put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..
then
change group:
chgrp www-data /path/to/python-script.py
make it executabel
chmod +x /path/to/python-script.py
try it
shell_exec("/path/to/python-script.py");
I hope it works ;)
TIPP: Apache and PHP are for delivering Documents and Strings, if you want some kind of control and an API start with nodejs and https://www.npmjs.com/package/rpi-gpio package. This way you will have one place for your solid automation environment

This worked for me:
test.php
<?php
echo shell_exec("python test.py");
?>
test.py
f = open("test.txt", "a+")
f.write("hiya buddy!!\n")
f.close()
print "some output"
Here's my relevant ls -l output from /var/www/html:
jason#Jason-one /var/www/html $ ls -l
-rw-r--r-- 1 jason jason 44 Sep 20 18:12 test.php
-rwxr-xr-x 1 jason jason 82 Sep 20 17:44 test.py
-rw-rw-rw- 1 jason jason 38 Sep 20 18:15 test.txt
Since I don't have GPIO pins on my laptop, I decided to write to a file as a test. Notice I didn't have to use sudo because of the way I set the permissions on test.py.

Related

shell_exec not running in websever

I have an apache web sever with php file /var/www/html/test.php, and servo.py in the same directory.
When I go to localhost/test.php it doesn't work. Yet when I run test.php in an IDE it works fine. Any help?
test.php:
<?php
shell_exec("python servo.py");
?>
It was a permissions error! I had to add www-data to the gpio group.
sudo adduser www-data gpio

Run Debian Bash script with PHP

I'm having trouble to run a bash file using PHP.
PHP File :
chdir('/var/www/PATH/inc/bash/');
exec('./status.sh argument, $output);
Bash File :
#!/bin/bash
echo 'test' >> /var/www/PATH/inc/bashOutput/test.txt
PHP File (ls -al handler.func.php) :
-rw-r--r-- 1 root root 461 Jul 5 11:35 handler.func.php
Bash File (ls -al status.sh) :
-rwxr-xr-x 1 root root 255 Jul 5 11:39 status.sh
Script is working using through root with SSH.
I'm not a pro on Linux.
But I think it's a problem come with the file owner.
But I have already done some damages in the past with "chown" so If it is indead the problem I would prefer some guidance from more experienced people.
Thanks for you help,
Konorr.
Script is working using through root with SSH. There is the problem. When a PHP script run via a web request it usually runs as the user www-data. In anycase <?php exec('./status.sh argument, $output);?> in a security hole. Most server admins would have this disabled.
Your other option is to put sudo in your exec function exec('sudo bash /var/www/PATH/inc/bash/status.sh'). Along with running the script with an absolute path bash /var/www/PATH/inc/bash/status.sh
Why can't you run a cron on your script?
Thanks for you answer but unfortunatly it didn't worked for me.
I search a litle more about file owners.
I did few changes
I made in these change :
chown -R www-data:www-data /var/www/PATH
usermod -a -G www-data user
chgrp -R www-data /var/www/PATH
chmod 2750 /var/www/PATH
chmod 2750 /var/www/PATH/inc/bash
It wasn't yet working till I removed the sudo from the EXEC function.
So I don't know from which point my problem was already fixed.
I followed this article : www-data permissions?
Thanks for you time and in the hope it can help someone else.

Run ./script.sh from PHP

I have web server that play music from Raspberry and turn on LED with script gpio.sh.
I am using mpd, mpc and gpio.
My /var/www/index.php :
<html><body><?php
echo exec('whoami');
if(isset($_POST['button1']))
{
shell_exec('mpc play')
shell_exec('/bin/bash /var/www/gpio.sh');
}
?>
<form method="post">
<p align=center>
OUTPUT (AUDIO) => <button name="button1">PLAY</button>
</p>
</form>
</body></html>
In terminal, I can run /bin/bash /var/www/gpio.sh successfully and LED turning on.
From web server 'mpc play' WORKS and can play a song BUT it can't run that gpio.sh .
The owner of index.php is www-data
-rwx------ 1 www-data www-data 1262 Dec 8 10:45 gpio.sh
-rwx------ 1 www-data www-data 272 Dec 9 09:39 index.php
What should I do ? When I change owner of index.php or gpio.sh to root, php can't execute.
Is my index.php wrong?
I can't execute .sh from php.
Please help.
Perfect solution for you would be to set SUID for script gpio.sh but unfortunately
you can't do so as far as gpio.sh is a script.
You have three options:
You can turn your script into the say C++ application and then set SUID
You can use some GPIO lib for python that don't force usage by root like: pigpio
You can set SUID for python interpreter, but I'm not sure its good idea generally

Apache server not able to run php script containing python code

This is how my python code looks like:
[root#localhost html]# vi brow1.php
<?php
echo exec("whoami");
echo exec("ls -l brow1.php") . "<br>";
$command = escapeshellcmd('/usr/temp_rohit.py');
shell_exec($command);
?>
I want to call python script from this php.
If i run the php script then it executes the python code but when run from the browser it does not execute the python script.
python script has all the required execute permission.
The output on the web browser is:
apache
-rwxrwxrwx. 1 root root 219 Nov 26 23:03 brow1.php
-rwxrwxrwx. 1 root root 115 Nov 26 22:38 /usr/temp_rohit.py
I am stuck at this point, Please help!

php shell_exec no permission

I am using a php script to call a backend python script.
<?php
error_reporting(-1);
$output = shell_exec("sh run.sh 2>&1");
echo "<pre>$output</pre>";
?>
The run.sh script is:
#!/bin/bash
wget http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
python hw7-9.py index.html
echo "done";
The output is
run.sh: wget: not found
run.sh: python: not found
done
If I run it normally from shell it works perfectly.
to try and fix the not found I did "which wget" and replace full path
/afs/cad/sw.common/bin/wget -O index.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
I get permission denied
What are the permissions of your php and your shell script?
I've used the same approach that you're using, successfully. Full ownership and attribute details below.
# ls -l
-rw-r--r-- 1 root root 2332 Jan 4 23:07 daily.php
-rwxr-xr-x 1 root root 232 Oct 30 22:43 get_stuff.sh
The user/group ownership on your system will vary. The read/write/execute permissions don't have to strictly match mine, either. But for reference, my setup is achieved via:
chmod 644 daily.php
chmod 755 get_stuff.sh
chown root:root *

Categories